2009-11-12

Empty structures in C and C++

//test.c
#include <stdio.h>

struct Temp0{};
struct Temp1{char a;};

int main() {
printf("Temp0 size: %d\n", sizeof(struct Temp0));
printf("Temp1 size: %d\n", sizeof(struct Temp1));
return 0;
}


This code being compiled with GNU toolchain gives the following results:

$ gcc test.c -o test && ./test
Temp0 size: 0
Temp1 size: 1



$ g++ test.c -o test && ./test
Temp0 size: 1
Temp1 size: 1


The reasons explained

I wonder are other toolchains give the same results...