![](/img/trans.png)
[英]C - linked list valgrind uninitialized error depending on order elements are added
[英]Valgrind Uninitialized Values (making a linked list data struct)
我已經創建了一個鏈表類,但是這個函數正在生成valgrind錯誤,該錯誤表示在此函數中有一個基於未初始化值的條件跳轉。 我不確定我需要做些什么來解決它。
本質上,鏈接列表有一個節點類,它迭代所有節點,檢查key參數是否與預先存在的節點匹配,如果是,則返回該值。
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;
}
有任何想法嗎?
我已經重新審視了valgrind跟蹤起源,這是輸出:
==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)
看起來這可能來自dictionary_parse所以我也會發布這個函數。
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;
}
線條像
char key_word[key_length];
看起來非常可疑。
我不知道你對這些做了什么,但是為那些應該持續比函數調用更長時間的東西創建一個臨時變量長度數組似乎很奇怪。
此外,可變長度數組不包括終止'\\0'
。
你是不是正確空終止的字符串key_word
和value_word
,而這種錯誤顯然是傳播通過。 這個循環是個問題:
for(j=0;j<key_length;j++){
key_word[j] = *colon;
colon++;
}
它將key_length
字符復制到key_word
,但這些復制的字符都不是空終止符。 您可以通過向key_word
添加一個額外字節來解決問題:
char key_word[key_length + 1];
然后在for()
循環后添加:
key_word[key_length] = '\0';
也沒有必要在finalkey
和finalvalue
創建副本(無論如何都有錯誤的類型 - 這就是為什么你最終需要所有那些丑陋的演員表)。 所以整體看起來像這樣:
char key_word[key_length + 1];
char value_word[value_length + 1];
for (j = 0; j < key_length; j++) {
key_word[j] = *colon;
colon++;
}
key_word[key_length] = '\0';
space++;
for(k = 0; k < value_length; k++) {
value_word[k] = *space;
space++;
}
value_word[value_length] = '\0';
dictionary_add(d, key_word, value_word);
實際上,你應該使用string.h
的工具來簡化這個功能。 例如, strstr()
將允許您搜索分隔鍵和值的": "
字符串,而memcpy()
與for()
循環的等效字符串相同:
int dictionary_parse(dictionary_t *d, char *key_value)
{
char *colon;
char *value;
int key_length = -1; //Default key length to check for failure
colon = strstr(key_value, ": ");
if (colon != NULL) {
key_length = colon - key_value; // Number of characters before the colon
value = colon + 2; // Value is portion of the string after ": "
}
if (key_length < 1) {
return -1;
}
char key_word[key_length + 1];
memcpy(key_word, key_value, key_length);
key_word[key_length] = '\0';
dictionary_add(d, key_word, value);
return 0;
}
如果您的程序正常運行,請不要理會這些警告。 我已經看到了程序上的條件跳轉警告,否則它們會完美運行。 它可能與編譯器生成的匯編代碼有關,而與代碼沒有直接關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.