为什么当 current->next == NULL 时 current = current->next 会出现分段错误?
Why current = current->next gives segmentation fault when current->next == NULL?
我有下面的 C 程序来实现升序链表。问题出在 buildList() 函数中,因此您可以忽略除 main() 和 buildList() 之外的其他函数。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void buildList(struct node **, int);
void printList(struct node **);
int main()
{
struct node *head;
head = NULL; /*Empty list*/
int hold, i, j;
printf("How many integers in this list: ");
scanf("%d",&i);
j = 1;
for(i; i > 0; i--)
{
printf("Integer %d: ",j);
scanf("%d",&hold);
buildList(&head, hold);
j++;
}
printList(&head);
return 0;
}
void buildList(struct node **headRef, int data)
{
struct node *newNode;
newNode = malloc(sizeof(struct node));
struct node *current, *current1;
current = *headRef;
//If list is empty, add the number as the first node.
if (current == NULL)
{
newNode->data = data;
newNode->next = *headRef;
*headRef = newNode;
}
//If the list is not empty.
else
{
//If the number is not greater than first number.
if (data <= current->data)
{
newNode->data = data;
newNode->next = current;
*headRef = newNode;
}
//If the number is greater than the first number in list.
else
{
int flag = 0;
while (data > (current->data))
{
current1 = current;
current = current->next;
}
newNode->data = data;
current1->next = newNode;
newNode->next = current;
}
}
}
//Prints nodes and total number of nodes.
void printList(struct node **headRef)
{
int count = 0;
struct node *current;
current = *headRef;
while (current != NULL)
{
count++;
printf("%d ",current->data);
current = current->next;
}
printf("\nTotal nodes: %d\n",count);
}
在我给出一个比列表中存在的任何数字都大的数字之前,该程序工作正常。在那种情况下,我会遇到分段错误。
案例一(精细正确输出)
-bash-4.1$ ./a.out
How many integers in this list: 3
Integer 1: 5
Integer 2: 1
Integer 3: 4
1 4 5
Total nodes: 3
-bash-4.1$
情况二(此处代码中断(段错误))
-bash-4.1$ ./a.out
How many integers in this list: 3
Integer 1: 5
Integer 2: 6
Segmentation fault
-bash-4.1$
经过很长时间试图弄清楚我的代码出了什么问题,我发现当一个数字大于列表中的任何数字时,在这种情况下,它应该被插入到列表,此语句(在函数 buildList() 的最后一个 else 中)导致了问题:
current = current->next;
我终于弄明白当current->next 为NULL 时,这条语句会导致seg 错误。
我想出了一个如下所示的解决方法,它可以提供正确的输出。
else
{
int flag = 0;
while (data > (current->data))
{
current1 = current;
if(current->next != NULL)
{
current = current->next;
}
else
{
flag = 1;
break;
}
}
newNode->data = data;
current1->next = newNode;
if (flag == 1)
{
newNode->next = NULL;
}
else
{
newNode->next = current;
}
}
现在我得到了正确的输出。
-bash-4.1$ ./a.out
How many integers in this list: 3
Integer 1: 5
Integer 2: 1
Integer 3: 10
1 5 10
Total nodes: 3
-bash-4.1$
现在我想知道为什么不是 current = current->next;当 current->next 为 NULL 时工作。我期待这条语句将 NULL 分配给 current.
谁能告诉我这是什么原因?
我的解决方法也很好吗?或者有更好的方法吗?
抱歉问了这么长的问题,但花了大约 2 个小时调试这个,我想我快疯了。
谢谢。
您的调试技术有点误会了您。问题出在这个循环中:
while (data > (current->data))
{
current1 = current;
current = current->next;
}
但不是你认为的那样。如果 current->next
是 NULL
,则 current
会像您描述的那样设置为 NULL
。然而,下一个发生的操作是循环条件——取消引用 current
。布拉莫。你可能想要这样的东西:
while (current && (data > current->data))
{
current1 = current;
current = current->next;
}
我没有分析你的整个程序,所以除了明显的崩溃之外,我无法真正评论这部分的正确性。
当第二个输入大于第一个输入时,这个while
循环不会停止。
while ( data > (current->data))
{
current1 = current;
current = current->next;
}
您最终取消了对 NULL 指针的引用。您需要:
while ( current != NULL && data > (current->data))
{
current1 = current;
current = current->next;
}
我有下面的 C 程序来实现升序链表。问题出在 buildList() 函数中,因此您可以忽略除 main() 和 buildList() 之外的其他函数。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void buildList(struct node **, int);
void printList(struct node **);
int main()
{
struct node *head;
head = NULL; /*Empty list*/
int hold, i, j;
printf("How many integers in this list: ");
scanf("%d",&i);
j = 1;
for(i; i > 0; i--)
{
printf("Integer %d: ",j);
scanf("%d",&hold);
buildList(&head, hold);
j++;
}
printList(&head);
return 0;
}
void buildList(struct node **headRef, int data)
{
struct node *newNode;
newNode = malloc(sizeof(struct node));
struct node *current, *current1;
current = *headRef;
//If list is empty, add the number as the first node.
if (current == NULL)
{
newNode->data = data;
newNode->next = *headRef;
*headRef = newNode;
}
//If the list is not empty.
else
{
//If the number is not greater than first number.
if (data <= current->data)
{
newNode->data = data;
newNode->next = current;
*headRef = newNode;
}
//If the number is greater than the first number in list.
else
{
int flag = 0;
while (data > (current->data))
{
current1 = current;
current = current->next;
}
newNode->data = data;
current1->next = newNode;
newNode->next = current;
}
}
}
//Prints nodes and total number of nodes.
void printList(struct node **headRef)
{
int count = 0;
struct node *current;
current = *headRef;
while (current != NULL)
{
count++;
printf("%d ",current->data);
current = current->next;
}
printf("\nTotal nodes: %d\n",count);
}
在我给出一个比列表中存在的任何数字都大的数字之前,该程序工作正常。在那种情况下,我会遇到分段错误。
案例一(精细正确输出)
-bash-4.1$ ./a.out
How many integers in this list: 3
Integer 1: 5
Integer 2: 1
Integer 3: 4
1 4 5
Total nodes: 3
-bash-4.1$
情况二(此处代码中断(段错误))
-bash-4.1$ ./a.out
How many integers in this list: 3
Integer 1: 5
Integer 2: 6
Segmentation fault
-bash-4.1$
经过很长时间试图弄清楚我的代码出了什么问题,我发现当一个数字大于列表中的任何数字时,在这种情况下,它应该被插入到列表,此语句(在函数 buildList() 的最后一个 else 中)导致了问题:
current = current->next;
我终于弄明白当current->next 为NULL 时,这条语句会导致seg 错误。
我想出了一个如下所示的解决方法,它可以提供正确的输出。
else
{
int flag = 0;
while (data > (current->data))
{
current1 = current;
if(current->next != NULL)
{
current = current->next;
}
else
{
flag = 1;
break;
}
}
newNode->data = data;
current1->next = newNode;
if (flag == 1)
{
newNode->next = NULL;
}
else
{
newNode->next = current;
}
}
现在我得到了正确的输出。
-bash-4.1$ ./a.out
How many integers in this list: 3
Integer 1: 5
Integer 2: 1
Integer 3: 10
1 5 10
Total nodes: 3
-bash-4.1$
现在我想知道为什么不是 current = current->next;当 current->next 为 NULL 时工作。我期待这条语句将 NULL 分配给 current.
谁能告诉我这是什么原因? 我的解决方法也很好吗?或者有更好的方法吗?
抱歉问了这么长的问题,但花了大约 2 个小时调试这个,我想我快疯了。
谢谢。
您的调试技术有点误会了您。问题出在这个循环中:
while (data > (current->data))
{
current1 = current;
current = current->next;
}
但不是你认为的那样。如果 current->next
是 NULL
,则 current
会像您描述的那样设置为 NULL
。然而,下一个发生的操作是循环条件——取消引用 current
。布拉莫。你可能想要这样的东西:
while (current && (data > current->data))
{
current1 = current;
current = current->next;
}
我没有分析你的整个程序,所以除了明显的崩溃之外,我无法真正评论这部分的正确性。
当第二个输入大于第一个输入时,这个while
循环不会停止。
while ( data > (current->data))
{
current1 = current;
current = current->next;
}
您最终取消了对 NULL 指针的引用。您需要:
while ( current != NULL && data > (current->data))
{
current1 = current;
current = current->next;
}