r/cprogramming • • Jul 14 '23

What's the rationale behind "empty structs and zero-length arrays are (apparently) non-standard"?

Many posts on the internet like https://stackoverflow.com/a/9723093 and https://stackoverflow.com/a/1626493 say that zero-length arrays and structs with no members are actually non-standard.

This is quite surprising. Does anyone know why this is the case? Are there any problems of making them standard? Are there any caveats (in terms of implementing the compiler)?

11 Upvotes

19 comments sorted by

4

u/ellisonch Jul 15 '23 edited Jul 15 '23

The C99 rationale gives a small bit of... well, rationale that you might find helpful.

In general, the declaration of zero-length arrays is invalid under the principle of not providing for zero-length objects. The most common use of this contruct has been in the declaration of dynamicly allocated variable size arrays. C99 adds flexable array members for this purpose: see §6.7.2.1. [p 82]

[and yes, all three spelling mistakes are in the original]

If you haven't seen them, flexible array members (arrays with no size at the end of a struct) are allowed since C99, and accomplish most of the things you generally want "empty" arrays for.

2

u/spherical_shell Jul 15 '23

So, do they say why 0-sized objects are not provided?

0

u/flatfinger Jul 18 '23

People on the Committee who wanted the language to include useful constructs compromised with those who didn't want to require that all compilers support them: implementations would be free to extend the language to support the useful constructs provided they document such treatment and issue at least one diagnostic(*) when fed any program that relies upon such an extension, and programmers would be free to use the constructs and ignore the warnings about constructs which most quality compilers would support, even though some compilers might not.

(*) That requirement would be satisfied by an implementation that, when fed any program, would unconditionally output "Warning: this implementations doesn't bother with any diagnostics besides this one for constructs for which the Standard should have, but didn't, provide."

3

u/IamImposter Jul 15 '23

I see 'contruct'. What are other 2?

Edit: 'dynamicly'. What's last one?

Edit2: 'flexable'.

Took me way longer.

5

u/zhivago Jul 14 '23

C does not support any zero length objects, which is an important consideration for pointer arithmetic.

Consider what might happen if it did.

typedef struct {} empty;
empty a[10];
empty *p = a;

Should p + 1 == p?

1

u/spherical_shell Jul 14 '23

Good point! p+1==p seems the only reasonable possibility.

1

u/Clen23 Jul 15 '23

Can you explain the second line of your code please? Im still learning, I guess you're initializing a struct using the "empty" template created at the first line, but what does the [10] mean ?

1

u/zhivago Jul 15 '23

empty a[10]; defines an array, of 10 objects of type empty, named a.

It is not initialized in this case.

0

u/flatfinger Jul 18 '23

The only form of pointer arithmetic for which zero-sized objects would pose any ambiguity is a subtraction of one pointer from another. Having a compile-time constraint that requires that the pointer target be a complete type with non-zero size would alleviate that ambiguity without eliminating the advantages associated that zero-sized types would otherwise provide, such as the ability to explicitly pad structures with elements that may or may not take any space (e.g. if one needs an 8-byte structure whose last 8 bytes hold a pointer).

1

u/zhivago Jul 18 '23

That would prevent any arrays of zero sized objects, and since every C object is effectively stored in an array, any objects involving zero sized objects.

Which limits its use to cases like padding structs.

At this point the committee's approach to special casing the problem seems rather understandable.

1

u/flatfinger Jul 19 '23

I should have clarified that the constraint would only apply when subtracting one pointer from another.

1

u/zhivago Jul 19 '23

So, what is sizeof (zero_sized_type[10]) ?

Any array of zero sized objects must also be zero length.

Which means that you can't even use them for padding.

This is why flexible array members exist outside of the type system.

0

u/flatfinger Jul 19 '23

Arrays of zero-sized objects would not generally be useful, but generalizing constructs down to zero size can be. Consider, for example:

struct tailpointer8 {
  char pad[8-sizeof (void*)];
  void *ptr;
};

The structure should have a fixed size of 8 bytes, and when processed on a compiler where sizeof (void*) is 8 bytes it shouldn't have any padding, but on an implementation where a void* is smaller, it should be padded so that casting the address of whatever follows the struct tailpointer8 to void** and subtracting 1 would yield the address of the ptr member of the struct tailpointer8.

And in response to your question, sizeof (a zero-size type) would be zero. Why shouldn't it be?

1

u/[deleted] Jul 14 '23

There is no definite answer to this that will satisfy everybody. For every reason X you can give of why it is like that, there is always a reason Y of why it isn't made like that to resolve this issue. I can only guess here:

  1. Some features are artifacts/unintentional outcomes of language design.
  2. Some features are intentionally kept nonstandard as it gives compiler designers space to play around with optimizations. They may result in better performance or functionality, but also less portability or compatibility.
  3. Any undefined and unspecified behaviour is meant to be avoided by the programmer and not relied on.
  4. I won't say that it is surprising at all as it is not something abundantly used. To me more surprising is how the order of evaluation of function arguments and subexpressions within any expression is unspecified as they are more common and fundamental aspects of the language.

1

u/spherical_shell Jul 14 '23

These points are generic - do you know any specific points about empty struct?

2

u/[deleted] Jul 14 '23

The C standard and compiler documentation do not explain the reason behind this. Only the language standard committee or compiler designers may have a crisp answer (if any).

1

u/McUsrII Jul 15 '23

I just humbly wonder:

What is the use of a zero length array or an empty struct?

What are the advantages of having those constructs available?

1

u/spherical_shell Jul 15 '23

See, for instance, std::monostate in C++. Or equivalent things in other languages.

1

u/flatfinger Jul 19 '23

Suppose that one wants to have a pointer-type structure member end at a particular offset within the structure. In dialects which allow zero-sized arrays, one could use:

    struct tailPtr8 {
      char pad[8-sizeof (void*)];
      void *ptr;
    };

and have pointers preceded by 6 or 4 bytes on platforms where they would occupy 2 or 4 bytes, but not preceded by any padding on platforms where they would occupy all eight bytes.

The preprocessor can't selectively include or exclude padding in such cases, because it has no way of knowing the sizes of things.