问题
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++?
回答1:
The compiler will ignore the flexible array member as it were not there.
C11-§6.7.2.1 (p18)
[...] 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 [...].
AFAIK, flexible array member is not the part of C++ standard till c++14. But, GNU support it as an extension. Behaviour will be similar for both C and C++.
来源:https://stackoverflow.com/questions/45193984/size-of-a-struct-with-flexible-array-member