[英]Initializing struct seg fault
I'm initializing a struct which represents a hash table. 我正在初始化一个代表哈希表的结构。 I'm attempting to make the frequencies = 0 and the keys = null, but I'm getting a seg fault and not too sure why.
我试图使频率= 0,键=空,但我遇到段错误,但不太确定为什么。 Here is my code:
这是我的代码:
htable htable_new(int capacity) {
int i;
htable result = emalloc(sizeof *result);
result->capacity = capacity;
result->frequencies = emalloc(capacity * sizeof result->frequencies[0]);
result->keys = emalloc(capacity * sizeof result->keys[0]);
for (i = 0; i < capacity; i++) {
result->frequencies[i] = 0;
}
for (i = 0; i < capacity; i++) {
result->keys[i] = NULL;
}
return result;
}
and here is the struct which I'm initializing: 这是我正在初始化的结构:
struct htablerec {
int capacity;
int num_keys;
int *frequencies;
char **keys;
};
I don't see what's wrong as I'm trying to initialize all the int's i've allocated to 0, and all the char pointers to NULL. 我尝试初始化分配给0的所有int以及所有指向NULL的char指针时,都没有看到什么问题。 Is this wrong?
错了吗 Should I be doing this some other way?
我应该以其他方式这样做吗? Thanks for your help!
谢谢你的帮助!
Try the following (notice pointers near result and return type): 尝试以下操作(注意结果和返回类型附近的指针):
htable *htable_new(int capacity) {
int i;
htable *result = emalloc(sizeof *result);
result->capacity = capacity;
result->frequencies = emalloc(capacity * sizeof result->frequencies[0]);
result->keys = emalloc(capacity * sizeof result->keys[0]);
for (i = 0; i < capacity; i++) {
result->frequencies[i] = 0;
}
for (i = 0; i < capacity; i++) {
result->keys[i] = NULL;
}
return *result;
}
Or, as far as your structure is small, you may want to return it by value: 或者,就您的结构而言,您可能希望按值返回它:
htable htable_new(int capacity) {
int i;
htable result = { 0 };
result.capacity = capacity;
result.frequencies = emalloc(capacity * sizeof result->frequencies[0]);
result.keys = emalloc(capacity * sizeof result->keys[0]);
for (i = 0; i < capacity; i++) {
result.frequencies[i] = 0;
}
for (i = 0; i < capacity; i++) {
result.keys[i] = NULL;
}
return result;
}
I changed result
to be an actual struct, rather than pointer to it. 我将
result
更改为实际的结构,而不是指向它的指针。 Because of that, it's allocated on stack, fields are initialized as usual and then I return it as is. 因此,它分配在堆栈上,像往常一样初始化字段,然后按原样返回它。 That means, all struct will be copied to the new place.
这意味着,所有结构都将被复制到新位置。
Remember that only field of htable
are copied; 请记住,仅复制
htable
字段; capacity
and keys
are copied as pointers. capacity
和keys
被复制为指针。 That means that if you do htable a = new_htable(10); htable b = a;
这意味着如果您执行
htable a = new_htable(10); htable b = a;
htable a = new_htable(10); htable b = a;
then a.keys == b.keys
(they point to the same array). 然后
a.keys == b.keys
(它们指向同一数组)。
I don't know, how your code compiled at all. 我不知道您的代码是如何编译的。 By the way, you forgot to initialize
num_keys
field and the name of struct you defined doesn't match the name of return type. 顺便说一句,您忘记初始化
num_keys
字段,并且您定义的结构名称与返回类型的名称不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.