为什么这段代码不分配 C 内存?
更新的问题在这里
我我正在用 C 语言制作一个哈希表。这就是我所做的。 我认为我走在正确的道路上,但是当我尝试
main.c
HashTablePtr hash;
hash = createHashTable(10);
insert(hash, "hello");
insert(hash, "world");
HashTable.c
HashTablePtr createHashTable(unsigned int capacity){
HashTablePtr hash;
hash = (HashTablePtr) malloc(sizeof(HashTablePtr));
hash->size = 0;
hash->capacity = capacity;
ListPtr mylist = (ListPtr)calloc(capacity, sizeof(ListPtr)); /* WHY IT DOESN'T ALLOCATE MEMORY FOR mylist HERE?? */
mylist->head = NULL;
mylist->size = 0;
mylist->tail = NULL;
hash->list = mylist;
return hash;
ListPtr 是一个 LinkedList ptr
List.h
typedef struct list List;
typedef struct list * ListPtr;
struct list {
int size;
NodePtr head;
NodePtr tail;
};
...
...
HashTable.h
typedef struct hashtable * HashTablePtr;
typedef struct hashtable HashTable;
struct hashtable {
unsigned int capacity;
unsigned int size;
ListPtr *list;
unsigned int (*makeHash)(unsigned int, void *);
};
...
...
当我运行调试器时,我看到没有内存分配给 myList。 在上面的示例中,我尝试将其设为包含 10 个列表的数组。
请帮我解决这个问题。
我不是 C 方面的专家,如果这有帮助的话。
Updated question is here
Memory allocation problem in HashTable
I'm working on making a HashTable in C. This is what I've done. I think I'm going on a right path but when I'm trying to
main.c
HashTablePtr hash;
hash = createHashTable(10);
insert(hash, "hello");
insert(hash, "world");
HashTable.c
HashTablePtr createHashTable(unsigned int capacity){
HashTablePtr hash;
hash = (HashTablePtr) malloc(sizeof(HashTablePtr));
hash->size = 0;
hash->capacity = capacity;
ListPtr mylist = (ListPtr)calloc(capacity, sizeof(ListPtr)); /* WHY IT DOESN'T ALLOCATE MEMORY FOR mylist HERE?? */
mylist->head = NULL;
mylist->size = 0;
mylist->tail = NULL;
hash->list = mylist;
return hash;
ListPtr is a LinkedList ptr
List.h
typedef struct list List;
typedef struct list * ListPtr;
struct list {
int size;
NodePtr head;
NodePtr tail;
};
...
...
HashTable.h
typedef struct hashtable * HashTablePtr;
typedef struct hashtable HashTable;
struct hashtable {
unsigned int capacity;
unsigned int size;
ListPtr *list;
unsigned int (*makeHash)(unsigned int, void *);
};
...
...
When I run my debugger, I see no memory being allocated to myList. In above example, my attempt is to make it an array of 10 lists.
Please help me to solve this.
I'm not that expert in C, if that helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
应该
should be
我认为这里存在很多问题。 您还没有包含您遇到的错误,所以我将列出几个:
I think there are a whole host of problems here. You haven't included the error you getting so, I will list a couple:
就我个人而言,我不太喜欢使用 typedef,尤其是当您是初学者时。 我认为这可能是让您感到困惑的部分原因。 您最好避免以下情况:
使用许多 typedef 将使您的代码更难以阅读,因为您还需要不断查找它们所引用的内容。
主要问题是您为哈希表/列表指针的大小分配内存,而不是为其相关结构的大小分配内存。 我认为下面的代码很好地说明了这一点。 您还需要检查您的分配是否有效。 如malloc、calloc、realloc。 等失败,他们返回 NULL。 如果发生这种情况并且您不检查这种情况,您将收到段错误并且您的程序将崩溃。
还要遵循 c99 标准,并将所有变量声明放在函数的开头。
c99 标准
malloc 手册页
另外请记住在释放哈希表之前释放列表:
Personally I am not a huge fan of using typedefs, especially when you are a beginner. I think that may be partially what is confusing you. You are better avoiding things like:
Using to many typedefs will make your code harder to read since you will need to constantly look up what they are referring too.
The main problem is that you were allocating the memory for the size of a hashtable/list pointer and not for the size of their respected structures. I think the code below shows this well. You will also want to check if you allocations worked. If malloc, calloc, realloc. etc. fail they return NULL. If this happens and you do not check for this case you will get a segfault error and your program will crash.
Also follow the c99 standard, and put all of your variable declarations at the start of the function.
c99 std
malloc manpage
Also remember to free you list before you free your hashtable:
您正在分配 ListPtr 的连续块,但实际上您想为所有结构分配空间,而不仅仅是指向这些结构的指针(ListPtr):
我同意 gman 关于不隐藏指针的评论。 当用 C 编码时,我从不将
List *
类型定义为ListPtr
。 它使代码更难理解。You are allocating a contigous block of ListPtr's, but you actually want to allocate space for the all the structures rather than just pointers (ListPtr) to those structures:
I agree with gman's comment about not hiding pointers. When coding in C, I never typdef a
List *
as aListPtr
. It makes the code harder to understand.