道歉的真正糟糕的问题 - 我真的不知道该怎么说。 我正在执行一段代码,我试图使用动态数组。这是段错误 - 荷兰国际集团在这一行:Segfault使用动态数组
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的方式,但在线搜索并尝试了几个不同的选项,仍然收到段错误。有人能够提供一些见解吗?
谢谢你满口
勿投'malloc'。 – 2015-03-25 11:37:47
你在'stack_new()'中为's'分配了什么内存? – 2015-03-25 11:38:13
和一个为什么你不应该''malloc'返回值的链接:http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – juhist 2015-03-25 11:39:05