当我使用valgrind时,我在我的代码中反复收到以下错误。我不太确定这些是什么意思,我无法确定未初始化的值。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;
//首先//一个新的节点是使用node_init方法创建一个新的字典创建
void dictionary_init(dictionary_t *d)
{
d->head = NULL;
d->head = malloc(sizeof(node));
node_init(d->head);
}
。
void node_init(node *n)
{
n->key = NULL;
n->value =NULL;
n->key = malloc(sizeof(char));
n->value = malloc(sizeof(char));
n->next = NULL;
}
//该新节点方法(当我们增加新的术语)的前一个是专门为在创建所述结构的开始创建一个前哨原始初始化后使用。
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;
}
//这个函数也被调用来在这种情况下添加“key”和“value”作为一对。
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;
}
任何人有任何想法,为什么我得到这些错误。迄今为止,主要的方法只创建了一个字典并称为add函数。 Valgrind在8个函数调用过程中报告了超过38个上下文中的45多个错误。我认为这些可能是我重复犯下的小错误。
我现在也得到这个函数相同的错误。甲的valgrind --track-起源= 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
顺便说一句,为什么在分配'malloc'返回的指针之前立即为变量赋值'NULL'?这没有必要。 – 2010-09-05 06:46:51