1
我有一個BST結構的函數中動態數組:分配結構
struct bst {
int *data;
int max;
};
而且我有一個函數來創建最初是一個BST:
struct bst *create_bst(int max) {
struct bst *b;
b->data = malloc(pow(2, max) * sizeof(int));
return b;
}
但我在得到錯誤我將內存分配給數據的行。
我做錯了什麼?
你得到一個錯誤,因爲你沒有爲'b'分配內存,因爲你將它定義爲一個指向'struct bst'的指針 –
當malloc失敗時,max的值是多少? –
'struct bst * b;' - >'struct bst * b = malloc(sizeof(* b));' – BLUEPIXY