Segfault from adding a variable

。_饼干妹妹 提交于 2019-12-06 13:44:05

You need to dynamically allocate your nodes (using malloc).

As you have it, your new node is declared on the stack. When the split function returns, that new node is no longer valid memory.

Adding a variable causes a segfault because that variable changes the layout of the stack causing slightly different behavior when the function returns.

Try setting the Nodes child property to NULL, C doesn't automagically zero out memory so it looks like your may have garbage in child (or your could use calloc instead of malloc). SoapBox's answer is also correct.

Valgrind is a great tool to help find these types of problems. You can just do "valgrind myappname" from the command line and it will give you details on these types of errors.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!