Search code examples
clanguage-lawyerc11variable-length-arrayflexible-array-member

If a compiler defines __STDC_NO_VLA__, does it still have to support flexible array members?


In C99, flexible array members (of a structure) and variable length arrays were mandatory parts of the standard — conforming C99 compilers (implementations) have to support them both.

In C11, an implementation is allowed to define (§6.10.8.3 Conditional feature macros):

__STDC_NO_VLA__ The integer constant 1, intended to indicate that the implementation does not support variable length arrays or variably modified types.

I've not spotted anywhere in the standard that stipulates that a structure with a FAM is a variably modified type, so I think that even without support for VLAs, a C11 compiler is required to support FAMs. One item in favour of this interpretation: the size of a structure with a FAM is fixed; the FAM is not counted as part of the size (whereas the size of a VLA is not a compile-time constant).


Solution

  • Well, to belabor the obvious, the standard doesn't say that FAMs are optional, so FAMs aren't optional.

    To go further, though, it seems very unlikely that the standards committee would bother to embrace implementations which didn't support FAMs. Compared to VLAs, adding support for flexible arrays is trivial -- tweak the parser a bit, allow the last member of a struct to be an array of size zero, and call it a day. VLAs require more fiddly static analysis and could potentially be onerous or impossible to implement in some teeny free-standing architectures.