C语言 在 C 中为具有字符指针的结构分配内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18248047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Allocate memory for a struct with a character pointer in C
提问by H_squared
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* wordis a string of variable length, and malloc was not allocating enough memory for it. In fact it was only allocating 20 Bytesfor the whole struct. Is there a way around this issue, without turning the char*into sth like char word[50]?
由于这char* word是一个可变长度的字符串,代码给出了很多内存错误,并且 malloc 没有为其分配足够的内存。事实上,它只是20 Bytes为整体分配struct。有没有办法解决这个问题,而不会char*变成 sth 之类的char word[50]?
回答by Devolus
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. 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. You must still free the memory yourself though.
您只为结构本身分配内存。这包括指向 char 的指针,它在 32 位系统上只有 4 个字节,因为它是结构的一部分。它不包括未知长度字符串的内存,所以如果你想要一个字符串,你也必须手动分配内存。如果您只是复制字符串,则可以使用strdup()which 分配和复制字符串。不过,您仍然必须自己释放内存。
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. If this is applicable or not, depends on your design though.
如果不想自己为字符串分配内存,唯一的选择是在结构中声明一个固定长度的数组。然后它将成为结构的一部分,sizeof(mystruct)并将包含它。如果这适用与否,则取决于您的设计。
回答by HymanCColeman
Add a second mallocfor whatever length (N) you need for word
malloc为您需要的任何长度 (N)添加一秒word
mystruct* structptr = malloc(sizeof(mystruct));
structptr->word = malloc(sizeof(char) * N);
回答by No Idea For Name
回答by Yu Hao
When you allocate memory for structptr, the pointer wordin the structhas no valid memory to point. So you either malloca piece of memory for word, too, or make wordpoint to another character.
当您分配内存structptr,指针word在struct没有有效的内存点。所以你要么malloc为 留下一段记忆word,要么word指向另一个角色。
回答by Angela Yan
malloc the outer struct will only allocate 1 byte memory pointed by *wordsince it is a 'char *' type. If you want to allocate more than 1 byte of memory pointed by word, there are 2 options:
malloc 外部结构将只分配 1 个字节指向的内存,*word因为它是一个 'char *' 类型。如果要分配超过 1 个字节的内存word,有 2 个选项:
- Like what you said, declare it as
char word[50]instead of `char *' - malloc/calloc (I personally prefer calloc, saving you the trouble of zeromemory, which is a very important..) the outer struct, then malloc/calloc the inner
wordas well. Remember to callfreetwice as well in this case.
- 就像你说的那样,将它声明为
char word[50]而不是 `char *' - malloc/calloc(我个人更喜欢 calloc,省去了零内存的麻烦,这是一个非常重要的..)外部结构,然后 malloc/calloc 内部
word也是如此。记住free在这种情况下也调用两次。
回答by Abhishek
Use word=malloc(128);
用 word=malloc(128);
this will allocate 128 bytes to your varible word,
这将为您的变量字分配 128 个字节,

