Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't C++ support dynamic arrays on the stack? [closed]

In C99 this was legal:

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

However, this - dynamically sized stack arrays - has been dropped in C++, and not seeing a return in C++11.

AFAIK C++ was made with C compatibility in mind, so I wondered There must be some very good argument of not including this useful feature, right?

All I could think of was this:

Pros

  • Memory savings by allowing smarter array sizes that need to be on the stack (temporary buffers?).
  • Less "smart pointers" (or worse, manual bug-introducing delete []'s) and slow heap allocations.
  • Compatibility with C99.

Cons

  • Allows people to easily allocate too large arrays on the stack giving hard-to-debug stack overflows.
  • More complicated for compiler writers.

So, why did they didn't they include it when they imported other C99 features?


To prevent this from being closed as "subjective" or "not constructive", I'm looking for quotes from commitee members or links to discussions talking about the matter - with bonus points for a quick SO roundup of course.

Rather than seeing this as a Ponies vs Hamsters discussion, see it as a historical question, mere interest in the advantages and disadvantages that were considered (if at all).


EDIT: As James McNellis pointed out in the comments below C++ existed before C99 standardized variable-length arrays. You might read my question then as: "Why didn't and won't they add it?".

like image 304
orlp Avatar asked Sep 18 '11 01:09

orlp


People also ask

Does C support dynamic arrays?

Unlike other high-level languages (Python, JavaScript, etc) C doesn't have built-in dynamic arrays.

Can stack be implemented using dynamic array?

Stack/ Stack Using Dynamic Array (Both Incremental and Doubling Strategy). Implementing stack using Dynamic Array to overcome the exception of Overflow when the array gets full. 3) Second approach is to increase the size of new array by twice. 4) Comparing the Time complexity in both the cases.

What is stack dynamic array?

A stack-dynamic array is one in which the subscript ranges are dynamically bound, and the storage allocation is dynamic “during execution.” Once bound they remain fixed during the lifetime of the variable.

Is a dynamic array stored on heap?

Dynamically allocated arrays are allocated on the heap at run time. The heap space can be assigned to global or local pointer variables that store the address of the allocated heap space (point to the first bucket).


2 Answers

I think, it's because C++ provides superior solutions: std::vector<T> and std::array<T,N> (C++11); though the latter is not dynamic as such but it's superior to raw arrays. You can always know the size, no matter which function you pass the vector or array.

Since C cannot provide these solutions, C99 came up with Variable Length Array (VLA). It has the same problem as regular arrays: it decays into a pointer on passing it to function, and you no longer know the size of the array.

And as Florian Weimer asked here at comp.std.c++ that if C++0x allows VLA, then what would 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 the vector template at compile-time when it's type argument depends on n which is known at runtime?

like image 149
Nawaz Avatar answered Sep 17 '22 16:09

Nawaz


This functionality largely duplicates that of std::vector, except that it consumes a more limited resource (stack vs heap space). As such, there is not really any need for it in C++, semantics-wise.

One could argue that on-stack allocation can improve efficiency (particularly in the face of multiple threads); however, this can also be achieved in C++ using custom allocators to build a private memory pool, either on the stack or heap. This is again more flexible than placing memory on the stack, and indeed you could create a custom allocator that carves chunks out of an on-stack memory buffer easily enough. It's not exactly the same as dynamic array semantics, but the existence of custom allocators and STL containers covers most use cases you'd want stack allocation.

like image 20
bdonlan Avatar answered Sep 18 '22 16:09

bdonlan