[英]How work with malloc and array of struct pointer
I would like to try allocating my first 10 structs.我想尝试分配我的前 10 个结构。 We have to store an array of struct pointer at the heap but I don't know what i am doing wrong
我们必须在堆中存储一个结构指针数组,但我不知道我在做什么错
my struct looks like this我的结构看起来像这样
typedef struct TEST
{
char* first_string;
char* second_string;
} test;
what i have written:我写的是:
test **string_pair;
string_pair = malloc(10 * sizeof(test *));
if(string:pair == NULL)
{
free(string_pair);
exit(4);
}
This works fine However later I am trying in an for loop to allocate my strings这很好用但是后来我尝试在for循环中分配我的字符串
string_pair[count]->first_string = malloc(10 * sizeof(char)); // note that count is currently 0
if(string_pair[count]->first_string == NULL)
{
free(string_pair[count]->first_string);
free(string_pair[count]);
exit(4);
}
And I get an segmentation fault我得到一个分段错误
Before anyone suggests we are not allowed to simply use an array of structs we have to do it the complicated way.在任何人建议我们不允许简单地使用结构数组之前,我们必须以复杂的方式来做。
Thx in advance提前谢谢
You allocated an array of pointers.您分配了一个指针数组。
But you did not allocate memory for the actual structures, and you did not initialize the pointers you allocated.但是您没有为实际结构分配 memory,也没有初始化分配的指针。
This line:这一行:
string_pair = malloc(10 * sizeof(test *));
Allocates array of 4 or 8 (depending on your architecture) byte variables that can store memory addresses.分配可以存储 memory 地址的 4 或 8 个(取决于您的架构)字节变量的数组。
It does not allocate struct TEST
.它不分配
struct TEST
。 The pointers will contain garbage (possibly invalid) addresses until you initialize them.指针将包含垃圾(可能无效)地址,直到您对其进行初始化。
You are suppose to do it by calling malloc
for struct TEST
, and placing the resulting pointer in the array.您应该通过为
struct TEST
调用malloc
并将结果指针放入数组中来实现。
Otherwise, when your code reaches this line:否则,当您的代码到达这一行时:
string_pair[count]->first_string
it will look for struct TEST
at some random address, possibly 0, and try to read its first_string
field, which will cause segmentation fault.它将在某个随机地址(可能为 0)处查找
struct TEST
,并尝试读取其first_string
字段,这将导致分段错误。
You are getting a segmentation fault because you are allocating an array of pointers but not allocating the structure itself.您遇到了分段错误,因为您分配了一个指针数组,但没有分配结构本身。
With string_pair = malloc(10 * sizeof(test *));
使用
string_pair = malloc(10 * sizeof(test *));
, you allocate string_pair
(a pointer to a pointer) to 10 pointers (pointing to a test
structure). ,您将
string_pair
(指向指针的指针)分配给 10 个指针(指向test
结构)。
Then, with string_pair[count]->first_string = malloc(10 * sizeof(char));
然后,使用
string_pair[count]->first_string = malloc(10 * sizeof(char));
, you allocate the first_string
member of string_pair[count]
to a string of 10 characters. ,您将
string_pair[count]
的first_string
成员分配给 10 个字符的字符串。 The ->
token means to access the member in string_pair[count]
. ->
标记表示访问string_pair[count]
中的成员。 However, the actual structure is not allocated.但是,实际结构并未分配。
You need to allocate string_pair[count]
, with something like string_pair[count] = malloc(sizeof(test));
您需要分配
string_pair[count]
,例如string_pair[count] = malloc(sizeof(test));
. .
I'd recommend just using a test*
for your array, and allocating it with test* string_pair = malloc(10 * sizeof(test));
我建议只为您的数组使用
test*
,并使用test* string_pair = malloc(10 * sizeof(test));
分配它. .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.