Questions tagged [flexible-array-member]

Flexible array members is a C feature introduced in C99 whereby one can declare the last element to be an array of unspecified size.

Quoted from N1570, 6.7.2.1 Structure and union specifiers:

18 As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

20 EXAMPLE 2 After the declaration:

      struct s { int n; double d[]; }; 

the structure struct s has a flexible array member d. A typical way to use this is:

      int m = /* some value */;
      struct s *p = malloc(sizeof (struct s) + sizeof (double [m]));

and assuming that the call to malloc succeeds, the object pointed to by p behaves, for most purposes, as if p had been declared as:

      struct { int n; double d[m]; } *p;

(there are circumstances in which this equivalence is broken; in particular, the offsets of member d might not be the same).

130 questions
9
votes
4 answers

How to push and pop a void pointer in C

I have this working code: #import #import typedef struct myarray { int len; void* items[]; } MYARRAY; MYARRAY *collection; void mypop(void** val) { puts(collection->items[collection->len]); *val =…
Lance
  • 75,200
  • 93
  • 289
  • 503
8
votes
1 answer

Is this C program with two struct definitions, involving a flexible array member, defined?

The fact that a struct with a flexible array member is a type with which a variable can be declared and to which sizeof can be applied leads to an unusual behavior in the following program. file fam1.c: #include #include struct…
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
8
votes
3 answers

How can I initialize a flexible array in rodata and create a pointer to it?

In C, the code char *c = "Hello world!"; stores Hello world!\0 in rodata and initializes c with a pointer to it. How can I do this with something other than a string? Specifically, I am trying to define my own string type typedef struct { size_t…
Gavin S. Yancey
  • 1,216
  • 1
  • 13
  • 34
8
votes
4 answers

Using the sizeof operator in the initialization of a struct with a flexible array

I want to declare a struct with a flexible array member in it, and then use sizeof() on it. The prototype is: typedef struct { uint16_t length; uint8_t array[][2]; } FLEXIBLE_t; I then declare it: const FLEXIBLE_t test = { …
user1522973
8
votes
2 answers

C99: Flexible array inside union?

I tried to convert something from using the struct hack to using a flexible array member, only to run into the following error message: error: invalid use of structure with flexible array member (GCC 4.8.1, gnu99, MinGW) After trying to track down…
TLW
  • 1,373
  • 9
  • 22
8
votes
3 answers

What are the real benefits of flexible array member?

After reading some posts related to flexible array member, I am still not fully understand why we need such a feature. Possible Duplicate: Flexible array members in C - bad? Is this a Flexible Array Struct Members in C as well? (Blame me if I…
Yanzhe Chen
  • 427
  • 3
  • 9
7
votes
3 answers

Does union support flexible array members?

I have declared a flexible array member in union, like this: #include union ut { int i; int a[]; // flexible array member }; int main(void) { union ut s; return 0; } and compiler gives an error : source_file.c:8:9:…
msc
  • 33,420
  • 29
  • 119
  • 214
7
votes
2 answers

Allocating struct with flexible array member

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…
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
6
votes
5 answers

How to allocate a struct with a flexible array member on the stack

Let's say we have a struct ending with a flexible array member: struct foo { size_t len; uint8_t data[]; }; How to allocate this struct on the stack (ie. memory is automatically released at the end of the scope)? In add, it would be nice if…
Jérôme Pouiller
  • 9,249
  • 5
  • 39
  • 47
6
votes
1 answer

Size of a struct with flexible array member

Given struct Foo { uint32_t a; uint32_t b[]; }; What is sizeof(Foo)? Is it implementation-defined or undefined behaviour? Does the answer differ for C vs C++?
Timmmm
  • 88,195
  • 71
  • 364
  • 509
5
votes
1 answer

What is a flexible array member in a struct?

I'm writing array list in C. I defined an implementation specific struct against a general header. struct array_list_env { void **array; // pointer to the array size_t capacity; // length of the array size_t size; // number of…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
5
votes
1 answer

Flexible array as a class member

GCC G++ 9 This code: class foo { int bar[] = {111, 123}; }; produces an error about initializer for flexible array. But this one: class foo { int bar[2] = {111, 123}; }; compiles as normal. Any workaround for not counting…
tohaz
  • 197
  • 5
  • 19
5
votes
2 answers

If a compiler defines __STDC_NO_VLA__, does it still have to support flexible array members?

In C99, flexible array members (of a structure) and variable length arrays were mandatory parts of the standard — conforming C99 compilers (implementations) have to support them both. In C11, an implementation is allowed to define (§6.10.8.3…
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
5
votes
2 answers

How does an array of structures with flexible array members behave?

As the title states I was wondering how arrays of C-structs with a flexible array member behaves. Here is an example: struct vector { size_t length; double array[]; }; The Wikipedia article says: The sizeof operator on such a struct is…
LastSecondsToLive
  • 744
  • 10
  • 24
5
votes
1 answer

What are the differences between Variable Length Arrays and Flexible Array Member?

I've seen in the ISO C99 committee draft that structs can have an incomplete an array with unspecified size its end, known as Flexible Array Member. On the other hand C99 also has Variable Length Arrays, which allow declaring arrays with size not…
villasv
  • 6,304
  • 2
  • 44
  • 78
1
2
3
8 9