arrayscgccclangsize

What is the rationale for 0-size arrays?


Both GCC and Clang support (as an extension) 0-size (also called "0-length") arrays.

What is the rationale for 0-size arrays (example int x[0])?


Solution

  • What is the rationale for 0-size arrays?

    Taking you to mean an explicit zero length:

    int array[0];
    

    The GCC manual describes it as

    useful as the last element of a structure that is really a header for a variable-length object

    . That's the same purpose served by C99+ flexible array members, as the manual goes on to observe, and it recommends FAMs over zero-length arrays for this purpose. The manual also contrasts with pre-C99 conventional practice of using a length-1 array for the same purpose, observing that the two have different effects on the size of the host structure. I take that to be the original inspiration for the extension, and I take continued support to be for backwards compatibility.

    The manual does not discuss using zero-length arrays in other contexts, but GCC does accept that. No rationale is given, but I take it to be for consistency with allowing it for structure members. C semantics can largely be squared with such arrays, though they are not very useful.

    As for Clang, I don't find this extension documented among Clang / LLVM language extensions. If it is supported by that system then my best guess would be that that is for compatibility with GCC, though it could also be a case of parallel evolution.