gpt4 book ai didi

c++ - 双链表实现中的"Conditional jump or move depends on uninitialised value"

转载 作者:太空宇宙 更新时间:2023-11-04 04:49:50 25 4
gpt4 key购买 nike

Valgrind 告诉我两个我无法解决的错误。我在以下函数 unplug_set 中收到“条件跳转或移动取决于未初始化的值”错误消息。是因为 NULL 检查吗?该函数应该从双链表中拔出元素。

void unplug_set(set * set_to_unplug){
set * last_set = set_to_unplug->last;
set * next_set = set_to_unplug->next;
set_to_unplug->next = NULL;
set_to_unplug->last = NULL;
148 if(last_set == NULL && next_set == NULL){ // only one element left
ordered_by_score_set[set_to_unplug->weight] = NULL;
150 }else if(last_set == NULL){ // first set
next_set->last = NULL;
ordered_by_score_set[set_to_unplug->weight] = next_set;
} else if (next_set == NULL) { // last set
last_set->next = NULL;
} else { // set in the middle
last_set->next = next_set;
next_set->last = last_set;
}
}

Valgrind 命令:

valgrind --dsymutil=yes --track-origins=yes ./set_program

输出:

==45085== Conditional jump or move depends on uninitialised value(s)
==45085== at 0x100001A2C: unplug_set(set*) (:148)
...
==45085== by 0x100001294: main (Main.cpp:81)
==45085== Uninitialised value was created by a heap allocation
==45085== at 0xC713: malloc (vg_replace_malloc.c:274)
==45085== by 0x63346: operator new(unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==45085== by 0x100001E0C: add_set(int, int, unsigned int const*, unsigned short const*, int, unsigned int const*) (:57)
==45085== by 0x10000127E: main (Main.cpp:39)
==45085==
==45085== Conditional jump or move depends on uninitialised value(s)
==45085== at 0x100001A60: unplug_set(set*) (:150)
...
==45085== by 0x100001294: main (Main.cpp:81)
==45085== Uninitialised value was created by a heap allocation
==45085== at 0xC713: malloc (vg_replace_malloc.c:274)
==45085== by 0x63346: operator new(unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==45085== by 0x100001E0C: add_set(int, int, unsigned int const*, unsigned short const*, int, unsigned int const*) (:57)
==45085== by 0x10000127E: main (Main.cpp:39)

最佳答案

set_to_unplug 参数值由其构造函数使用 new 运算符初始化。如果构造函数中没有初始化,那么你就有了 valgrind 提示的原因。

我们没有足够的信息来回答您的问题。发布如何使用此功能。以及如何初始化参数值。

关于c++ - 双链表实现中的"Conditional jump or move depends on uninitialised value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16718847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com