Sizeof empty struct is 0 byte in C but in C++ it is 1 byte. Why? What's the difference?
Asked
Active
Viewed 1.3k times
6
-
What's the difference? They're different languages. – Mike Seymour Feb 18 '14 at 11:21
-
ok! But new features of C is nothing but C++ Correct! if it is different language means how the header files are supported of c in C++? There is a similarity b/w two languages. but why it is 0 byte in C? – Ramakrishna Feb 18 '14 at 11:25
-
@Ramakrishna: Simple: not all C header files are supported in C++. – PlasmaHH Feb 18 '14 at 11:51
1 Answers
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