正确的内存分配
我有以下结构:
typedef struct bucket {
char *key;
ENTRY *data;
struct bucket *next;
} bucket;
typedef struct {
size_t size;
bucket **table;
} hash_table;
但我不知道如何为此分配内存。 我尝试过:
hash_table* ht = malloc(sizeof(hash_table)*101);
为了创建 101 个条目的哈希表,但它不起作用! 谁能帮我? 我真的很感激!
I have the following construction:
typedef struct bucket {
char *key;
ENTRY *data;
struct bucket *next;
} bucket;
typedef struct {
size_t size;
bucket **table;
} hash_table;
But I have no idea how to allocate memory for that. I tried:
hash_table* ht = malloc(sizeof(hash_table)*101);
in order to create a hashtable for 101 entries but it din't work! Can anyone help me? I would really appreciate it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
预先分配所有 101 个(或任意数量)存储桶是没有意义的,通常在向表中插入新数据时一次分配一个存储桶。
预分配哈希数组确实有意义,该数组具有固定大小,但这是一个存储桶指针数组,而不是存储桶数组,因此有些的答案是错误的。
你会有这样的事情,创建一个空的哈希表,带有固定大小的存储桶数组:
此代码:
sizeof
,但不在类型上,因此没有括号malloc()
的返回值,因为这在 C 中从来都不是一个好主意操作需要第二个函数来执行实际的哈希插入,然后需要分配一个新的存储桶,根据键计算哈希值,在哈希表的数组中选择正确的位置,然后在那里插入新条目。
It doesn't make sense to allocate all 101 (or however many) buckets upfront, you'd typically allocate them one at a time, when inserting new data into the table.
It does make sense to pre-allocate the hash array, which will have a fixed size, but that is an array of bucket pointers, not an array of buckets, so some of the answers are wrong.
You'd have something like this, to create a an empty hash table, with a fixed-size bucket array:
This code:
sizeof
whenever possible, but not on types, so no parenthesismalloc()
, since that is never a good idea in CA second function would be needed to do an actual hash insert, which will then need to allocate a new bucket, compute the hash value from the key, pick the proper location in the hash table's array, and insert the new entry there.
不完全的。 假设这是 C,您可能想要创建一个函数:
您可能需要该结构中的一些其他字段。
如果你想变得狡猾,并且从不重新分配存储桶,你可以这样做:
编辑:我将我的存储桶*表固定为存储桶**
编辑2:我已经摆脱了memsets并添加了malloc的错误检查。
Not quite. Assuming this is C, you probably want to make a function:
You might need some other fields in that struct.
If you wanted to be tricky, and never realloc the bucket, you can do this:
EDIT: I fixed my bucket* table's to bucket**
EDIT2: I've gotten rid of the memsets and added error checking for malloc.
hash_table
始终只有sizeof(hash_table)
字节大。table
元素是一个指向bucket
元素的指针数组的指针。 所以你需要这样的东西:但我怀疑可能有一些随之而来的初始化方法,然后你可以做这样的事情:
不管怎样,我对 C 有点生疏,所以把这个与谷物一起考虑盐。
The
hash_table
will always be onlysizeof(hash_table)
bytes big. Thetable
element is a pointer to an array of poiinters tobucket
elements. So you'd need something like this:But I suspect that there may be some initialization method that comes with that, and you could then do something like this:
Anyway, I'm kind of rusty in C, so take this with a grain of salt.
你的 typedef 有一些问题。 假设您使用 MSVC。
声明此处类型的简单方法如下:
此 typedef 包括 _type {} 类型,*ptype; 格式同时声明类型和指向自定义类型的指针。 如果您在 hash_table 中看到,您可以使用 pbucket *table,它消除了代码中的额外 ***,并且可以在进行动态分配时提供帮助(帮助您清楚分配的内容等。 )。 你原来的 typedef,如果你看起来有 typedef struct bucket {} bucket;,你至少需要修改你指定 typedef 时的两个“bucket”名称之一。
如果您使用 C++ 构建设置,您还需要进行强制转换,如果使用纯 C,则可能不需要强制转换,因此您的 malloc 行将是(我进行了以下 typedef 更改);
不管怎样,这个片段应该适合你;
There's a few things worong with your typedef's. Assuming your using MSVC.
An easy way to declare the types you have here would be something like;
This typedef includes the _type {} type, *ptype; format which declares the type and the pointer to your custom type all at the same time. If you see down in hash_table, your are able to use pbucket *table, which eliminates the extra *** in your code and can help when doing dynamic allocation (help so mucah as to keep your head straight about what your allocating etc..). Your original typedef, if you look had typedef struct bucket {} bucket;, you need to at least modify one of the two "bucket" names you have there when you specify your typedef.
You also need to cast if your using C++ build settings, if using plain C you may not need the cast, so your malloc line would be (with the following typedef changes I made);
Either way, this snippet should work for you;