Linux版 - Valgrind报uninitialized value was created by a heap allocation |
|
|
|
|
|
o****i 发帖数: 1706 | 1 程序运行正常,可是在测memery leak的时候报上面那个错,具体错误消息如下:
==25663== Conditional jump or move depends on uninitialised value(s)
==25663== at 0x400C9F: add_edge (graph.c:59)
==25663== by 0x40071A: main (main.c:13)
==25663== Uninitialised value was created by a heap allocation
==25663== at 0x4A0515D: malloc (vg_replace_malloc.c:195)
==25663== by 0x400A80: add_vertex (graph.c:22)
==25663== by 0x400DF8: add_edge (graph.c:84)
==25663== by 0x40071A: main (main.c:13)
我的程序用链接表构建一个无向图来解决TSP的.
graph.c:22
for(adj_v=(*vp)->adj_list; adj_v->next!=NULL; adj_v=adj_v->next);
adj_v->next = (adj_vertex_t *)malloc(sizeof(adj_vertex_t));
adj_v=adj_v->next;
adj_v->edge_weight=weight;
我这个该怎么初始化新alloc的memery space呢?因为我马上要加新的节点信息到这个
地方了,看起来应该没问题啊?C语言不太好,请大牛指教 | S*A 发帖数: 7142 | 2 有两个问题。
1)你刚刚 malloc 的 pointer 没有检查是不是 NULL.
后面就直接赋值了。 segfault if out of memory
2) malloc 出来的 memory, ptr->next 没有赋值。
ptr->next = NULL; 应该就可以了。
【在 o****i 的大作中提到】 : 程序运行正常,可是在测memery leak的时候报上面那个错,具体错误消息如下: : ==25663== Conditional jump or move depends on uninitialised value(s) : ==25663== at 0x400C9F: add_edge (graph.c:59) : ==25663== by 0x40071A: main (main.c:13) : ==25663== Uninitialised value was created by a heap allocation : ==25663== at 0x4A0515D: malloc (vg_replace_malloc.c:195) : ==25663== by 0x400A80: add_vertex (graph.c:22) : ==25663== by 0x400DF8: add_edge (graph.c:84) : ==25663== by 0x40071A: main (main.c:13) : 我的程序用链接表构建一个无向图来解决TSP的.
|
|
|
|
|
|