flexible-array-member

Allocating struct with flexible array member

冷暖自知 提交于 2019-11-27 06:06:47
问题 This is C99 code: typedef struct expr_t { int n_children; foo data; // Maybe whatever type with unknown alignment struct expr_t *children[]; } expr_t; Now, how do I allocate memory ? expr_t *e = malloc (sizeof (expr_t) + n * sizeof (expr_t *)); or expr_t *e = malloc (offsetof (expr_t, children) + n * sizeof (expr_t *)); ? Is sizeof even guaranteed to work on an type with flexible array member (GCC accepts it) ? 回答1: expr_t *e = malloc (sizeof (expr_t) + n * sizeof (expr_t *)); is well defined

Flexible array member in C-structure

做~自己de王妃 提交于 2019-11-26 19:09:19
Quoting from the C-std section 6.7.2.1, struct s { int n; double d[]; }; This is a valid structure declaration. I am looking for some practical use of this kind of syntax. To be precise, how is this construct any more or less powerful than keeping a double* as the 2nd element? Or is this another case of 'you-can-do-it-in-multiple-ways'? Arpan The C FAQ answers precisely this question. The quick answer is that this structure will include the double array inside the structure rather than a pointer to an array outside the structure. As a quick example, you could use your structure as in this

Flexible array member not getting copied when I make a shallow copy of a struct

帅比萌擦擦* 提交于 2019-11-26 18:38:19
问题 I have made a shallow copy a struct I have in the following manner: struct Student{ char *name; int age; Courses *list; //First course (node) Student *friends[]; //Flexible array member stores other student pointers }Student; void shallowCopy(const Student *one){ Student *oneCopy = malloc(sizeof(one) + 20*sizeof(Student*)); *oneCopy = *one; <--------------- ERROR POINTS TO THIS LINE } When I check the first element of the flexible array member it oneCopy , it is null. But if I check the first

How to initialize a structure with flexible array member

╄→гoц情女王★ 提交于 2019-11-26 17:46:09
问题 I have the following structure typedef struct _person { int age; char sex; char name[]; }person; I have done some basic internet search (but unsuccessful) on how to create an instance and initialize a structure with a flexible array member without using malloc() . For example: for normal structures like struct a { int age; int sex; }; We can create an instance of struct a and initialize it like struct a p1 = {10, 'm'}; But for structures with flexible array in it (like _person as mentioned

Unsized array declaration in a struct

戏子无情 提交于 2019-11-26 12:31:21
Why does C permit this: typedef struct s { int arr[]; } s; where the array arr has no size specified? Shafik Yaghmour This is C99 feature called flexible arrays , the main feature is to allow the use variable length array like features inside a struct and R.. in this answer to another question on flexible array members provides a list of benefits to using flexible arrays over pointers . The draft C99 standard in section 6.7.2.1 Structure and union specifiers paragraph 16 says: As a special case, the last element of a structure with more than one named member may have an incomplete array type;

Unsized array declaration in a struct

江枫思渺然 提交于 2019-11-26 02:58:56
问题 Why does C permit this: typedef struct s { int arr[]; } s; where the array arr has no size specified? 回答1: This is C99 feature called flexible arrays , the main feature is to allow the use variable length array like features inside a struct and R.. in this answer to another question on flexible array members provides a list of benefits to using flexible arrays over pointers . The draft C99 standard in section 6.7.2.1 Structure and union specifiers paragraph 16 says: As a special case, the

Are flexible array members valid in C++?

空扰寡人 提交于 2019-11-26 02:58:18
问题 In C99, you can declare a flexible array member of a struct as such: struct blah { int foo[]; }; However, when someone here at work tried to compile some code using clang in C++, that syntax did not work. (It had been working with MSVC.) We had to convert it to: struct blah { int foo[0]; }; Looking through the C++ standard, I found no reference to flexible member arrays at all; I always thought [0] was an invalid declaration, but apparently for a flexible member array it is valid. Are

Is using flexible array members in C bad practice?

旧城冷巷雨未停 提交于 2019-11-26 00:41:53
问题 I recently read that using flexible array members in C was poor software engineering practice. However, that statement was not backed by any argument. Is this an accepted fact? (Flexible array members are a C feature introduced in C99 whereby one can declare the last element to be an array of unspecified size. For example: ) struct header { size_t len; unsigned char data[]; }; 回答1: It is an accepted "fact" that using goto is poor software engineering practice. That doesn't make it true. There

Array of zero length

扶醉桌前 提交于 2019-11-26 00:38:17
问题 I am working on refactoring some old code and have found few structs containing zero length arrays (below). Warnings depressed by pragma, of course, but I\'ve failed to create by \"new\" structures containing such structures (error 2233). Array \'byData\' used as pointer, but why not to use pointer instead? or array of length 1? And of course, no comments were added to make me enjoy the process... Any causes to use such thing? Any advice in refactoring those? struct someData { int nData; BYTE