Wednesday, April 02, 2008

Empty structure size in gcc and g++

Did you know that empty structure in GNU C has size 0, whereas in c++ it is 1 character?
#include <stdio.h>

struct A {
};

int main(void)
{
printf("%zu\n", sizeof(struct A));
return 0;
}

Output of this after gcc a.c -oac && ./ac is 0, g++ a.c -oac && ./ac will output 1...

See gnu cc manual, chapter 5.15.

No comments: