簡體   English   中英

Valgrind /內存錯誤

[英]Valgrind / Memory Errors

當我使用valgrind時,在我的代碼中反復收到以下錯誤。 我不太清楚這些是什么意思,也無法識別未初始化的值。

==16795== Conditional jump or move depends on uninitialised value(s)
==16795==    at 0x4A06E8A: strcmp (mc_replace_strmem.c:412)
==16795==    by 0x4009C7: dictionary_add (libdictionary.c:44)
==16795==    by 0x40061B: main (part2.c:28)
==16795== 
==16795== Invalid write of size 1
==16795==    at 0x4A082E7: strcpy (mc_replace_strmem.c:303)
==16795==    by 0x400AA8: dictionary_add (libdictionary.c:57)
==16795==    by 0x40061B: main (part2.c:28)
==16795==  Address 0x4c361a3 is 0 bytes after a block of size 3 alloc'd
==16795==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==16795==    by 0x400931: node_newnode (libdictionary.c:28)
==16795==    by 0x400A8C: dictionary_add (libdictionary.c:54)
==16795==    by 0x40061B: main (part2.c:28)

我正在創建一個鏈表數據結構,這些是給出此內存錯誤時要調用的函數。

//這是節點和字典數據結構的構造。

typedef struct node
{
char* key;
char* value;
struct node* next;
}node;


typedef struct _dictionary_t
{
node* head;

} dictionary_t;

//首先創建一個新字典

void dictionary_init(dictionary_t *d)
{
d->head = NULL;
d->head = malloc(sizeof(node));
node_init(d->head);
}

//使用node_init方法創建一個新節點。

void node_init(node *n)
{
n->key = NULL;
n->value =NULL;

n->key = malloc(sizeof(char));
n->value = malloc(sizeof(char));
n->next = NULL;
}

//此新的node方法在原始初始化之后使用(當我們添加新術語時)。前一個方法專門用於在創建結構的開頭創建哨兵。

void node_newnode(node *n, int x, int y)
{
n->key = NULL;
n->value = NULL;

n->key = malloc(x*sizeof(char));
n->value = malloc(y*sizeof(char));
n->next = NULL;
}

//在這種情況下,也會調用此函數以將“鍵”和“值”成對添加。

int dictionary_add(dictionary_t *d, const char *key, const char *value)
{
node *current;
current =  d->head;
if(strcmp(current->key,key)==0)
        return -1;
while(current->next != NULL){
        current=current->next;
        if(strcmp(current->key,key)==0)
                return -1;
}
        current->next = NULL;
        current->next = malloc(sizeof(node));

        node_newnode(current->next,strlen(key),strlen(value));

        current = current->next;
        strcpy((current->key), key);
        strcpy((current->value),value);
        return 0;
}

任何人對我為什么會得到這些錯誤有任何想法。 到目前為止,main方法僅創建了一個字典並稱為add函數。 Valgrind在8個函數調用過程中報告了38個上下文中的45+個錯誤。 我認為這些可能是我反復犯的小錯誤。

我現在也收到與此函數相同的錯誤。 valgrind --track-origins = yes命令在以下函數中將其跟蹤到堆棧分配:

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;

}

謝謝,-newprogrammer

第一個錯誤(“有條件的跳轉或移動取決於未初始化的值”)是因為current->key指向的字符從未初始化。 您在node_init()node_init()分配了一個字節,但實際上從未將該字節設置為任何值。 如果您希望head節點的key像一個空字符串一樣,請像這樣更改node_init()

n->key = malloc(sizeof(char));
n->key[0] = '\0';

第二個錯誤(“大小為1的無效寫入”)是因為您的node_newnode()函數分配的字節數少於字符串所需的字節數。 strlen(key)計算字符串中的實際字符,但空終止符字符還需要一個字節。 采用:

node_newnode(current->next,strlen(key) + 1,strlen(value) + 1);

(但就我個人而言,我只是將keyvalue傳遞給node_newnode() ,並讓它既完成分配又完成strcpy() )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM