AFAIK, the sizeof
should not return 0
, but the following program:
#include <iostream>
class A {
public:
int a[0];
};
int main() {
A obj;
std::cout << sizeof(obj) << std::endl;
}
outputs 0
. Why?
C++ does not allow zero-sized arrays. A conforming compiler rejects the code, e.g.:
$ g++-4.8 -pedantic-errors main.cpp
main.cpp:5:14: error: ISO C++ forbids zero-size array 'a' [-Wpedantic]
int a[0];
^
So the behaviour of sizeof
here is simply not relevant. GCC allows it (without -pedantic
) as a compiler extension.
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