12

I noticed that when compiled with GCC 4.6 sizeof(Foo) is 0 and sizeof(Bar) is 1. For some reason adding an empty array into an empty structure made its size 0. I thought that the sizes of both structures must be the same. What is going on here?

struct Foo
{
    char x[];
};

struct Bar {};
Peter O.
  • 32,158
  • 14
  • 82
  • 96
pic11
  • 14,267
  • 21
  • 83
  • 119
  • 1
    That's odd, `sizeof` can never be _0_... – K-ballo Jun 10 '12 at 19:24
  • @K-ballo: For flexible arrays like `char x[]` sizeof is 0. – Tudor Jun 10 '12 at 19:25
  • @Tudor: Flexible arrays, is that a compiler extension? – K-ballo Jun 10 '12 at 19:26
  • 2
    @K-ballo No, it's in the standard since C99. But a struct that has a flexible array member must have more than one named member. – Daniel Fischer Jun 10 '12 at 19:27
  • @Daniel Fischer: I guess this is one of those things in which _C_ and _C++_ differ... – K-ballo Jun 10 '12 at 19:28
  • 7
    @K-ballo From [gcc extension docs](http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html): _Flexible array members have incomplete type, and so the sizeof operator may not be applied. As a quirk of the original implementation of zero-length arrays, sizeof evaluates to zero._ – pb2q Jun 10 '12 at 19:28
  • 2
    gcc 4.2 here reports a compiler error: _error: flexible array member in otherwise empty struct_, which agrees with the blurb from the extension docs. – pb2q Jun 10 '12 at 19:32
  • Does this answer your question? [How can this structure have sizeof == 0?](https://stackoverflow.com/questions/47352663/how-can-this-structure-have-sizeof-0) – user202729 Jan 16 '22 at 05:59

3 Answers3

16

Neither struct declaration is allowed by the C standard. 6.7.2.1 (8) in n1570:

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.

And paragraph 18 in the same section:

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply.

(emphasis mine)

Flexible array members are not allowed in C++, so the code is not valid C++ either.

As it is not valid code, the values reported by sizeof for these are meaningless.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
1

The sizeof operator never yields 0, even for an empty class.

as you can see here on msdn

furthermore the msdn is stating:

The sizeof operator cannot be used with the following operands:

  • Functions. (However, sizeof can be applied to pointers to functions.)
  • Bit fields.
  • Undefined classes.
  • The type void.
  • Dynamically allocated arrays.
  • External arrays.
  • Incomplete types.
  • Parenthesized names of incomplete types.
Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
1

C and C++ do not permit zero-sized objects.

gcc does support them as an extension. If you compile with the proper options, such as

gcc -std=c99 -pedantic -Wall -Wextra

gcc will at least warn you about them; g++ has similar options.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631