Richard Stallman asked:
>   > ! Second, because C11 no longer requires support for variable length
>   > ! arrays,
> 
> Can you tell us what's behind this?  Is there some practical problem
> caused by supporting variable-length arrays?

The same question was asked in comp.std.c in 2012 [1]. The main answers were:

1) The standardization group wanted a "core C" with optional features. VLA
   was deemed optional because (quoting Jens Gustedt)

   "There will be more and more borderline architectures in
    the future that combine aspects of hosted and freestanding
    environments. Best example are platforms with GPU. E.g OpenCL is
    supposed to implement most elements of C99, but not VLA. It combines
    compilation on the host environment and on the GPU device.

    And programming for devices will become more specialized. For a device
    like GPU that does parallel geometric processing you might want
    complex numbers (so you'd support that option), atomics and threads
    (maybe the C11 can be made compatible with their lowlevel threads) but
    you don't want VLA on the device.

    The programming model would be very close of what nowadays is a hosted
    environment, but without VLA."

2) Specific problems with VLAs (quoting John Nagle):

   "Here are some of the known problems with VLAs:

    - Putting arbitrarily large arrays on the stack causes trouble
    in multithreaded programs in implementations where stack growth
    is bounded.

    - There's no way to recover from an out-of-memory condition when
    allocating a VLA.

    - Microsoft declines to support them.

    - VLAs aren't used much. There appear to be only three in
    Google Code, and no VLA parameters. The Linux kernel had one,
    but it was taken out because there was no way to handle an
    out of space condition. (If anyone can find an example of
    a VLA parameter in publicly visible production code, please
    let me know.)

    - The semantics of VLA parameters is painful. They're
    automatically reduced to pointers, with the length information
    lost. "sizeof" returns the size of a pointer.

    - Prototypes of functions with VLA parameters do not have
    to exactly match the function definition. This is
    incompatible with C++ style linkage and C++ function
    overloading, preventing the extension of this feature
    into C++."

Later, in C23, VLAs as types and function parameters were made mandatory
again, while VLAs with automatic storage duration (i.e. stack allocation)
are being kept optional. [2]
  "The C23 standard makes VLA types mandatory again. Only creation of
   VLA objects with automatic storage duration is optional."

The rationale behind this [3] is:
  "Security concerns about their use only affect automatic VLAs on the stack"
You can find the WG14 discussions in [4], in particular that
  VLAs (as types) "are useful in many ways, including bounds-checking,
  without overhead."

Bruno

[1] https://groups.google.com/g/comp.std.c/c/AoB6LFHcd88/m/BvA4NiBe2mcJ?pli=1
[2] https://en.wikipedia.org/wiki/Variable-length_array#C99
[3] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2660.pdf
[4] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2921.pdf section 5.6




Reply via email to