6

Sizeof empty struct is 0 byte in C but in C++ it is 1 byte. Why? What's the difference?

unwind
  • 391,730
  • 64
  • 469
  • 606
Ramakrishna
  • 89
  • 1
  • 1
  • 3

1 Answers1

10

In C its not correct, you cannot have a struct without a member in it.

C99 says,

If the struct-declaration-list contains no named members, the behavior is undefined.

However GCC allows you to have a no member struct with size being 0. G++ treats struct as if it has a single member of type char in it.

Look at this previous SO answering why in C++ the size is 1B.

Community
  • 1
  • 1
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
  • Clang will also print 1 byte if you apply `sizeof()` in an empty struct (should be same for a class). – gsamaras Apr 13 '19 at 15:39