Structure size with flexible array element

Considering

struct Foo { uint32_t a; uint32_t b[]; }; 

What is sizeof(Foo) ? Is this an implementation or undefined behavior? Is the answer different for C vs C ++?

+5
source share
1 answer

The compiler will ignore a flexible array element that was not there.

C11-Β§6.7.2.1 (p18)

[...] In most situations, the flexible element of an array is ignored. In particular, the size of the structure looks as if the element of the flexible array were omitted , except that it could have a longer complement than the omission, would imply [...].

AFAIK, the flexible array element is not part of the C ++ standard to C ++ 14. But GNU supports it as an extension . The behavior will be the same for C and C ++.

+7
source

Source: https://habr.com/ru/post/1269987/


All Articles