【发布时间】:2014-11-17 13:29:33
【问题描述】:
我有一个 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)); -
@T.V.我只是添加 b = malloc(sizeof(struct bst)); ?
-
@T.V.你误解了
*b。*b不是指针。