This is a contradiction from my point of view. The standard does not allow objects of size zero which make sense to me, because it avoids two distinct objects from sharing same memory address, so empty classes must have a size of at least one byte.
A naive program for testing this idea:
#include <iostream>
using namespace std;
class MyClass {};
int main()
{
MyClass myClassInstance;
std::cout << sizeof(MyClass);
return 0;
}
As expected, sizeof(MyClass)
is 1 byte.
However, the compiler allocates exactly 16 bytes on the stack or 0x10h.
Here is the concrete example:
- In the first example I will use a commented object instance to show that no allocation will occur for the stack.
- In the second one I can observe that making a instance will allocate 16 bytes for the stack. We can take a look over the assembly instruction
sub $0x10, %esp
which subtracts 16 from the extended stack pointer, resulting in a allocation size of 128 bits.
Why is the compiler allocating 16 bytes if the object's size is only 1 byte?