3
votes

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).

2
I do not see why support of VLAs and flexible array members should be related.alk
@alk: I think they're not related as far as the standard is concerned, but they both involve arrays with a size determined at run-time (as do many uses of malloc() et al). I was about to add some code to a 'type size' printing program for both VLA types and structure-with-FAM types and wondered whether the __STDC_NO_VLA__ meant that the FAM code should be left untested.Jonathan Leffler
Just out of morbid interest: Have you encountered a modern C compiler that does not support flexible array members? (I'm hoping you have not, and the question came about some other way.)Nominal Animal
@NominalAnimal — No, but I've not tried a recent Microsoft compiler. They have better support for C99 (and maybe C11) than older versions, but I'm not sure how close they hew to either standard, or what their macro definitions claim as support.Jonathan Leffler
Ah, right. I was thinking more along the various embedded C compilers. (As I've mentioned in other discussions, I no longer hold any trust, nor hope, for MS compiler products or even the future versions of the C standard.)Nominal Animal

2 Answers

9
votes

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.

6
votes

Flexible array member support should be independent of VLA support. In fact, one could use flexible array members before C99 standard gave them name by declaring a zero-length array at the end of a struct.

Basically, the only thing you need to do in order to support flexible array member is changing the compiler to support the flexible[] syntax.

In contrast, supporting VLAs requires a lot more effort:

  • Automatic allocations may no longer be done at compile time
  • sizeof operator must be changed to support run-time evaluation
  • A special structure must be designed to keep the size of the array available

These implementation points may be tricky enough for a compiler designer to decide to not implement VLAs.