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?".
-
19It hasn't been "dropped" because it was never part of C++.James McNellis– James McNellis2011年09月18日 01:06:55 +00:00Commented Sep 18, 2011 at 1:06
-
6BTW, this seems to me like a clear question with a definite answer. It doesn't seem subjective, and while the term 'dropped' is a bit loaded and not entirely accurate, I think that's a minor nit.Omnifarious– Omnifarious2011年09月18日 01:11:31 +00:00Commented Sep 18, 2011 at 1:11
-
38I'm kind of surprised that people get so fond of closing "Why?" questions as "subjective". IMHO, many of the best questions on StackOverflow are indeed the "Why" questions. (See here.) Just because multiple people can have multiple guesses as to the answer doesn't mean that there isn't a right answer. I think people should chill down a bit and allow questions like this to go on, instead of closing them on the spot just because they can't think of an objective answer. :\user541686– user5416862011年09月18日 01:17:44 +00:00Commented Sep 18, 2011 at 1:17
-
5The reason for closing questions is to preserve the quality of content on SO. Of all the crappy questions that remain, how is this question generating this much controversy? Just leave it open; it's helpful and interesting.tenfour– tenfour2011年09月18日 01:24:55 +00:00Commented Sep 18, 2011 at 1:24
-
5This isn't a bad question because it's subjective, it's bad because it's a duplicate. :-)Omnifarious– Omnifarious2011年09月18日 01:45:01 +00:00Commented Sep 18, 2011 at 1:45
2 Answers 2
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?
14 Comments
std::vector<T>
initialises its elements, which may incur an unacceptable performance hit in some cases. The decltype
issue is a red-herring; the compiler could simply disallow it, as does the Objective-C++ compiler: Variably modified type 'decltype(a)' (aka 'int [n]') cannot be used as a template argument
.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.
17 Comments
_alloca
so often?)std::string
implementations often have small strings allocated within the object anyway...std::string
implementation uses the small-string optimization.