I have two classes as follows
class A
{
};
class B
{
int a[];
};
int main()
{
cout << sizeof(A) <<endl; //outputs 1
cout << sizeof(B) <<endl; //outputs 0
return 0;
}
I am familiar that size of empty class is 1,but why is the size of class B coming to be ZERO??
GCC permits zero length arrays as an extension: http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
And:
As a quirk of the original implementation of zero-length arrays, sizeof evaluates to zero.
Your code is ill-formed as far as C++ language is concerned. In particular, the class B
shouldn't compile in C++ Standard Conformant compiler. Your compiler has either bug, or it provides this feature as extension.
GCC with -pedantic-errors -std=c++11
gives this error:
cpp.cpp:18:11: error: ISO C++ forbids zero-size array 'a' [-Wpedantic]
int a[];
^
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