J'ai créé une classe de liste chaînée, mais cette fonction génère des erreurs de type valgrind, indiquant qu'il existe un saut conditionnel basé sur une valeur non initialisée dans cette fonction. Je ne suis pas exactement sûr de ce que je dois faire pour le réparer. Il y a essentiellement une classe de nœuds pour la liste chaînée et c'est l'itération sur tous les nœuds vérifiant si le paramètre clé correspond à un nœud préexistant et s'il le renvoie la valeur.Valgrind Valeurs non initialisées (création d'une structure de données de liste chaînée)
const char *dictionary_get(dictionary_t *d, const char *key)
{
node* current;
current = d->head;
if(strcmp(current->key,key)==0)
return current->value;
while(current->next != NULL){
current = current->next;
if(current!=NULL && strcmp(current->key,key)==0)
return current->value;
}
return NULL;
}
Des idées?
J'ai suivi avec des origines réexaminé de valgrind et c'est la sortie:
==25042== Conditional jump or move depends on uninitialised value(s)
==25042== at 0x4A06E6A: strcmp (mc_replace_strmem.c:412)
==25042== by 0x400DD6: dictionary_get (libdictionary.c:143)
==25042== by 0x400826: main (part2.c:84)
==25042== Uninitialised value was created by a stack allocation
==25042== at 0x400AE3: dictionary_parse (libdictionary.c:69)
==25042==
==25042== Conditional jump or move depends on uninitialised value(s)
==25042== at 0x4A06E8A: strcmp (mc_replace_strmem.c:412)
==25042== by 0x400DD6: dictionary_get (libdictionary.c:143)
==25042== by 0x400826: main (part2.c:84)
==25042== Uninitialised value was created by a stack allocation
==25042== at 0x400AE3: dictionary_parse (libdictionary.c:69)
==25042==
==25042== Conditional jump or move depends on uninitialised value(s)
==25042== at 0x400DD9: dictionary_get (libdictionary.c:143)
==25042== by 0x400826: main (part2.c:84)
==25042== Uninitialised value was created by a stack allocation
==25042== at 0x400AE3: dictionary_parse (libdictionary.c:69)
On dirait que cela pourrait venir de dictionary_parse donc je vais aussi poster cette fonction.
int dictionary_parse(dictionary_t *d, char *key_value)
{
char* colon;
char* space;
colon = key_value;
space = key_value;
space++;
int key_length = -1; //Default key length to check for failure
int i=0;
int j=0; // Loop variables
int k=0;
int length = strlen(key_value);
for(i=0;i<length-2;i++){
if(*colon == ':' && *space == ' '){
key_length = i;
break;
}
colon++;
space++;
}
if(key_length == -1 || key_length == 0)
return -1;
int value_length = length-2-key_length;
colon = key_value;
char key_word[key_length];
key_word[0] = '\0';
char value_word[value_length];
value_word[0] = '\0';
for(j=0;j<key_length;j++){
key_word[j] = *colon;
colon++;
}
space++;
for(k=0; k<value_length;k++){
value_word[k] = *space;
space++;
}
char* finalkey[key_length];
strcpy((char*)finalkey,key_word);
char* finalvalue[value_length];
strcpy((char*)finalvalue,value_word);
dictionary_add(d,(char*)finalkey,(char*)finalvalue);
return 0;
}
Hé, s'il vous plaît exécuter Valgrind la même façon que vous avez été, mais aussi avec le '--track-origins = yes' et collez la sortie dans la question. Aussi, vous pouvez utiliser 'gdb' pour inspecter le programme avec' --db-attach = yes' – alternative
Quelle ligne est la ligne 69 dans libdictionary.c? Quelle est la ligne 84 dans la partie2.c? – alternative
84 dans part2.c: s = dictionary_get (& dictionary, "e"); 69 dans libdict.c: int dictionary_parse (dictionnaire_t * d, char * valeur_clé) { – newprogrammer