July 22, 2010
sizeof empty struct in C
An interesting question, what is the size of an empty struct in C/C++?
This question was discussed here.
I did a research and found,
| Visual Studio 2008 (compile as C) | Not allowed. Prompt that C requires Struct or unoin has at least one member. |
| Visual Studio 2008 (compile as C++) | 1 |
| GCC | 0 (sizeof should be followed by a variable, instead of a struct name) |
| G++ | 1 |
Summary: In C++, empty struct is size 1. In C, it is somehow meaningless.
Actually, struct is treated as class in C++ and so empty struct is also allowed. Without any member or functions, the size of a class/struct is 1.
Tags: C/C++
