3 Replies - 643 Views - Last Post: 27 February 2017 - 09:08 PM

#1 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5830
  • View blog
  • Posts: 19,870
  • Joined: 05-May 12

VLAs optional in C11?!?!

Posted 27 February 2017 - 09:50 AM

I just stumbled across this last night. I didn't know that VLAs were optional in C11. I would have expected that if C99 required VLA's C11 would have kept it as well. Talk about feeling very behind the times.

Also while talking about VLAs: I was under the impression that VLA's were supposed to always allocate on the stack (like alloca()) based on what I'd read about VLAs from the primary C/C++ question forum. Apparently it is up to the compiler to decide whether to use the stack or the heap.

I guess since I don't actively use VLAs, I'm just coming around to learning more about this feature.

Is This A Good Question/Topic? 0
  • +

Replies To: VLAs optional in C11?!?!

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5304
  • View blog
  • Posts: 16,521
  • Joined: 25-December 09

Re: VLAs optional in C11?!?!

Posted 27 February 2017 - 11:04 AM

Yes VLA are optional in C11 but "mandatory" for C99.

Quote

I was under the impression that VLA's were supposed to always allocate on the stack (like alloca()) based on what I'd read about VLAs from the primary C/C++ question forum. Apparently it is up to the compiler to decide whether to use the stack or the heap.

Well since VLA can't be used as static or global variables I would say that the allocation is meant to be on the stack.

Quote

6.7.6.2 Array declarators
Constraints
2
An ordinary identifier (as defined in 6.2.3) that has a variably modified type shall have
either block scope and no linkage 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.
...
10
EXAMPLE 4 All declarations of variably modified (VM) types have to be at either block scope or
function prototype scope. Array objects declared with the _Thread_local, static, or extern
storage-class specifier cannot have a variable length array (VLA) type. However, an object declared with
the static storage-class specifier can have a VM type (that is, a pointer to a VLA type). Finally, all
identifiers declared with a VM type have to be ordinary identifiers and cannot, therefore, be members of
structures or unions.


Quote

I guess since I don't actively use VLAs

IMO, it a good idea not to use them unless absolutely necessary. Remember you're only guaranteed to have them available if you compile as C99. C90 and earlier don't offer any standard support and with C11 the use is optional. Also C++ doesn't allow this "feature" in any of the current standards including the upcoming C++17 standard.

Quote

I'm just coming around to learning more about this feature.

I myself consider VLA a non-feature you should at least be aware of. IMO, the biggest problems I see with this "type" is that they're to easy to use without knowing that you're using them. And since, in many ways, they don't behave the same as "regular" arrays you can easily get into problems and the error messages for their misuse tend to be cryptic at times.

Also be aware several of the C/C++ compilers have implementation defined support for this feature that may or may not follow the actual C standard in all aspects.

Jim

This post has been edited by jimblumberg: 27 February 2017 - 11:04 AM

Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5830
  • View blog
  • Posts: 19,870
  • Joined: 05-May 12

Re: VLAs optional in C11?!?!

Posted 27 February 2017 - 06:43 PM

But does the memory really have to be allocated on the stack? The same way a compiler can inject code to do stackframe checks on entry or exit of a function, it could just as easily inject code to free heap memory allocated by a VLA within a scope. Admittedly, most of the time heap memory alloc/dealloc is slower than changing the stack pointer, but it seems to be within the reach of even a basic compiler.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 5304
  • View blog
  • Posts: 16,521
  • Joined: 25-December 09

Re: VLAs optional in C11?!?!

Posted 27 February 2017 - 09:08 PM

Quote

But does the memory really have to be allocated on the stack?

I doubt that the standard has anything to say about where memory is allocated. I doubt that there is even mention of "stack" or "heap" in the standard as these terms are implementation dependent. Looking at the malloc/calloc/realloc function descriptions in the standard there is no mention of where the memory will be allocated.

Quote

The same way a compiler can inject code to do stackframe checks on entry or exit of a function, it could just as easily inject code to free heap memory allocated by a VLA within a scope.

While it may be possible, IMO the added overhead would probably be very problematic.


Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1