道歉的真正糟糕的問題 - 我真的不知道該怎麼說。 我正在執行一段代碼,我試圖使用動態數組。這是段錯誤 - 荷蘭國際集團在這一行: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