如何在C中正确地malloc for struct数组

时间:2021-12-06 19:57:11

I will read in two set of char* (or strings) using strtok, and since those two set of chars are related, (address : command\n) I decided to use a structure.

我将使用strtok读取两组char *(或字符串),并且由于这两组chars是相关的,(address:command \ n)我决定使用一个结构。

struct line* array = (struct line*)malloc(sizeof(file) * sizeof(struct line*));

This line mallocing space for the function gives me a segmentation fault and was wondering if you can tell me a proper way to malloc space for it. For context, here is the rest of my code:

这个函数的mallocing空间给了我一个分段错误,并想知道你是否可以告诉我一个正确的malloc空间方式。对于上下文,这是我的其余代码:

struct line
{
    char* addr;
    char* inst;
};
while loop{
    x = strtok(line,": ");
    y = strtok(NULL,"\n");
    strcpy(array[i].addr,x); //assume that x and y are always 3characters
    strcpy(array[i].inst,++y);
    i++;
}

2 个解决方案

#1


15  

Allocating works the same for all types. If you need to allocate an array of line structs, you do that with:

所有类型的分配都是相同的。如果需要分配一个行结构数组,可以使用:

struct line* array = malloc(number_of_elements * sizeof(struct line));

In your code, you were allocating an array that had the appropriate size for line pointers, not for line structs. Also note that there is no reason to cast the return value of malloc().

在您的代码中,您分配的数组具有适合行指针的大小,而不是行结构。另请注意,没有理由强制转换malloc()的返回值。

Note that's it's better style to use:

请注意,它的使用方式更好:

sizeof(*array)

instead of:

代替:

sizeof(struct line)

The reason for this is that the allocation will still work as intended in case you change the type of array. In this case this is unlikely, but it's just a general thing worth getting used to.

这样做的原因是,如果您更改数组类型,分配仍将按预期工作。在这种情况下,这不太可能,但这只是一个值得习惯的一般事情。

Also note that it's possible to avoid having to repeat the word struct over and over again, by typedefing the struct:

还要注意,通过typedefing结构,可以避免不得不一遍又一遍地重复单词struct:

typedef struct line
{
    char* addr;
    char* inst;
} line;

You can then just do:

你可以这样做:

line* array = malloc(number_of_elements * sizeof(*array));

Of course don't forget to also allocate memory for array.addr and array.inst.

当然不要忘记为array.addr和array.inst分配内存。

#2


3  

For what you have described, You do not need to allocate memory for your struct, rather, you need to allocate memory for the members char *addr;, and char *inst;. If you want to have a single copy of that structure, the first section of code illustrates how to initialize, and assign values. If you want an array, the second code example illustrates the differences.

对于您所描述的内容,您不需要为结构分配内存,而是需要为成员char * addr;和char * inst;分配内存。如果您希望拥有该结构的单个副本,则代码的第一部分将说明如何初始化和分配值。如果你想要一个数组,第二个代码示例说明了这些差异。

This illustrates how to allocate memory for the members of a single struct line:

这说明了如何为单个结构行的成员分配内存:

typedef struct
{
    char* addr;
    char* inst;
}LINE;

LINE line;  

int main(void)
{   

    strcpy(line.addr, "anystring"); //will fail
    line.addr = malloc(80);
    line.inst = malloc(80);
    strcpy(line.addr, "someString");//success;
    strcpy(line.inst, "someOtherString");//success;

}

For array of struct line...

对于struct line的数组......

typedef struct
{
    char* addr;
    char* inst;
}LINE;  //same struct definition

LINE line[10]; //but create an array of line here.

int main(void)
{   
    int i;

    for(i=0;i<10;i++)
    {
      line[i].addr = malloc(80);
      line[i].inst = malloc(80);
    }

    for(i=0;i<10;i++)
    {
        strcpy(line[i].addr, "someString");
        strcpy(line[i].inst, "someOtherString");
    }
    //when done, free memory
    for(i=0;i<10;i++)
    {
        free(line[i].addr);
        free(line[i].inst);
    }      


}

#1


15  

Allocating works the same for all types. If you need to allocate an array of line structs, you do that with:

所有类型的分配都是相同的。如果需要分配一个行结构数组,可以使用:

struct line* array = malloc(number_of_elements * sizeof(struct line));

In your code, you were allocating an array that had the appropriate size for line pointers, not for line structs. Also note that there is no reason to cast the return value of malloc().

在您的代码中,您分配的数组具有适合行指针的大小,而不是行结构。另请注意,没有理由强制转换malloc()的返回值。

Note that's it's better style to use:

请注意,它的使用方式更好:

sizeof(*array)

instead of:

代替:

sizeof(struct line)

The reason for this is that the allocation will still work as intended in case you change the type of array. In this case this is unlikely, but it's just a general thing worth getting used to.

这样做的原因是,如果您更改数组类型,分配仍将按预期工作。在这种情况下,这不太可能,但这只是一个值得习惯的一般事情。

Also note that it's possible to avoid having to repeat the word struct over and over again, by typedefing the struct:

还要注意,通过typedefing结构,可以避免不得不一遍又一遍地重复单词struct:

typedef struct line
{
    char* addr;
    char* inst;
} line;

You can then just do:

你可以这样做:

line* array = malloc(number_of_elements * sizeof(*array));

Of course don't forget to also allocate memory for array.addr and array.inst.

当然不要忘记为array.addr和array.inst分配内存。

#2


3  

For what you have described, You do not need to allocate memory for your struct, rather, you need to allocate memory for the members char *addr;, and char *inst;. If you want to have a single copy of that structure, the first section of code illustrates how to initialize, and assign values. If you want an array, the second code example illustrates the differences.

对于您所描述的内容,您不需要为结构分配内存,而是需要为成员char * addr;和char * inst;分配内存。如果您希望拥有该结构的单个副本,则代码的第一部分将说明如何初始化和分配值。如果你想要一个数组,第二个代码示例说明了这些差异。

This illustrates how to allocate memory for the members of a single struct line:

这说明了如何为单个结构行的成员分配内存:

typedef struct
{
    char* addr;
    char* inst;
}LINE;

LINE line;  

int main(void)
{   

    strcpy(line.addr, "anystring"); //will fail
    line.addr = malloc(80);
    line.inst = malloc(80);
    strcpy(line.addr, "someString");//success;
    strcpy(line.inst, "someOtherString");//success;

}

For array of struct line...

对于struct line的数组......

typedef struct
{
    char* addr;
    char* inst;
}LINE;  //same struct definition

LINE line[10]; //but create an array of line here.

int main(void)
{   
    int i;

    for(i=0;i<10;i++)
    {
      line[i].addr = malloc(80);
      line[i].inst = malloc(80);
    }

    for(i=0;i<10;i++)
    {
        strcpy(line[i].addr, "someString");
        strcpy(line[i].inst, "someOtherString");
    }
    //when done, free memory
    for(i=0;i<10;i++)
    {
        free(line[i].addr);
        free(line[i].inst);
    }      


}