J'écris un programme qui doit analyser la variable d'environnement PATH à un délimiteur ":". Tout semble fonctionner correctement jusqu'à ce que la fonction coupe essaye de retourner le tableau. Je reçois alors l'erreur suivante: "glibc détecté ./a.out: liste double-liée corrompue: 0x08f8f148". J'utilise le chop pour analyser aussi les entrées utilisateur et cela fonctionne correctement. Toute aide est grandement appréciée.C++: liste double-liée corrompue détectée par la glibc: 0x08f8f148
//Parse environment variable
char const* pPath = getenv("PATH");
if (pPath == NULL){;}
else{
string ePath(pPath);
envp = chop(ePath,':');
}
char **chop(string s, char c){
int i, j, k, len, words = 0;
len = s.length();
//determine # of words
for(i=0;i<len+1;i++){
if(s[i] == c || s[i] == '\0'){words++;}
}
char **array;
string x;
//allocate memory for char pointers
if((array=(char**)malloc((words+1)*sizeof(char*))) == NULL){return NULL;}
array[0] = &x[0];
i = 0; k = 0;
//split string on char c
for(j = 1; j < (words+1); j++,k++,i++){
//read in characters until delimiter
while (s[k] != c && s[k] != '\0'){
x[i] = s[k];
i++; k++;
}
x[i] = '\0';
array[j] = &x[i+1];
}
array[j] = 0;
return array;
}
L'erreur n'est pas dans le code que vous avez montré. Très probablement, vous supprimez quelque chose deux fois. –
duplicata possible de [Qu'est-ce qu'une erreur glibc libre/malloc/realloc invalide taille suivante/pointeur invalide et comment y remédier?] (Http://stackoverflow.com/questions/23680334/what-is-a-glibc-free -malloc-realloc-invalide-next-size-invalide-pointeur-erreur-et) – PlasmaHH