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

2

5

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

Scaife answered 1/7, 2018 at 15:29 Comment(5)
I do not see why support of VLAs and flexible array members should be related.Emersed
@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.Scaife
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.)Fat
@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.Scaife
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.)Fat
M
10

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.

Magda answered 1/7, 2018 at 15:43 Comment(5)
As an aside, I wish the committee had had the wisdom to only make declaring objects with VLA type an optional feature, while mandating support for variably-modified types. This would allow the main practical use of VLA, pointer-to-VLA.Progressive
@R.. Completely agree. This is a defect in C11, in my opinion. Pointer-to-VLA is an important feature in modern C programming. Actual allocated VLA instances: not so much.Nanceynanchang
@R..: IMHO, they should have had a three-way macro to indicate support for VLA instances, VLA types but not instances, or neither. Even if a target platform would have no particular problem supporting VLA types, adding support to a compiler would require adding considerable extra complexity. If a compilers never ends up getting used with any programs that would need VLA types, such complexity would represent a total waste.Castalia
@R..: Or, to put it another way, supports an optional feature that a compiler's users customers want should be regarded as an indication of quality, but support for features customers don't care about shouldn't.Castalia
pointer-to-VAL is an extremely "good thing" indeed. It is not solving the lack of it but I am curious why was nobody mentioning the alloca here? perhaps as a poor mans substitute to VLA?Liner
A
7

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.

Anissaanita answered 1/7, 2018 at 15:43 Comment(2)
The pre-C99 struct hack techniques usually involved an array of size 1. GCC probably allowed size 0 arrays; it still does even though the standard does not.Scaife
@JonathanLeffler: Before C89, a lot of compilers accepted zero-size arrays since it was easier for programmers to use those than to hack around the quirks of size-1 arrays. C99's addition of flexible array members was nothing more than the regaining of a feature that should never have been removed in the first place.Castalia

© 2022 - 2024 — McMap. All rights reserved.