3 ms·
A zero sized array declaration was a constraint violation (this requiring a diagnostic) in ISO C 90 in all contexts: even as a function parameter, where the ide
by kazinator 11y ago
A zero sized array declaration was a constraint violation (this requiring a diagnostic) in ISO C 90 in all contexts: even as a function parameter, where the identifier being declared is actually a pointer and not an array at all!
C99 added the "flexible array member" feature: the last element of a struct can be an array of size zero: "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." [C99, 6.7.2.1 ¶16]
In C90 code, the "struct hack" is implemented using an array of size [1] at the end of a struct. Some compilers allowed zero (like GNU C, I think) before C99.
In any case, supporting a [0]-sized array member which is not the last member of a struct, without issuing a diagnostic, is non-conforming. The example given in the article with consecutive [0] arrays requires a diagnostic.
By the way, the (apparently only?) advantage of the flexible array member is that we can use sizeof (type) to obtain the size of the structure just excluding the first element of the array. Whereas with the C90 style struct hack, we must use offsetof(type, last_member) so that we exclude [1] from the calculation:
struct foo {
/* ... */
int array[ZERO_OR_ONE];
};
/* Correct in C99, if ZERO_OR_ONE is 0
Incorrect in C90, (ZERO_OR_ONE can't be zero). */
size_t foo_plus_3_elems = sizeof (struct foo) + 3 * sizeof(int);
/* Correct in C99 whether or not ZERO_OR_ONE is 0 or 1.
Correct in C90 with ZERO_OR_ONE being 1. */
size_t foo_plus_3_elems = offsetof (struct foo, array) + 3 * sizeof(int);
Here, "incorrect" means we calculate slightly more storage than needed, usually without any downside.
"Correct in C90" means de facto correct, not that it was well-defined behavior. "Everyone" was doing it.
- comex 11y agoI think you made a small mistake: an "incomplete array type", as allowed by the clause you cited, is one with empty brackets (int foo[]). Per 6.7.5.2.4: > If the size is not present, the array type is an incomplete type. This is different from specifying the size as 0, which is still not allowed, per 6.7.5.2.1: > In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expression or *. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero. Oh, and for the record, while you may be alluding to this in your last paragraph: another advantage of flexible array members is that indexing them is actually well-defined per the spec (if enough space has been allocated, of course), while AFAIK the spec has never added a special case for length-1 arrays, so accessing the "extra elements" is technically undefined behavior. However, common compilers like GCC do treat length 1 specially, as an exception to optimizations that generally assume you won't index out of bounds, so 'technically' really is 'technically' - it's not something that some random future version of GCC is likely to break your code if you use.
- caf 11y agoThis is right - a flexible array member is denoted with []. If you update the OPs example to try and put two of them in, you get this error from GCC: x.c:7:10: error: flexible array member not at end of struct
- kazinator 11y agoEven though I got the quote right out of C99, I still mixed it up. The reason for this is that ... I don't use flexible array member myself or the [0] GCC extension, either. Flexible array member doesn't seem to bring anything to the table other than formal definedness of behavior. I can't imagine any implementation which supports flexible-array member in its C99+ modes, which makes [1] fail in the same modes or in the C99 mode. In any old compiler that doesn't have C99 support, that formally undefined struct hack is all you have. I don't mind using offsetof(type, member) to obtain the size of the header before the array, rather than sizeof(type). The classic hack is portable to C90 and C++98, making it good for "Clean C". That said, flexible array member is elegant in the sense that it just uses an incomplete array type similarly to a file scope array declaration. Using [1] is a bit like while (1) instead of for (;;). I don't like the [1] in the struct hack, but I don't use C for its beauty.
- maxlybbert 11y ago> "Correct in C90" means de facto correct, not that it was well-defined behavior. "Everyone" was doing it. If "everyone" is defined as "people using GCC."
- kazinator 11y agoThe use of an array of size [1] at the end of a structure is far broader than users of GCC. It is not a GCC extension; it's a trick which based on undefined behavior: accessing [2], [3], [4] ... in that array based on knowing that the space was malloced, which depends on the pointer arithmetic working, while the optimizer looks the other way. C FAQ: [http://c-faq.com/struct/structhack.html http://c-faq.com/struct/structhack.html] "Despite its popularity, the technique is also somewhat notorious: Dennis Ritchie has called it ``unwarranted chumminess with the C implementation,'' and an official interpretation has deemed that it is not strictly conforming with the C Standard, although it does seem to work under all known implementations. (Compilers which check array bounds carefully might issue warnings.)"
- maxlybbert 11y agoI got the impression it was a GCC extension because it's listed in the "C Extensions" of the GCC documentation ( https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html ). EDIT: the FAQ actually refers to arrays of one element where GCC makes a big deal about saving memory by accepting zero-length arrays. But you are correct that this kind of cleverness wasn't limited to GCC ( https://blogs.msdn.microsoft.com/oldnewthing/20031212-00/?p=41523 https://blogs.msdn.microsoft.com/oldnewthing/20031212-00/?p=... , https://blogs.msdn.microsoft.com/oldnewthing/20040826-00/?p=38043 https://blogs.msdn.microsoft.com/oldnewthing/20040826-00/?p=... ).
- comex 11y agoThree different ways to declare an array at the end of a structure: [1] is the old school approach, which is still honored by compilers despite indexing past the first element technically being undefined behavior. [0] is a GCC extension. [] is C99.