How can this structure have sizeof == 0?

There is an old post asking you to build a construct for which sizeof will return 0 . There are several high-level answers from users of high reputation saying that by the standard, no type or variable can have sizeof 0. And I agree 100% with that.

However, there is this new answer that presents this solution:

 struct ZeroMemory { int *a[0]; }; 

I was going to vote and comment on this, but the time spent here taught me to check even what I am 100% sure of. Therefore ... to my surprise, both gcc and clang show the same results: sizeof(ZeroMemory) == 0 . Moreover, sizeof variable 0 :

 ZeroMemory z{}; static_assert(sizeof(z) == 0); // Awkward... 

Whaaaat ...?

link godbolt

How is this possible?

+76
c ++ language-lawyer sizeof
Nov 17 '17 at 14:14
source share
4 answers

Before C was standardized, many compilers would not have to work with null types if the code never tried to subtract one pointer to zero size from another. These types were useful, and supporting them was easier and cheaper than forbidding them. Other compilers decided to ban such types, however, and some static assertion code could rely on the fact that they would scream if the code tried to create an array of zero size. The authors of the Standard are faced with a choice:

  • Allow compilers to silently accept zero-size array declarations, even in cases where the purpose of such statements would be to initiate diagnostics and interrupts, and require all compilers to accept such declarations (although not necessarily silently).

  • Allow compilers to silently accept declarations of arrays of zero size, even in cases where the purpose of such statements would be to initiate diagnostics and interruptions, and also allow compilers to meet with such declarations in order to either stop compilation or continue at their leisure.

  • Require implementations to issue diagnostics if the code declares a zero-sized array, but then allow implementations to either interrupt compilation or continuation (with any semantics that they consider necessary) at their leisure.

The authors of the Standard have chosen No. 3. Therefore, declarations of zero-size arrays are considered the standard β€œextension”, although such constructions were widely supported before the standard forbade them.

The C ++ Standard allows empty objects to exist, but in order to allow the addresses of empty objects to be used as tokens, it requires that they have a minimum size of 1. For an object that has no members, therefore size 0 will violate the Standard. However, if an object contains elements with a zero size, the C ++ standard does not impose requirements on how it is processed, except that a program containing such an announcement must initiate diagnostics. Since most codes that use such declarations expect the resulting objects to be zero size, the most useful behavior for compilers receiving such code is to process them this way.

+48
Nov 17 '17 at 18:18
source share

As pointed out by Jarod42, zero-size arrays are not standard C ++, but extensions of GCC and Clang.

Adding -pedantic triggers this warning:

 5 : <source>:5:12: warning: zero size arrays are an extension [-Wzero-length-array] int *a[0]; ^ 

I always forget that std=c++XX (instead of std=gnu++XX ) does not disable all extensions.

This still does not explain the sizeof behavior. But at least we know that this is not a standard ...

+39
Nov 17 '17 at 14:19
source share

In C ++, a zero-size array is illegal.

ISO / IEC 14882: 2003 8.3.4 / 1:

[..] If there is a constant expression (5.19), it must be an integral constant expression, and its value must be greater than zero . A constant expression defines the boundary (number of elements in) of the array. If the value of the constant expression is N , the array has N elements numbered 0 through N-1 , and the identifier type D is an array of declarator derivatives of type N T ". [..]

g ++ requires the -pedantic flag -pedantic issue a warning in a zero-size array.

+16
Nov 17 '17 at 14:28
source share

Zero-length arrays are extensions to GCC and Clang. Applying sizeof to arrays of zero length is evaluated to zero .

The C ++ class (empty) cannot have size 0 , but note that the ZeroMemory class ZeroMemory not empty. It has a named element with size 0 and using sizeof returns zero.

+4
Nov 17 '17 at 16:16
source share



All Articles