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
131
votes
5 answers

What's the need of array with zero elements?

In the Linux kernel code I found the following thing which I can not understand. struct bts_action { u16 type; u16 size; u8 data[0]; } __attribute__ ((packed)); The code is here:…
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
85
votes
7 answers

Is using flexible array members in C bad practice?

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…
Lionel
60
votes
10 answers

Are flexible array members valid in C++?

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…
MSN
  • 53,214
  • 7
  • 75
  • 105
57
votes
5 answers

Array of zero length

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…
bgee
  • 989
  • 2
  • 13
  • 19
43
votes
4 answers

Internal mechanism of sizeof in C?

I use sizeof to get size of a struct in C, but the result I got is unexpected. struct sdshdr { int len; int free; char buf[]; }; int main(){ printf("struct len:%d\n",(sizeof(struct sdshdr))); return 0; } //struct len:8, with or…
ssj
  • 1,737
  • 2
  • 16
  • 28
36
votes
3 answers

How to initialize a structure with flexible array member

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…
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
34
votes
3 answers

Flexible array members can lead to undefined behavior?

By using flexible array members (FAMs) within structure types, are we exposing our programs to the possibility of undefined behavior? Is it possible for a program to use FAMs and still be a strictly conforming program? Is the offset of the flexible…
Dror K.
  • 1,989
  • 17
  • 26
24
votes
2 answers

Can I "over-extend" an array by allocating more space to the enclosing struct?

Frankly, is such a code valid or does it produce UB? #include #include #include struct __attribute__((__packed__)) weird_struct { int some; unsigned char value[1]; }; int main(void) { unsigned char…
user4385532
24
votes
1 answer

Why does static initialization of flexible array member work?

I have written the following basic code for a menu: typedef struct Menu { char* title; unsigned num_submenus; struct Menu *submenu[]; } Menu; Menu sub1 = {"Submenu 1", 0, {NULL}}; Menu sub2 = {"Submenu 2", 0, {NULL}}; Menu Main = {"Main…
thndrwrks
  • 1,644
  • 1
  • 17
  • 29
23
votes
6 answers

Struct hack equivalent in C++

The struct hack where you have an array of length 0 as the last member of a struct from C90 and C99 is well known, and with the introduction of flexible array members in C99, we even got a standardized way of using it with []. Unfortunately, C++…
Jon Gjengset
  • 4,078
  • 3
  • 30
  • 43
16
votes
2 answers

Are flexible array members really necessary?

A struct with a flexible array member, apparently, is not intended to be declared, but rather used in conjunction with a pointer to that struct. When declaring a flexible array member, there must be at least one other member, and the flexible array…
Theo Chronic
  • 1,623
  • 2
  • 15
  • 16
15
votes
1 answer

How to implement the C flexible array member pattern in Rust?

I would like to implement this C code which uses a flexible array member (sometimes called the struct hack) in Rust: struct test { int key; int dataSize; int data[]; }; struct test* t = malloc(sizeof(struct test) + sizeOfData) The empty…
kirugan
  • 2,514
  • 2
  • 22
  • 41
15
votes
2 answers

Unsized array declaration in a struct

Why does C permit this: typedef struct s { int arr[]; } s; where the array arr has no size specified?
sashang
  • 11,704
  • 6
  • 44
  • 58
14
votes
5 answers

Is it safe to allocate too little space (if you know you won't need it)?

So C99 blessed the commonly-used "flexible array member" hack to allow us to make structs that could be overallocated to suit our size requirements. I suspect it's perfectly safe on most sane implementations to do this, but is it legal in C to…
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
10
votes
4 answers

Flexible array member in C-structure

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…
Fanatic23
  • 3,378
  • 2
  • 28
  • 51
1
2 3
8 9