简体   繁体   English

将元素添加到灵活数组成员

[英]Adding elements to Flexible Array Members

I've read and looked at some example of flexible array members but I am not exactly sure how to add and read elements of this variable length array. 我已经阅读并查看了一些灵活数组成员的示例,但是我不确定如何添加和读取此可变长度数组的元素。

typedef struct School {
    char *name;
    char *courses[]; //Flexible Array Member
} School;

1) Can someone please show me an example of how I can add an element to this Flexible Length Member and print it after it is stored. 1)有人可以给我一个例子,说明如何将元素添加到此“可变长度”成员中,并在存储后将其打印出来。

2) I also would like to know how to malloc it correctly. 2)我也想知道如何正确地分配它。 Based on what I have read about Flexible Array Members, we would need to add more space for the flexible array member and can't just use sizeof(School); 根据我对灵活数组成员的了解,我们将需要为灵活数组成员增加更多空间,而不仅仅是使用sizeof(School); . My only issue is how do I know how much do add for that flexible member. 我唯一的问题是我如何知道为该灵活成员增加了多少。

You should modify the struct to add the number of courses present in the allocated structure: 您应该修改struct以添加分配的结构中存在的课程数量:

typedef struct School {
    char *name;
    int ncourses;
    char *courses[]; //Flexible Array Member
} School;

Say you have 2 schools, one with 3 courses, one with 2. You would allocate the structures this way: 假设您有2所学校,其中一所开设3门课程,一所开设2门课程。

School *mc = malloc(offsetof(struct School, courses) + 3 * sizeof(char *));
mc->name = strdup("Math College");
mc->ncourses = 3;
mc->courses[0] = strdup("Math 101");
mc->courses[1] = strdup("Math 102");
mc->courses[2] = strdup("Math 103");

School *ps = malloc(offsetof(struct School, courses) + 2 * sizeof(char *));
ps->name = strdup("Psycho School");
ps->ncourses = 2;
ps->courses[0] = strdup("Psycho 101");
ps->courses[1] = strdup("Unknown 404");

As you can see, elements of the variable array are accessed like any other array elements. 如您所见,变量数组的元素像其他数组元素一样被访问。 The malloc call allocates the appropriate size in bytes for the struct members and the array elements (here char * pointers), that are located at the end of the structure. malloc调用为位于结构末尾的struct成员和数组元素(此处为char *指针)分配了适当的字节大小。

You could use a generic function to allocate and initialize such structures: 您可以使用通用函数来分配和初始化此类结构:

School create_school(const char *school_name, int ncourses, char *courses[]) {
    School *sp = malloc(offsetof(struct School, courses) + ncourses * sizeof(char *));
    sp->name = strdup(school_name);
    sp->ncourses = ncourses;
    for (int i = 0; i < ncourses; i++) {
        sp->courses[i] = strdup(courses[i]);
    }
    return sp;
}

The exact formula for computing the required size of your structure is: 计算所需结构尺寸的确切公式为:

size_t need = offsetof(struct School, courses) + num_courses * sizeof(char *);

Note the use of offsetof . 注意使用offsetof Some people use sizeof , but this can introduce memory overhead due to structure padding. 某些人使用sizeof ,但这会由于结构填充而导致内存开销。

Essentially the technique is to dynamically allocate enough memory for the struct, plus the elements of the last array. 本质上,该技术是为结构以及最后一个数组的元素动态分配足够的内存。

School *data = malloc(sizeof(*data) + number * sizeof(*(data->courses)));

for (i = 0; i < number; ++i)
{
     const char hello[] = "Hello";
     data->courses[i] = malloc(strlen(hello) + 1));   /* sizeof char is 1 by definition */
     strcpy(courses[i], hello);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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