不使用全局列表时 C 列表的分段错误
Segmentation Fault with C List when not using global List
我已经编写了一些代码来创建一个单链表,然后在 C 中将其转换为动态数组。
如果我只为列表 header 创建一个全局变量,代码就可以正常工作。但是,如果我想在我的主函数中创建列表,我总是会遇到分段错误。
只要我创建一个全局列表变量并将该列表作为参数从所有函数中删除,下面的代码就可以正常工作。
如果我想将列表作为参数传递给函数并因此能够创建多个列表,谁能告诉我为什么这不起作用?
#include <stdlib.h>
#include <stdio.h>
typedef struct NodeStruct* Node;
typedef struct NodeStruct {
Node next;
int val;
} NodeStruct;
typedef Node List;
Node newNode(int x){
Node n = (Node)malloc(sizeof(NodeStruct));
if(n!=NULL){
n->val = x;
n->next = NULL;
return n;
}
else{
printf("ERROR: Could not allocate memory!\n");
}
exit(1);
}
void prepend(List l, Node node){
if (l == NULL) l = node;
else{
node->next = l;
l = node;
}
}
void printList(List l){
if(l!=NULL){
Node n = l;
while(n->next != NULL){
printf("%d, ", n->val);
n = n->next;
}
printf("%d\n", n->val);
}
else{
printf("ERROR: List empty!\n");
}
}
/*=============================*/
int* arrOf(List l){
if(l==NULL){
printf("ERROR: List empty\n");
exit(1);
}
int size = 0;
Node n = l;
while(n!=NULL){
size++;
n = n->next;
}
int* arr = (int*)malloc((size+1)*sizeof(int));
n = l;
int i = 0;
arr[i++] = size;
while(n != NULL){
arr[i++] = n->val;
n = n->next;
}
printf("Returning Array\n");
return arr;
}
int main(int argc, char *argv[]){
List l;
prepend(l, newNode(5));
prepend(l, newNode(6));
prepend(l, newNode(7));
prepend(l, newNode(8));
prepend(l, newNode(9));
prepend(l, newNode(4));
printList(l);
printf("\n===========================================\n");
int* arr = arrOf(l);
for(int i = 0; i < 10; ++i){
printf("%d, ", arr[i]);
}
return 0;
}
当您在 main
中初始化 List l
时,您没有分配默认值。它存储在堆栈中并且没有被初始化。这意味着该值未定义且不一定为空。
在全局创建List l
时,变量存储在 bss 段中并用 null 初始化。
将 List l
的声明更改为:
List l = NULL;
这个函数
void prepend(List l, Node node){
if (l == NULL) l = node;
else{
node->next = l;
l = node;
}
}
处理在 main
中声明的指针 l
的值的副本
List l;
而且还没有初始化。
因此在函数内更改这些语句中的副本
if (l == NULL) l = node;
//...
l = node;
不影响main中声明的指针的原始值。
你至少要写得像
void prepend(List *l, Node node){
node->next = *l;
*l = node;
}
在主要部分
List l = NULL;
函数可以这样调用
prepend( &l, newNode(5) );
也就是说,指向头节点的指针必须通过引用传递给函数。
您还需要为列表和数组释放所有动态分配的内存。
我已经编写了一些代码来创建一个单链表,然后在 C 中将其转换为动态数组。
如果我只为列表 header 创建一个全局变量,代码就可以正常工作。但是,如果我想在我的主函数中创建列表,我总是会遇到分段错误。
只要我创建一个全局列表变量并将该列表作为参数从所有函数中删除,下面的代码就可以正常工作。
如果我想将列表作为参数传递给函数并因此能够创建多个列表,谁能告诉我为什么这不起作用?
#include <stdlib.h>
#include <stdio.h>
typedef struct NodeStruct* Node;
typedef struct NodeStruct {
Node next;
int val;
} NodeStruct;
typedef Node List;
Node newNode(int x){
Node n = (Node)malloc(sizeof(NodeStruct));
if(n!=NULL){
n->val = x;
n->next = NULL;
return n;
}
else{
printf("ERROR: Could not allocate memory!\n");
}
exit(1);
}
void prepend(List l, Node node){
if (l == NULL) l = node;
else{
node->next = l;
l = node;
}
}
void printList(List l){
if(l!=NULL){
Node n = l;
while(n->next != NULL){
printf("%d, ", n->val);
n = n->next;
}
printf("%d\n", n->val);
}
else{
printf("ERROR: List empty!\n");
}
}
/*=============================*/
int* arrOf(List l){
if(l==NULL){
printf("ERROR: List empty\n");
exit(1);
}
int size = 0;
Node n = l;
while(n!=NULL){
size++;
n = n->next;
}
int* arr = (int*)malloc((size+1)*sizeof(int));
n = l;
int i = 0;
arr[i++] = size;
while(n != NULL){
arr[i++] = n->val;
n = n->next;
}
printf("Returning Array\n");
return arr;
}
int main(int argc, char *argv[]){
List l;
prepend(l, newNode(5));
prepend(l, newNode(6));
prepend(l, newNode(7));
prepend(l, newNode(8));
prepend(l, newNode(9));
prepend(l, newNode(4));
printList(l);
printf("\n===========================================\n");
int* arr = arrOf(l);
for(int i = 0; i < 10; ++i){
printf("%d, ", arr[i]);
}
return 0;
}
当您在 main
中初始化 List l
时,您没有分配默认值。它存储在堆栈中并且没有被初始化。这意味着该值未定义且不一定为空。
在全局创建List l
时,变量存储在 bss 段中并用 null 初始化。
将 List l
的声明更改为:
List l = NULL;
这个函数
void prepend(List l, Node node){
if (l == NULL) l = node;
else{
node->next = l;
l = node;
}
}
处理在 main
中声明的指针l
的值的副本
List l;
而且还没有初始化。
因此在函数内更改这些语句中的副本
if (l == NULL) l = node;
//...
l = node;
不影响main中声明的指针的原始值。
你至少要写得像
void prepend(List *l, Node node){
node->next = *l;
*l = node;
}
在主要部分
List l = NULL;
函数可以这样调用
prepend( &l, newNode(5) );
也就是说,指向头节点的指针必须通过引用传递给函数。
您还需要为列表和数组释放所有动态分配的内存。