我正在尝试初始化一个指向struct TLDNode的指针数组。我通过遍历树并按顺序添加节点来完成此操作。但是,我正在使用valgrind的memcheck工具,并指出:
Conditional jump or move depends on uninitialised value(s)
at 0x400D55: main (tldmonitor.c:88)
Uninitialised value was created by a heap allocation
at 0x4C28C0F: malloc (vg_replace_malloc.c:299)
by 0x4014CA: tldlist_iter_create (tldlist.c:146)
by 0x400CC8: main (tldmonitor.c:83)
我很困惑,因为我正在初始化,即使它处于不同的功能。
代码: tldlist.c
static void inorder_add(TLDIterator *iter, TLDNode *c) {
if(c != NULL) {
inorder_add(iter,c->left);
*(iter->array + iter->index) = c;//intialize each index
iter->index++;
inorder_add(iter,c->right);
}
}
TLDIterator* tldlist_iter_create(TLDList *tld) {
TLDIterator *iter = malloc(sizeof(TLDIterator));
if(iter != NULL) {
iter->length = tldlist_count(tld);
iter->index = 0;
iter->array = malloc(sizeof(TLDNode*)*iter->length);//line 146
inorder_add(iter,tld->root);
iter->index = 0;
return iter;
}
return NULL;
}
TLDNode* tldlist_iter_next(TLDIterator* iter) {
if(iter->index < iter->length) {
TLDNode *next = *(iter->array+iter->index);
iter->index++;
return next;
}
return NULL;
}
tldmonitor.c:
tld = tldlist_create(begin, end);
//nodes are added in between
total = (double)tldlist_count(tld);
it = tldlist_iter_create(tld); //line 83
while ((n = tldlist_iter_next(it))) {//line 88 (84-87 not needed)
printf("%6.2f %s\n", 100.0 * (double)tldnode_count(n)/total, tldnode_tldname(n));
}
tldmonitor应打印出树中每个键的计数百分比。但是,tldmonitor.c是作为入门代码提供的,所以我的问题必须在tldlist.c中。
为什么说我没有初始化?是因为我是在一个单独的功能中做到的吗?或者我实际上没有以某种方式初始化数组?