[英]Allocate memory for a struct with a character pointer in C
I was struggling to fix a code today, then I come across something similar to: 我今天很难修复代码,然后我遇到类似的东西:
typedef struct {
int a;
int b;
int c;
int d;
char* word;
} mystruct;
int main(int argc, char **argv){
mystruct* structptr = malloc(sizeof(mystruct));
if (structptr==NULL) {
printf("ERROR!")
...
}
...
free(structptr);
return 0;
}
the code was giving lots of memory errors due to the fact, that char* word
is a string of variable length, and malloc was not allocating enough memory for it. 由于
char* word
是一个可变长度的字符串,而且malloc没有为它分配足够的内存,因此代码提供了大量的内存错误。 In fact it was only allocating 20 Bytes
for the whole struct
. 实际上它只为整个
struct
分配了20 Bytes
。 Is there a way around this issue, without turning the char*
into sth like char word[50]
? 有没有办法绕过这个问题,而不是将
char*
变成像char word[50]
?
You are allocating only memory for the structure itself. 您只为结构本身分配内存。 This includes the pointer to char, which is only 4 bytes on 32bit system, because it is part of the structure.
这包括指向char的指针,它在32位系统上只有4个字节,因为它是结构的一部分。 It does NOT include memory for an unknown length of string, so if you want to have a string, you must manually allocate memory for that as well.
它不包含未知长度的字符串的内存,因此如果您想要一个字符串,您还必须手动为其分配内存。 If you are just copying a string, you can use
strdup()
which allocates and copies the string. 如果您只是复制字符串,则可以使用
strdup()
来分配和复制字符串。 You must still free the memory yourself though. 你仍然必须自己释放内存。
mystruct* structptr = malloc(sizeof(mystruct));
structptr->word = malloc(mystringlength+1);
....
free(structptr->word);
free(structptr);
If you don't want to allocate memory for the string yourself, your only choice is to declare a fixed length array in your struct. 如果您不想自己为字符串分配内存,那么您唯一的选择是在结构中声明一个固定长度的数组。 Then it will be part of the structure, and
sizeof(mystruct)
will include it. 然后它将成为结构的一部分,而
sizeof(mystruct)
将包含它。 If this is applicable or not, depends on your design though. 如果这适用与否,取决于您的设计。
Add a second malloc
for whatever length (N) you need for word
为
word
所需的长度(N)添加第二个malloc
mystruct* structptr = malloc(sizeof(mystruct));
structptr->word = malloc(sizeof(char) * N);
When you allocate memory for structptr
, the pointer word
in the struct
has no valid memory to point. 当您分配内存
structptr
,指针word
的struct
没有有效的记忆点。 So you either malloc
a piece of memory for word
, too, or make word
point to another character. 让你无论是
malloc
一块内存的word
,也还是让word
点到另一个角色。
malloc the outer struct will only allocate 1 byte memory pointed by *word
since it is a 'char *' type. malloc外部struct只分配
*word
指向的1字节内存,因为它是'char *'类型。 If you want to allocate more than 1 byte of memory pointed by word
, there are 2 options: 如果要分配超过1个字节的内存
word
,有2个选项:
char word[50]
instead of `char *' char word[50]
而不是`char *' word
as well. word
。 Remember to call free
twice as well in this case. free
通话。 Use word=malloc(128);
使用
word=malloc(128);
this will allocate 128 bytes to your varible word, 这将为您的可变单词分配128个字节,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.