Why does C ++ not support dynamic arrays on the stack?

In C99, it was legal:

void f(size_t sz) { char arr[sz]; // ... } 

However, these β€” dynamically sized stack arrays β€” were flushed in C ++ and did not see a return in C ++ 11.

AFAIK C ++ was made considering compatibility with C, so I wondered. There must be some very good argument not including this useful function, right?

All I could think of was this:

Pros

  • Save memory due to a more reasonable array size that should be on the stack (temporary buffers?).
  • Less "smart pointers" (or, worse, manually entering delete [] errors) and slow heap allocations.
  • Compatible with C99.

Against

  • Allows people to easily allocate too large arrays on the stack, which allows overflowing.
  • Harder for compilers.

So why didn't they turn it on when they imported other C99 features?




To prevent this from being closed as β€œsubjective” or β€œnon-constructive,” I am looking for quotes from community members or links to discussions about this, with bonuses for a quick SO round, of course.

Instead of viewing this as a discussion of Ponies vs Hamsters, treating it as a historical issue, just be interested in the advantages and disadvantages that were considered (if at all).




EDIT . As James McNellis noted in the comments below, C ++ existed before standardized variable length arrays of C99. You could read my question, and then: " Why didn't they add it? "

+28
c ++ stack standards dynamic-arrays
Sep 18 2018-11-11T00:
source share
2 answers

I think this is because C ++ offers excellent solutions: std::vector<T> and std::array<T,N> (C ++ 11); although the latter is not dynamic as such, it is superior to the original arrays. You can always know the size, no matter what function you pass in a vector or array.

Since C cannot provide these solutions, C99 appeared with a variable array length (VLA). It has the same problem as regular arrays: it splits into a pointer to pass to its function, and you no longer know the size of the array.

And as Florian Weimer asked here in comp.std.c++ , what if C ++ 0x allows VLA, what will the following code mean?

 int vla[n]; //n is known at runtime! std::vector<decltype(vla)> v; //what does this mean? 

How is the compiler going to instantiate a vector template at compile time when the type argument is dependent on n , which is known at run time?

+17
Sep 18 '11 at 1:20
source share

This function basically duplicates the std::vector function, except that it consumes a more limited resource (stack versus heap space). So in C ++ there is no need for semantics.

It can be argued that stack allocation can increase efficiency (especially in the case of multiple threads); however, this can also be achieved in C ++ using custom allocators to create a private storage pool, either on the stack or on the heap. This is again more flexible than allocating memory on the stack, and indeed, you can create your own allocator that easily cuts pieces from the memory buffer on the stack. This is not the same as the semantics of dynamic arrays, but the presence of custom allocators and STL containers covers most use cases that require stack allocation.

+2
Sep 18 '11 at 1:17
source share



All Articles