我可以指定一个类似 Java 的“构造函数”吗?在c中?
我想在 c 中“构造”(读:malloc 和 memset)我的哈希表。为此,我创建了一个函数,如下所示:
int maketable(struct hash_entry **table, int size){
table = (struct hash_entry **)malloc(size*sizeof(struct hash_entry *));
int i = 0;
for (; i<size; i++) {
memset(table[i], '\0', sizeof(struct hash_entry *));
}
return 0;
}
假设该表已在
struct hash_entry **table[size]
输入此代码之前声明,那么当我从 maketable 返回时,我不会丢失任何内容,对吧?
编辑: 将 table
传递给 maketable()
是否可以确保只要我更改 table
指向的数据,更改就会被保留?
编辑二: 我正在尝试分配指向 hash_entries 的指针的指针数组
I want to 'construct' (read: malloc and memset) my hashtable in c. To do this, I created a function as follows:
int maketable(struct hash_entry **table, int size){
table = (struct hash_entry **)malloc(size*sizeof(struct hash_entry *));
int i = 0;
for (; i<size; i++) {
memset(table[i], '\0', sizeof(struct hash_entry *));
}
return 0;
}
Given that table will have been declared as
struct hash_entry **table[size]
before entering this code, I won't lose anything when I return from maketable, right?
EDIT:
Does passing table
into maketable()
ensure that as long as I change the data that table
points to, the changes will be preserved?
EDIT II:
I am trying to allocate an array of pointers to pointers to hash_entries
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码正在分配给本地
table
变量,调用者不受影响。这会导致内存泄漏。在函数之外,您已将 table 声明为指向 struct hash_entry 指针的指针数组 - 我猜您只是想要一个指向 struct hash_entry 的指针数组。
如果您实际上将
table
声明为数组,则无需 malloc 该空间。您只需要一个循环将其中的每个元素设置为 NULL(不要将每个元素设置为零)。如果目标是分配整个表,这可能就是您正在寻找的:
调用它
像我宁愿让它返回表一样
:如果声明
struct hash_entry **table[size]< /code> 确实是您想要的,您需要告诉我们您的 maketable() 函数实际上应该做什么(例如,您想要动态分配的“数组”作为该表中的元素之一吗?
Your code is assigning to the local
table
variable, the caller is not affected. This results in a memory leak.Outside the function, you have declared table as an array of pointer to pointers of struct hash_entry - I'm guessing you rather just want an array of pointers to struct hash entries.
If you actually declare
table
as an array, there's no need to malloc that space. You just need a loop to set every element in it to NULL (Don't memset each element to zeroes).If the goal is to allocate the entire table, this is perhps what you're looking for:
call it like
I'd rather make that return the table like so:
if the declaration
struct hash_entry **table[size]
is really what you want, you need to tell us what your maketable() function actually is supposed to do (e.g. do you want to a dynamically allocated 'array' as one of the elements in that table ?您需要将
malloc
的结果分配给* table
- 否则它将在函数外部不可见。此外,使用它的典型方法是声明一个指向哈希表的指针并将该指针的地址传递给函数。
You will need to assign the result of
malloc
to* table
- otherwise it won't be visible outside the function.Also, the typical way of using this is to declare a pointer to the hashtable and pass the address of that pointer into the function.
不,你们的类型不匹配。
您是否正在尝试分配一个哈希条目表(即
table[i]
的类型是struct hash_entry
),一个指针表hash_entries(即table[i]
的类型是struct hash_entry *
),还是其他什么?根据您的代码的读取方式,我假设第一种情况,但如果这是错误的,请告诉我。假设您要动态分配
struct hash_entry
的表,则调用者中对该表的声明应该是函数应调用为
并定义 有
几点需要指出。首先,不要强制转换
malloc()
的结果。从 C89 开始,您不需要这样做,并且如果您忘记包含 stdlib.h 或范围内没有malloc()
的原型,则强制转换将抑制诊断。其次,您可以对对象而不是类型使用sizeof
运算符。这可以帮助减少一些维护麻烦(即,如果您更改参数列表中的table
类型,则不必随之更改sizeof
调用) 。最后,请注意表的地址被传递给函数;由于我们正在尝试写入指针值,因此我们必须传递一个指向该指针的指针。
如果您试图创建一个指向
struct hash_entry
的指针表,代码基本上是相同的,只是一个额外的间接层:您在调用者中声明的表应该是
函数应该是< em>称为为
和定义为
编辑
maketable
示例中存在错误;在应用下标之前需要取消引用table
,即(*table)[i]
。我们将下标应用于table
指向的内容,而不是表指针本身。抱歉造成任何混乱。
No. Your types don't match up.
Are you trying to allocate a table of hash entries (i.e., the type of
table[i]
isstruct hash_entry
), a table of pointers to hash_entries (i.e., the type oftable[i]
isstruct hash_entry *
), or something else? Based on how your code reads, I'm assuming the first case, but let me know if that's wrong.Assuming you're dynamically allocating a table of
struct hash_entry
, your declaration of the table in the caller should bethe function should be called as
and defined as
A couple of things to point out. First of all, don't cast the result of
malloc()
. As of C89, you don't need to, and the cast will supress a diagnostic if you forget to include stdlib.h or otherwise don't have a prototype formalloc()
in scope. Secondly, you can use thesizeof
operator on objects instead of types. This can help reduce some maintenance headaches (i.e., if you change the type oftable
in the parameter list, you don't have to change thesizeof
calls along with it).Finally, note that the address of table is being passed to the function; since we're trying to write to a pointer value, we must pass a pointer to that pointer.
If you were attempting to create a table of pointers to
struct hash_entry
, the code is mostly the same, just an extra level of indirection:your declaration of the table in the caller should be
the function should be called as
and defined as
EDIT There was a bug in the
maketable
examples;table
needs to be dereferenced before applying the subscript, i.e.,(*table)[i]
. We're applying the subscript to whattable
points to, not the table pointer itself.Sorry for any confusion.