c - 不使用全局列表时 C 列表的分段错误
问题描述
我编写了一些代码来创建一个单链表,然后将其转换为 C 中的动态数组。
如果我只为列表标题创建一个全局变量,代码就可以正常工作。但是,如果我想在我的 main 函数中创建列表,我总是会遇到分段错误。
只要我创建一个全局列表变量并将列表作为参数从所有函数中删除,下面的代码就可以正常工作。
如果我想将列表作为参数传递给函数并因此能够创建多个列表,谁能告诉我为什么这不起作用?
#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;
}
解决方案
当您在 中初始化List l
时main
,您没有分配默认值。它存储在堆栈中并且未初始化。这意味着该值未定义且不一定为空。
在全局创建List l
时,变量存储在 bss 段中并初始化为 null。
将您的声明更改List l
为:
List l = NULL;
推荐阅读
- python - 为什么 tf.gradients 不适用于整数常量?
- javascript - 添加脚本标签并按顺序加载脚本
- c# - 从 WPF 应用程序将控制台附加到 (java) 进程
- awk - 当 2 个不同的列匹配时,在两个文件中打印行
- java - Bean 名称“searchPhrase”的 BindingResult 和普通目标对象都不能用作请求属性
- javascript - 将活动类添加到 jQuery 手风琴
- docker - Dockerfile 字符串插值工作
- javascript - 您可以将索引号数组传递给数组推送方法,而不是专门指定推送中的每个索引吗
- javascript - d3js v5 带异步函数的循环打包
- .net - Nupkg 未从 gitinfo.txt 读取 baseversion