使用动态数组的段错误
Segfault using dynamic array
对于这个非常糟糕的问题,我深表歉意 - 我真的不确定该如何措辞。
我正在执行一段代码,我正在尝试使用动态数组。在这一行出现段错误:
void myFunction(....) {
// other code up here
Stack *s = stack_new(); //segfault here
}
我的结构的相关头文件是:
typedef struct {
void **A;
int size;
int top; // the index of the current top of the stack
} Stack;
函数 stack_new() 是:
Stack
*stack_new() {
Stack *s;
s->size = 1;
s->top = -1;
s->A = (void **)malloc(s->size);
return s;
}
我想我已经包含了所有相关的内容,但如果您需要更多代码,请告诉我。
我认为问题出在我使用 malloc 的方式上,但是已经在线搜索并尝试了几个不同的选项,但仍然出现段错误。有人能提供一些见解吗?
多谢
这是你的问题:
Stack *s;
s->size = 1;
您实际上并没有分配 Stack
。 s
未初始化并指向内存中的任意位置。 s->size
显然会失败。
尝试:
Stack *s = malloc(sizeof(*s));
if (s == NULL)
{
fprintf(stderr, "Memory allocation error\n");
exit(1);
}
s->size = 1;
注意:您还应检查 s->A
是否为 NULL
。如果是这样,return 错误代码(例如 NULL
)并且在此之前记得释放您分配的 Stack
,或者打印错误消息并退出程序。如果您退出该程序,操作系统将回收所有使用的内存,因此无需显式执行。
另注:做的时候
s->size = 1;
s->top = -1;
s->A = (void **)malloc(s->size);
...您分配了 1 字节的内存,即使您应该分配 sizeof(void*)
字节的内存。尝试做
s->A = (void **)malloc(s->size*sizeof(void*));
相反。
这是你的第一个问题:
Stack *s;
s->size = 1;
此时您实际期望 s
的值是多少?它可以是字面上的 anything。如果结构本身尚未分配,则无法设置结构的字段。
尝试:
Stack *s = malloc(sizeof(*s));
if(!s){
//... error checking / exiting ..
}
然后是您正在做的所有其他事情。
您正在访问一个未初始化的指针!
Stack
*stack_new() {
Stack *s = std::nullptr; // initialize this pointer with nullptr
// and then you will see later (one line
// beyond) that you will try to access a
// null pointer
s->size = 1; // the problem occurs here!!
// you are accessing a pointer, for which has never
// been allocated any memory
s->top = -1;
s->A = (void **)malloc(s->size);
return s;
}
您将不得不使用 "malloc" 为该指针分配一些内存。
……这两行之间缺少这样的内容,我评论说:
堆叠
*stack_new() {
Stack *s = (Stack*)malloc(sizeof(Stack));
s->size = 1;
s->top = -1;
s->A = (void **)malloc(s->size);
return s;
}
对于这个非常糟糕的问题,我深表歉意 - 我真的不确定该如何措辞。 我正在执行一段代码,我正在尝试使用动态数组。在这一行出现段错误:
void myFunction(....) {
// other code up here
Stack *s = stack_new(); //segfault here
}
我的结构的相关头文件是:
typedef struct {
void **A;
int size;
int top; // the index of the current top of the stack
} Stack;
函数 stack_new() 是:
Stack
*stack_new() {
Stack *s;
s->size = 1;
s->top = -1;
s->A = (void **)malloc(s->size);
return s;
}
我想我已经包含了所有相关的内容,但如果您需要更多代码,请告诉我。
我认为问题出在我使用 malloc 的方式上,但是已经在线搜索并尝试了几个不同的选项,但仍然出现段错误。有人能提供一些见解吗?
多谢
这是你的问题:
Stack *s;
s->size = 1;
您实际上并没有分配 Stack
。 s
未初始化并指向内存中的任意位置。 s->size
显然会失败。
尝试:
Stack *s = malloc(sizeof(*s));
if (s == NULL)
{
fprintf(stderr, "Memory allocation error\n");
exit(1);
}
s->size = 1;
注意:您还应检查 s->A
是否为 NULL
。如果是这样,return 错误代码(例如 NULL
)并且在此之前记得释放您分配的 Stack
,或者打印错误消息并退出程序。如果您退出该程序,操作系统将回收所有使用的内存,因此无需显式执行。
另注:做的时候
s->size = 1;
s->top = -1;
s->A = (void **)malloc(s->size);
...您分配了 1 字节的内存,即使您应该分配 sizeof(void*)
字节的内存。尝试做
s->A = (void **)malloc(s->size*sizeof(void*));
相反。
这是你的第一个问题:
Stack *s;
s->size = 1;
此时您实际期望 s
的值是多少?它可以是字面上的 anything。如果结构本身尚未分配,则无法设置结构的字段。
尝试:
Stack *s = malloc(sizeof(*s));
if(!s){
//... error checking / exiting ..
}
然后是您正在做的所有其他事情。
您正在访问一个未初始化的指针!
Stack
*stack_new() {
Stack *s = std::nullptr; // initialize this pointer with nullptr
// and then you will see later (one line
// beyond) that you will try to access a
// null pointer
s->size = 1; // the problem occurs here!!
// you are accessing a pointer, for which has never
// been allocated any memory
s->top = -1;
s->A = (void **)malloc(s->size);
return s;
}
您将不得不使用 "malloc" 为该指针分配一些内存。 ……这两行之间缺少这样的内容,我评论说:
堆叠
*stack_new() {
Stack *s = (Stack*)malloc(sizeof(Stack));
s->size = 1;
s->top = -1;
s->A = (void **)malloc(s->size);
return s;
}