Can sizeof return 0 (zero)

In C++ an empty class or struct has a sizeof at least 1 by definition. From the C++ standard, 9/3 “Classes”: “Complete objects and member subobjects of class type shall have nonzero size.”

In C an empty struct is not permitted, except by extension (or a flaw in the compiler).

This is a consequence of the grammar (which requires that there be something inside the braces) along with this sentence from 6.7.2.1/7 “Structure and union specifiers”: “If the struct-declaration-list contains no named members, the behavior is undefined”.

If a zero-sized structure is permitted, then it’s a language extension (or a flaw in the compiler). For example, in GCC the extension is documented in “Structures with No Members”, which says:

GCC permits a C structure to have no members:

 struct empty {
 };

The structure will have size zero. In C++, empty structures are part of the language. G++ treats empty structures as if they had a single member of type char.

Leave a Comment