以下功能是否有問題?不知何故,他們正在創建一個段錯誤。調試以下功能(段故障錯誤)
struct processNode* create_node()
{
struct processNode* newNode = (struct processNode*)malloc(sizeof(struct processNode));
newNode->next = NULL;
return newNode;
}
struct processNode* append_node(struct processNode* list,struct processNode* newNode)
{
struct processNode* tracker= NULL;
tracker = list;
if(tracker == NULL)
{
tracker = newNode;
}
else
{
while(tracker->next != NULL)
{
tracker =tracker->next;
}
tracker->next = newNode;
tracker = tracker->next;
}
tracker->next=NULL;
tracker = list;
return tracker;
}
我在C中創建一個shell,需要創建一個鏈接列表來解析用戶的命令行。在第二個函數中,我打算用新添加的指針返回一個新列表;
將'list'視爲NULL。現在走這個功能。 *小心*。你認爲'tracker = list'確實如此(注意:它在該函數中出現兩次*;在'if'塊中出現一次*,在'else'塊之後出現一次*)。嗯。 – WhozCraig
你是否真的試圖在調試器中運行它(根據你的問題的標題)?這應該告訴你哪裏出了問題。我還發現valgrind是這些問題的有用工具,因爲它可能會導致內存分配出錯。 – Evert
[請參閱此討論爲什麼不在'C'中投射'malloc()'和家族的返回值。](http://stackoverflow.com/q/605845/2173917)。 –