Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
在结构drzewo.root-> value中,我有'5';
我这样打印,然后调用函数findNodeWithValue():
该函数的定义如下:
该程序的结果是:
5
4200289
为什么?
这是整个程序:http://pastebin.com/n2iS7bWt
如果删除结构,则必须记住要删除节点。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
在结构drzewo.root-> value中,我有'5';
我这样打印,然后调用函数findNodeWithValue():
printf("%d \n", drzewo.root->value);
return findNodeWithValue(drzewo.root, value);
该函数的定义如下:
Node* findNodeWithValue(Node * wierzcholek, int value) {
printf("%d \n", wierzcholek->value);
}
该程序的结果是:
5
4200289
为什么?
这是整个程序:http://pastebin.com/n2iS7bWt
最佳答案
我怀疑尚未初始化,并且指向堆栈中的某个位置,由下一个函数调用推送的数据会修改指向的值。
编辑
现在,我们已经有了完整的源代码,我们看到或多或少确实发生了这种情况:在drzewo.root
中,无需分配新节点,只需在堆栈上声明它即可。一旦离开insert()
的范围,引用该节点的内存就不再合法。
幸运的是,这很容易修复,只需正确分配新节点即可:
drzewo.root = new Node;
drzewo.root->value = value;
如果删除结构,则必须记住要删除节点。
关于c++ - 奇怪的printf()结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23408300/