C 堆栈清除后打印链表时出现SEGFULT
更具体地说,这段代码应该是Unix函数dc的一个较小的克隆。链表似乎运行良好。如果我尝试使用c清除内存,添加更多数字,然后用f再次打印,我会得到一个segfault。它似乎正在打印链接列表中的空节点C 堆栈清除后打印链表时出现SEGFULT,c,linked-list,C,Linked List,更具体地说,这段代码应该是Unix函数dc的一个较小的克隆。链表似乎运行良好。如果我尝试使用c清除内存,添加更多数字,然后用f再次打印,我会得到一个segfault。它似乎正在打印链接列表中的空节点 Interaction: $ ./test 1 2 3 f 3 2 1 c 4 5 f 5 4 0 Segmentation Fault 下面是代码本身: #include <stdio.h> #include <stdlib.h> struct Node{ int
Interaction:
$ ./test
1 2 3
f
3
2
1
c
4 5
f
5
4
0
Segmentation Fault
下面是代码本身:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int val;
struct Node *next;
};
void cons_node(struct Node **head, int num)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->val = num;
newNode->next = NULL;
if (*head == NULL){
*head = newNode;
}
else {
newNode->next = *head;
*head = newNode;
}
}
或者在clear func中:
void clear_stack(struct Node *head)
{
struct Node *current;
while ((current = head)!= NULL) {
head = head->next;
free(current);
}
}
void vorpal_func(struct Node *head)
{
struct Node *current;
current = head;
free(current);
}
int main(){
int input;
int first = 1;
char quit = 'n';
char inputchar = ' ';
struct Node *head = NULL;
while (quit == 'n'){
if (scanf("%d", &input) == 1){
cons_node(&head, input);
first = 0;
}
else{
inputchar = getchar();
if(first == 1)
printf("List is empty\n");
else{
switch (inputchar){
case 'q':
quit = 'y';
break;
case 'E':
quit = 'y';
break;
case 'f':
display_list(head);
break;
case 'p':
print_top(head);
break;
case 'c':
clear_stack(head);
first = 1;
break;
case 't':
vorpal_func(head);
break;
}
}
}
}
return 0;
}
几个小时以来,我一直在试图解决这个问题。有什么提示吗?在调用clear\u stack之后,您不会清空头部,因此当您添加下一个节点时,下一个指针将被设置为指向已释放内存的某个对象。或者,如果愿意,也可以将指针传递到head以清除堆栈
void clear_stack(struct Node **head)
{
while (*head != NULL)
{
Node *current = *head;
*head = current->next;
free(current);
}
}
顺便说一下,cons_节点可以这样写
void cons_node(struct Node **head, int num)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->val = num;
newNode->next = *head;
*head = newNode;
}
结构节点newNode=(结构节点)malloc(sizeof(结构节点));-请不要投malloc的结果,@TomTanner,嗯,因为我的教授是这样做的,所以才加了一点。我可以删除它,但我可以问为什么吗?看这里-这是不必要的,可能会导致奇怪的运行时错误。而且它使代码更难阅读感谢cons_节点的清理和建议。是的,你是对的。我需要睡觉,哈哈。谢谢。如果不太麻烦的话,你能猜一下为什么vorpal_func也会出现seg故障吗?@BlakeHoward最好不要将头指针直接指向函数。如果这些函数修改了头部,则不会得到链表的开头。取而代之的是在temp变量中取head值并传递这些值。@BlakeHoward不确定它被调用的位置/方式,我只能假设它被传递了一个错误的指针
void cons_node(struct Node **head, int num)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->val = num;
newNode->next = *head;
*head = newNode;
}