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