Sizeof empty struct is 0 byte in C but in C++ it is 1 byte. Why? What's the difference?
Generally, it is 1 byte.
In c the behaviour of an empty structure is compiler dependent versus c++ where it is part of the spec (explanations here) C++ A class with an empty sequence of members and base class objects is an empty class.
The size of an empty class is not zero. It is 1 byte generally. It is nonzero to ensure that the two different objects will have different addresses.
The size of the entire structure is 8 bytes. On knowing the structured padding, it is easier to redesign or rewrite the structure.
In C
its not correct, you cannot have a struct
without a member in it.
C99 says
,
If the struct-declaration-list contains no named members, the behavior is undefined.
However GCC
allows you to have a no member struct with size being 0
. G++
treats struct as if it has a single member of type char
in it.
Look at this previous SO answering why in C++ the size is 1B
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With