Only the non-static member variables are stored within the allocated memory block. With your example the memory holds only the
x
member and has therefore at least the size of an
int
.
You can verify this by printing out the addresses:
class A
{
public:
A() { x = 0; }
int x;
};
int main()
{
A *a = new A();
printf("Size of A = %lu\n", sizeof(*a));
printf("Address of a = %p\n", a);
printf("Address of a->x = %p\n", &a->x);
return 0;
}
which might print
Size of A = 4
Address of a = 0x602010
Address of a->x = 0x602010
Note also the correct syntax used in my example (class definition must be terminated with a semicolon and
a
is a pointer).