简体   繁体   English

使用C中的字符指针为结构分配内存

[英]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);

as you can read here you need to allocate the char * separately : 你可以在这里阅读你需要分别分配char *

mystruct* structptr = malloc(sizeof(mystruct));
structptr->word = malloc(sizeof(WhatSizeYouWant));

When you allocate memory for structptr , the pointer word in the struct has no valid memory to point. 当您分配内存structptr ,指针wordstruct没有有效的记忆点。 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个选项:

  1. Like what you said, declare it as char word[50] instead of `char *' 就像你说的那样,把它声明为char word[50]而不是`char *'
  2. 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 word as well. malloc / calloc(我个人更喜欢calloc,省去了zeromemory的麻烦,这是非常重要的..)外部结构,然后malloc / calloc也是内部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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM