1

I read in some books that the variables use to declare variable length arrays must have auto storage class.

Can someone explain me why does it need variables with auto storage class?

And are the space for variable length arrays allocated in stack or heaps?

1 Answers1

1

This is covered in the Rationale for International Standard—Programming Languages—C which says:

All variably modified types must be declared at either block scope or function prototype scope. File scope identifiers cannot be declared with a variably modified type. Furthermore, array objects declared with either the static or extern storage class specifiers cannot be declared with a variable length array type [...]

and:

Restricting variable length array declarators to identifiers with automatic storage duration is natural since “variableness” at file scope requires some notion of parameterized typing. There was sentiment for allowing structure members to be variably modified; however allowing structure members to have a variable length array type introduces a host of problems such as the treatment when passing these objects, or even pointers to these objects, as parameters. In addition, the semantics of the offsetof macro would need to be extended and runtime semantics added. Finally, there was disagreement whether the size of a variable length array member could be determined using one of the other members. The Committee decided to limit variable length array types to declarations outside structures and unions.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Can you explain me some of those problems?? – Andrew Flemming Jun 18 '15 at 19:45
  • This answer is wrong. The rationale just says that objects of `variable modified types` can only be declared to have automatic storage. It means that they can only be defined at function body, not at global scope or as structure member nor as `static`. The objects can be VLAs, pointer to VLAs, arrays of VLAs, etc. The pointer to VLA lets you create VLA objects dynamically – tstanisl Feb 19 '21 at 09:50
  • @AndrewFlemming One obvious problem if you'd allow `static size_t n=42; static int arr[n];` is the static initialization order. Static storage duration objects are initialized before main() is called. In this example `n` would go to `.data` and `arr` on `.bss`. Then you have to consider both the individual initialization order in each segment as well as the order between `.data` and `.bss`. Before you know it, you'd have invented something brain-damaged and unnecessary complex like the horrible initialization rules in C++. – Lundin Feb 19 '21 at 09:51
  • @tstanisl I wouldn't call it wrong. C states the following rule for declaring an array, 6.7.6.2: "If an identifier is declared as having a variably modified type, it shall be an ordinary identifier (as defined in 6.2.3), have no linkage, and have either block scope or function prototype scope. If an identifier is declared to be an object with static or thread storage duration, it shall not have a variable length array type." Meaning that you can't _declare_ a VLA with static storage duration. But yes, you can declare them with allocated storage duration too. – Lundin Feb 19 '21 at 09:57
  • @Lundin, surprisingly one can declare a pointer to VLA with static storage duration. `int n = 5; static int (*arr)[n];`. Is there any interesting application of this? – tstanisl May 14 '22 at 21:59
  • @tstanisl I don't think you can declare it at file scope still. As for interesting application... anywhere where you'll need a local static pointer. Which is rare, VLA or no VLA. – Lundin May 16 '22 at 07:42