Difference between sizeof(empty struct) and sizeof(struct with empty array)?

In C, the behavior of a program is undefined if a struct is defined without any named member.

C11-§6.7.2.1:

If the struct-declaration-list does not contain any named members, either directly or via an anonymous structure or anonymous union, the behavior is undefined.

GCC allows an empty struct as an extension and its size will be 0.


For C++, the standard doesn’t allow an object of size 0 and therefore sizof(EmptyStruct) returns a value of 1.
Arrays of zero length are not supported by standard C++¹, but are supported as an extension by GNU and the sizeof operator will return 0 if applied.


1. § 8.5.1-footnote 107) C++ does not have zero length arrays.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)