Why aren't variable-length arrays part of the C++ standard?












269














I haven't used C very much in the last few years. When I read this question today I came across some C syntax which I wasn't familiar with.



Apparently in C99 the following syntax is valid:



void foo(int n) {
int values[n]; //Declare a variable length array
}


This seems like a pretty useful feature. Was there ever a discussion about adding it to the C++ standard, and if so, why it was omitted?



Some potential reasons:




  • Hairy for compiler vendors to implement

  • Incompatible with some other part of the standard

  • Functionality can be emulated with other C++ constructs


The C++ standard states that array size must be a constant expression (8.3.4.1).



Yes, of course I realize that in the toy example one could use std::vector<int> values(m);, but this allocates memory from the heap and not the stack. And if I want a multidimensional array like:



void foo(int x, int y, int z) {
int values[x][y][z]; // Declare a variable length array
}


the vector version becomes pretty clumsy:



void foo(int x, int y, int z) {
vector< vector< vector<int> > > values( /* Really painful expression here. */);
}


The slices, rows and columns will also potentially be spread all over memory.



Looking at the discussion at comp.std.c++ it's clear that this question is pretty controversial with some very heavyweight names on both sides of the argument. It's certainly not obvious that a std::vector is always a better solution.










share|improve this question




















  • 3




    Just out of curiosity, why does it need to be allocated on the stack? Are you that affraid of heap allocation performance issues?
    – Dimitri C.
    Dec 11 '09 at 10:32






  • 27




    @Dimitri Not really, but there's no denying that stack allocation will be faster than heap allocation. And in some cases this may matter.
    – Andreas Brinck
    Dec 11 '09 at 10:37






  • 9




    The main advantage of variable length arrays that all data is close together so when you iterate through this array you read and write bytes next to each other. Your data is fetched into the cache and cpu can work on it without fetching and sending the bytes to/from the memory.
    – Calmarius
    Oct 24 '10 at 16:04






  • 2




    Variable length arrays are also may be used to replace preprocessor constants with static const variables. Also in C you don't have another options for VLA, and it is sometimes needed to write portable C/C++ code (compatible with both compilers).
    – Yury
    Dec 21 '12 at 6:45










  • as an aside, it appears clang++ allows VLAs.
    – user3426763
    Mar 16 '14 at 20:42
















269














I haven't used C very much in the last few years. When I read this question today I came across some C syntax which I wasn't familiar with.



Apparently in C99 the following syntax is valid:



void foo(int n) {
int values[n]; //Declare a variable length array
}


This seems like a pretty useful feature. Was there ever a discussion about adding it to the C++ standard, and if so, why it was omitted?



Some potential reasons:




  • Hairy for compiler vendors to implement

  • Incompatible with some other part of the standard

  • Functionality can be emulated with other C++ constructs


The C++ standard states that array size must be a constant expression (8.3.4.1).



Yes, of course I realize that in the toy example one could use std::vector<int> values(m);, but this allocates memory from the heap and not the stack. And if I want a multidimensional array like:



void foo(int x, int y, int z) {
int values[x][y][z]; // Declare a variable length array
}


the vector version becomes pretty clumsy:



void foo(int x, int y, int z) {
vector< vector< vector<int> > > values( /* Really painful expression here. */);
}


The slices, rows and columns will also potentially be spread all over memory.



Looking at the discussion at comp.std.c++ it's clear that this question is pretty controversial with some very heavyweight names on both sides of the argument. It's certainly not obvious that a std::vector is always a better solution.










share|improve this question




















  • 3




    Just out of curiosity, why does it need to be allocated on the stack? Are you that affraid of heap allocation performance issues?
    – Dimitri C.
    Dec 11 '09 at 10:32






  • 27




    @Dimitri Not really, but there's no denying that stack allocation will be faster than heap allocation. And in some cases this may matter.
    – Andreas Brinck
    Dec 11 '09 at 10:37






  • 9




    The main advantage of variable length arrays that all data is close together so when you iterate through this array you read and write bytes next to each other. Your data is fetched into the cache and cpu can work on it without fetching and sending the bytes to/from the memory.
    – Calmarius
    Oct 24 '10 at 16:04






  • 2




    Variable length arrays are also may be used to replace preprocessor constants with static const variables. Also in C you don't have another options for VLA, and it is sometimes needed to write portable C/C++ code (compatible with both compilers).
    – Yury
    Dec 21 '12 at 6:45










  • as an aside, it appears clang++ allows VLAs.
    – user3426763
    Mar 16 '14 at 20:42














269












269








269


106





I haven't used C very much in the last few years. When I read this question today I came across some C syntax which I wasn't familiar with.



Apparently in C99 the following syntax is valid:



void foo(int n) {
int values[n]; //Declare a variable length array
}


This seems like a pretty useful feature. Was there ever a discussion about adding it to the C++ standard, and if so, why it was omitted?



Some potential reasons:




  • Hairy for compiler vendors to implement

  • Incompatible with some other part of the standard

  • Functionality can be emulated with other C++ constructs


The C++ standard states that array size must be a constant expression (8.3.4.1).



Yes, of course I realize that in the toy example one could use std::vector<int> values(m);, but this allocates memory from the heap and not the stack. And if I want a multidimensional array like:



void foo(int x, int y, int z) {
int values[x][y][z]; // Declare a variable length array
}


the vector version becomes pretty clumsy:



void foo(int x, int y, int z) {
vector< vector< vector<int> > > values( /* Really painful expression here. */);
}


The slices, rows and columns will also potentially be spread all over memory.



Looking at the discussion at comp.std.c++ it's clear that this question is pretty controversial with some very heavyweight names on both sides of the argument. It's certainly not obvious that a std::vector is always a better solution.










share|improve this question















I haven't used C very much in the last few years. When I read this question today I came across some C syntax which I wasn't familiar with.



Apparently in C99 the following syntax is valid:



void foo(int n) {
int values[n]; //Declare a variable length array
}


This seems like a pretty useful feature. Was there ever a discussion about adding it to the C++ standard, and if so, why it was omitted?



Some potential reasons:




  • Hairy for compiler vendors to implement

  • Incompatible with some other part of the standard

  • Functionality can be emulated with other C++ constructs


The C++ standard states that array size must be a constant expression (8.3.4.1).



Yes, of course I realize that in the toy example one could use std::vector<int> values(m);, but this allocates memory from the heap and not the stack. And if I want a multidimensional array like:



void foo(int x, int y, int z) {
int values[x][y][z]; // Declare a variable length array
}


the vector version becomes pretty clumsy:



void foo(int x, int y, int z) {
vector< vector< vector<int> > > values( /* Really painful expression here. */);
}


The slices, rows and columns will also potentially be spread all over memory.



Looking at the discussion at comp.std.c++ it's clear that this question is pretty controversial with some very heavyweight names on both sides of the argument. It's certainly not obvious that a std::vector is always a better solution.







c++ arrays standards variable-length-array variable-length






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 12:18









Community

11




11










asked Dec 11 '09 at 10:15









Andreas Brinck

37.4k1371106




37.4k1371106








  • 3




    Just out of curiosity, why does it need to be allocated on the stack? Are you that affraid of heap allocation performance issues?
    – Dimitri C.
    Dec 11 '09 at 10:32






  • 27




    @Dimitri Not really, but there's no denying that stack allocation will be faster than heap allocation. And in some cases this may matter.
    – Andreas Brinck
    Dec 11 '09 at 10:37






  • 9




    The main advantage of variable length arrays that all data is close together so when you iterate through this array you read and write bytes next to each other. Your data is fetched into the cache and cpu can work on it without fetching and sending the bytes to/from the memory.
    – Calmarius
    Oct 24 '10 at 16:04






  • 2




    Variable length arrays are also may be used to replace preprocessor constants with static const variables. Also in C you don't have another options for VLA, and it is sometimes needed to write portable C/C++ code (compatible with both compilers).
    – Yury
    Dec 21 '12 at 6:45










  • as an aside, it appears clang++ allows VLAs.
    – user3426763
    Mar 16 '14 at 20:42














  • 3




    Just out of curiosity, why does it need to be allocated on the stack? Are you that affraid of heap allocation performance issues?
    – Dimitri C.
    Dec 11 '09 at 10:32






  • 27




    @Dimitri Not really, but there's no denying that stack allocation will be faster than heap allocation. And in some cases this may matter.
    – Andreas Brinck
    Dec 11 '09 at 10:37






  • 9




    The main advantage of variable length arrays that all data is close together so when you iterate through this array you read and write bytes next to each other. Your data is fetched into the cache and cpu can work on it without fetching and sending the bytes to/from the memory.
    – Calmarius
    Oct 24 '10 at 16:04






  • 2




    Variable length arrays are also may be used to replace preprocessor constants with static const variables. Also in C you don't have another options for VLA, and it is sometimes needed to write portable C/C++ code (compatible with both compilers).
    – Yury
    Dec 21 '12 at 6:45










  • as an aside, it appears clang++ allows VLAs.
    – user3426763
    Mar 16 '14 at 20:42








3




3




Just out of curiosity, why does it need to be allocated on the stack? Are you that affraid of heap allocation performance issues?
– Dimitri C.
Dec 11 '09 at 10:32




Just out of curiosity, why does it need to be allocated on the stack? Are you that affraid of heap allocation performance issues?
– Dimitri C.
Dec 11 '09 at 10:32




27




27




@Dimitri Not really, but there's no denying that stack allocation will be faster than heap allocation. And in some cases this may matter.
– Andreas Brinck
Dec 11 '09 at 10:37




@Dimitri Not really, but there's no denying that stack allocation will be faster than heap allocation. And in some cases this may matter.
– Andreas Brinck
Dec 11 '09 at 10:37




9




9




The main advantage of variable length arrays that all data is close together so when you iterate through this array you read and write bytes next to each other. Your data is fetched into the cache and cpu can work on it without fetching and sending the bytes to/from the memory.
– Calmarius
Oct 24 '10 at 16:04




The main advantage of variable length arrays that all data is close together so when you iterate through this array you read and write bytes next to each other. Your data is fetched into the cache and cpu can work on it without fetching and sending the bytes to/from the memory.
– Calmarius
Oct 24 '10 at 16:04




2




2




Variable length arrays are also may be used to replace preprocessor constants with static const variables. Also in C you don't have another options for VLA, and it is sometimes needed to write portable C/C++ code (compatible with both compilers).
– Yury
Dec 21 '12 at 6:45




Variable length arrays are also may be used to replace preprocessor constants with static const variables. Also in C you don't have another options for VLA, and it is sometimes needed to write portable C/C++ code (compatible with both compilers).
– Yury
Dec 21 '12 at 6:45












as an aside, it appears clang++ allows VLAs.
– user3426763
Mar 16 '14 at 20:42




as an aside, it appears clang++ allows VLAs.
– user3426763
Mar 16 '14 at 20:42












13 Answers
13






active

oldest

votes


















170














There recently was a discussion about this kicked off in usenet: Why no VLAs in C++0x.



I agree with those people that seem to agree that having to create a potential large array on the stack, which usually has only little space available, isn't good. The argument is, if you know the size beforehand, you can use a static array. And if you don't know the size beforehand, you will write unsafe code.



C99 VLAs could provide a small benefit of being able to create small arrays without wasting space or calling constructors for unused elements, but they will introduce rather large changes to the type system (you need to be able to specify types depending on runtime values - this does not yet exist in current C++, except for new operator type-specifiers, but they are treated specially, so that the runtime-ness doesn't escape the scope of the new operator).



You can use std::vector, but it is not quite the same, as it uses dynamic memory, and making it use one's own stack-allocator isn't exactly easy (alignment is an issue, too). It also doesn't solve the same problem, because a vector is a resizable container, whereas VLAs are fixed-size. The C++ Dynamic Array proposal is intended to introduce a library based solution, as alternative to a language based VLA. However, it's not going to be part of C++0x, as far as I know.






share|improve this answer



















  • 16




    +1 and accepted. One comment though, I think the safety argument is a little bit weak since there are so many other ways to cause stack overflows. The safety argument could be used to support the position that you should never use recursion and that you should allocate all objects from the heap.
    – Andreas Brinck
    Dec 11 '09 at 10:46






  • 16




    So you're saying that because there are other ways to cause stack overflows, we might as well encourage more of them?
    – jalf
    Dec 11 '09 at 10:57






  • 2




    @Andreas, agreed about the weakness. But for recursion, it takes a huge number of calls until stack is eaten up, and if that can be the case, people would use iteration. As some people on the usenet thread say, though, this is not an argument against VLAs in all cases, since sometimes you definitely may know an upper bound. But in those cases, from what i see a static array can equally be sufficient, since it would not waste much space anyway (if it would, then you would actually have to ask whether the stack area is large enough again).
    – Johannes Schaub - litb
    Dec 11 '09 at 11:00






  • 10




    Also look at Matt Austern's answer in that thread: The language specification of VLAs would probably considerably more complex for C++, because of the stricter type matches in C++ (example: C allows assigning a T(*) to a T(*)[N] - in C++ this is not allowed, since C++ does not know about "type compatibility" - it requires exact matches), type parameters, exceptions, con- and destructors and stuffs. I'm not sure whether the benefits of VLAs would really pay off all that work. But then, i have never used VLAs in real life, so i probably don't know good use cases for them.
    – Johannes Schaub - litb
    Dec 11 '09 at 11:03






  • 1




    @AHelps: Perhaps what would be best for that would be a type that behaves somewhat like vector but requires a fixed LIFO usage pattern and maintains one or more per-thread statically-allocated buffers which are generally sized according to the largest total allocation the thread has ever used, but which could be explicitly trimmed. A normal "allocation" would in the common case require nothing more than a pointer copy, pointer-from-pointer subtraction, integer comparison, and pointer addition; de-allocation would simply require a pointer copy. Not much slower than a VLA.
    – supercat
    Jun 3 '15 at 16:15



















171














(Background: I have some experience implementing C and C++ compilers.)



Variable-length arrays in C99 were basically a misstep. In order to support VLAs, C99 had to make the following concessions to common sense:




  • sizeof x is no longer always a compile-time constant; the compiler must sometimes generate code to evaluate a sizeof-expression at runtime.


  • Allowing two-dimensional VLAs (int A[x][y]) required a new syntax for declaring functions that take 2D VLAs as parameters: void foo(int n, int A[*]).


  • Less importantly in the C++ world, but extremely important for C's target audience of embedded-systems programmers, declaring a VLA means chomping an arbitrarily large chunk of your stack. This is a guaranteed stack-overflow and crash. (Anytime you declare int A[n], you're implicitly asserting that you have 2GB of stack to spare. After all, if you know "n is definitely less than 1000 here", then you would just declare int A[1000]. Substituting the 32-bit integer n for 1000 is an admission that you have no idea what the behavior of your program ought to be.)



Okay, so let's move to talking about C++ now. In C++, we have the same strong distinction between "type system" and "value system" that C89 does… but we've really started to rely on it in ways that C has not. For example:



template<typename T> struct S { ... };
int A[n];
S<decltype(A)> s; // equivalently, S<int[n]> s;


If n weren't a compile-time constant (i.e., if A were of variably modified type), then what on earth would be the type of S? Would S's type also be determined only at runtime?



What about this:



template<typename T> bool myfunc(T& t1, T& t2) { ... };
int A1[n1], A2[n2];
myfunc(A1, A2);


The compiler must generate code for some instantiation of myfunc. What should that code look like? How can we statically generate that code, if we don't know the type of A1 at compile time?



Worse, what if it turns out at runtime that n1 != n2, so that !std::is_same<decltype(A1), decltype(A2)>()? In that case, the call to myfunc shouldn't even compile, because template type deduction should fail! How could we possibly emulate that behavior at runtime?



Basically, C++ is moving in the direction of pushing more and more decisions into compile-time: template code generation, constexpr function evaluation, and so on. Meanwhile, C99 was busy pushing traditionally compile-time decisions (e.g. sizeof) into the runtime. With this in mind, does it really even make sense to expend any effort trying to integrate C99-style VLAs into C++?



As every other answerer has already pointed out, C++ provides lots of heap-allocation mechanisms (std::unique_ptr<int> A = new int[n]; or std::vector<int> A(n); being the obvious ones) when you really want to convey the idea "I have no idea how much RAM I might need." And C++ provides a nifty exception-handling model for dealing with the inevitable situation that the amount of RAM you need is greater than the amount of RAM you have. But hopefully this answer gives you a good idea of why C99-style VLAs were not a good fit for C++ — and not really even a good fit for C99. ;)





For more on the topic, see N3810 "Alternatives for Array Extensions", Bjarne Stroustrup's October 2013 paper on VLAs. Bjarne's POV is very different from mine; N3810 focuses more on finding a good C++ish syntax for the things, and on discouraging the use of raw arrays in C++, whereas I focused more on the implications for metaprogramming and the typesystem. I don't know if he considers the metaprogramming/typesystem implications solved, solvable, or merely uninteresting.






share|improve this answer



















  • 13




    I agree VLAs were just wrong. The much more widely implemented, and far more useful, alloca() should have been standardized in C99 instead. VLAs are what happens when a standards committee jumps out ahead of implementations, instead of the other way around.
    – MadScientist
    Mar 25 '14 at 11:21






  • 1




    I didn't realise that C allowed VLAs as function parameters. I agree (for what that is worth, as I don't write compilers) that that is rather frightening. VLAs as local variables on the other hand do seem sensible to me. Granted you can overflow your stack, but you have to think about that any time you use local variables, so there is nothing really new there. And the "sizeof" thing - isn't that just the price for a feature?
    – michaeljt
    Sep 26 '14 at 12:56






  • 5




    The variably-modified type system is a great addition IMO, and none of your bullet points violate common sense. (1) the C standard does not distinguish between "compile-time" and "run-time" so this is a non-issue; (2) The * is optional, you can (and should) write int A[n]; (3) You can use the type system without actually declaring any VLAs. For example a function can accept array of variably modified type, and it can be called with non-VLA 2-D arrays of differing dimensions. However you make valid points in the latter part of your post.
    – M.M
    Mar 17 '15 at 4:52






  • 1




    "declaring a VLA means chomping an arbitrarily large chunk of your stack. This is a guaranteed stack-overflow and crash. (Anytime you declare int A[n], you're implicitly asserting that you have 2GB of stack to spare" is empirically false. I just ran a VLA program with a stack far less than 2GB without any stack overflow.
    – Jeff
    May 31 '16 at 20:54






  • 1




    @Jeff: What was the maximum value of n in your test case, and what was the size of your stack? I suggest you try inputting a value for n at least as large as the size of your stack. (And if there's no way for the user to control the value of n in your program, then I suggest you just propagate the maximum value of n straight into the declaration: declare int A[1000] or whatever it is you need. VLAs are only necessary, and only dangerous, when the maximum value of n isn't bounded by any small compile-time constant.)
    – Quuxplusone
    May 31 '16 at 21:41





















25














You could always use alloca() to allocate memory on the stack at runtime, if you wished:



void foo (int n)
{
int *values = (int *)alloca(sizeof(int) * n);
}


Being allocated on the stack implies that it will automatically be freed when the stack unwinds.



Quick note: As mentioned in the Mac OS X man page for alloca(3), "The alloca() function is machine and compiler dependent; its use is dis-couraged." Just so you know.






share|improve this answer

















  • 2




    Also, the scope for alloca() is the entire function, not just the block of code containing the variable. So using it inside of a loop it will continuously increase the stack. A VLA does not have this problem.
    – sashoalm
    Jul 1 '16 at 11:49






  • 2




    However, VLAs having the scope of the enclosing block means they are significantly less useful than alloca() with the scope of the entire function. Consider: if (!p) { p = alloca(strlen(foo)+1); strcpy(p, foo); } This cannot be done with VLAs, precisely because of their block scope.
    – MadScientist
    Jan 12 '17 at 16:35










  • That does not answer OP's why question. Moreover, this is a C-like solution, and not really C++-ish.
    – Adrian W
    Jun 25 at 15:37





















10














There are situations where allocating heap memory is very expensive compared to the operations performed. An example is matrix math. If you work with smallish matrices say 5 to 10 elements and do a lot of arithmetics the malloc overhead will be really significant. At the same time making the size a compile time constant does seem very wasteful and inflexible.



I think that C++ is so unsafe in itself that the argument to "try to not add more unsafe features" is not very strong. On the other hand, as C++ is arguably the most runtime efficient programming language features which makes it more so are always useful: People who write performance critical programs will to a large extent use C++, and they need as much performance as possible. Moving stuff from heap to stack is one such possibility. Reducing the number of heap blocks is another. Allowing VLAs as object members would one way to achieve this. I'm working on such a suggestion. It is a bit complicated to implement, admittedly, but it seems quite doable.






share|improve this answer





























    10














    In my own work, I've realized that every time I've wanted something like variable-length automatic arrays or alloca(), I didn't really care that the memory was physically located on the cpu stack, just that it came from some stack allocator that didn't incur slow trips to the general heap. So I have a per-thread object that owns some memory from which it can push/pop variable sized buffers. On some platforms I allow this to grow via mmu. Other platforms have a fixed size (usually accompanied by a fixed size cpu stack as well because no mmu). One platform I work with (a handheld game console) has precious little cpu stack anyway because it resides in scarce, fast memory.



    I'm not saying that pushing variable-sized buffers onto the cpu stack is never needed. Honestly I was surprised back when I discovered this wasn't standard, as it certainly seems like the concept fits into the language well enough. For me though, the requirements "variable size" and "must be physically located on the cpu stack" have never come up together. It's been about speed, so I made my own sort of "parallel stack for data buffers".






    share|improve this answer





























      9














      Seems it will be available in C++14:



      https://en.wikipedia.org/wiki/C%2B%2B14#Runtime-sized_one_dimensional_arrays



      Update: It did not make it into C++14.






      share|improve this answer























      • interesting. Herb Sutter discusses it here under Dynamic Arrays: isocpp.org/blog/2013/04/trip-report-iso-c-spring-2013-meeting (this is the reference for the wikipedia information)
        – Default
        Aug 13 '13 at 11:12






      • 1




        "Run-time sized arrays and dynarray have been moved to the Array Extensions technical specification" wrote 78.86.152.103 on Wikipedia on 18 January 2014‎: en.wikipedia.org/w/…
        – strager
        Feb 24 '14 at 3:26








      • 9




        Wikipedia isn't a normative reference :) This proposal did not make it into C++14.
        – M.M
        Nov 4 '14 at 2:37










      • @M.M Will it ever?
        – this
        Nov 28 '15 at 19:43






      • 1




        @ViktorSehr: What's the status of this w.r.t. C++17?
        – einpoklum
        May 10 '16 at 23:37



















      6














      This was considered for inclusion in C++/1x, but was dropped (this is a correction to what I said earlier).



      It would be less useful in C++ anyway since we already have std::vector to fill this role.






      share|improve this answer



















      • 40




        No, we don't, std::vector doesn't allocate data on the stack. :)
        – Kos
        Aug 10 '11 at 16:34






      • 6




        "the stack" is an implementation detail; the compiler may allocate memory from anywhere so long as the guarantees about object lifetime are met.
        – M.M
        Mar 16 '14 at 21:20






      • 1




        @M.M: Fair enough, but in practice we still can't use std::vector instead of, say, alloca().
        – einpoklum
        May 10 '16 at 23:36










      • @einpoklum in terms of getting correct output for your program , you can. Performance is a quality-of-implementation issue
        – M.M
        May 10 '16 at 23:50



















      2














      Use std::vector for this. For example:



      std::vector<int> values;
      values.resize(n);


      The memory will be allocated on the heap, but this holds only a small performance drawback. Furthermore, it is wise not to allocate large datablocks on the stack, as it is rather limited in size.






      share|improve this answer



















      • 3




        A major application for variable length arrays is evaluation of arbitrary degree polynomials. In that case, your "small performance drawback" means "the code runs five times slower in typical cases." That's not small.
        – AHelps
        Apr 10 '15 at 18:26



















      1














      C99 allows VLA. And it puts some restrictions on how to declare VLA. For details, refer to 6.7.5.2 of the standard. C++ disallows VLA. But g++ allows it.






      share|improve this answer





















      • Can you provide a link to the standard paragraph that you are pointing ?
        – Vincent
        May 26 '17 at 11:10



















      0














      Arrays like this are part of C99, but not part of standard C++. as others have said, a vector is always a much better solution, which is probably why variable sized arrays are not in the C++ standatrd (or in the proposed C++0x standard).



      BTW, for questions on "why" the C++ standard is the way it is, the moderated Usenet newsgroup comp.std.c++ is the place to go to.






      share|improve this answer



















      • 5




        -1 Vector is not always better. Often, yes. Always, no. If you only need a small array, are on a platform where heap space is slow, and your library's implementation of vector uses heap space, then this feature might very well be better if it existed.
        – Patrick M
        Jul 11 '13 at 1:14



















      -1














      If you know the value at compile time you can do the following:



      template <int X>
      void foo(void)
      {
      int values[X];

      }


      Edit: You can create an a vector that uses a stack allocator (alloca), since the allocator is a template parameter.






      share|improve this answer



















      • 16




        If you know the value at compile time, you don't need a template at all. Just use X directly in your non-template function.
        – Rob Kennedy
        Dec 11 '09 at 15:43






      • 3




        Sometimes the caller knows at compile-time and the callee does not, that's what templates are good for. Of course, in the general case, no one knows X until run-time.
        – Qwertie
        Jul 20 '12 at 0:50












      • You can't use alloca in a STL allocator - allocated memory from alloca will be freed when the stack frame is destroyed - that's when the method who should allocate memory returns.
        – Oliver
        Oct 16 '12 at 12:58



















      -4














      I have a solution that actually worked for me. I did not want to allocate memory because of fragmentation on a routine that needed to run many times. The answer is extremely dangerous, so use it at your own risk, but it takes advantage of assembly to reserve space on the stack. My example below uses a character array (obviously other sized variable would require more memory).



      void varTest(int iSz)
      {
      char *varArray;
      __asm {
      sub esp, iSz // Create space on the stack for the variable array here
      mov varArray, esp // save the end of it to our pointer
      }

      // Use the array called varArray here...

      __asm {
      add esp, iSz // Variable array is no longer accessible after this point
      }
      }


      The dangers here are many but I'll explain a few:
      1. Changing the variable size half way through would kill the stack position
      2. Overstepping the array bounds would destroy other variables and possible code
      3. This does not work in a 64 bit build... need different assembly for that one (but a macro might solve that problem).
      4. Compiler specific (may have trouble moving between compilers). I haven't tried so I really don't know.






      share|improve this answer

















      • 4




        man alloca and save your sanity.
        – Kevin Cox
        Jul 31 '14 at 17:45










      • ... and if you want to roll this yourself, maybe use a RAII class?
        – einpoklum
        May 10 '16 at 23:39










      • You could simply use boost::container::static_vector thou.
        – Viktor Sehr
        May 11 '16 at 7:51










      • This doesn't have equivalents for other compilers which have more raw assembly than MSVC. VC will likely understand that esp changed and will adjust its accesses to stack, but in e.g. GCC you'll just break it completely — at least if you use optimizations and -fomit-frame-pointer in particular.
        – Ruslan
        Jun 22 '16 at 13:29





















      -4














      You need a constant expression to declare an array in C/C++.



      For dynamic size arrays, you need to allocate memory on heap, then manage the liftime of this memory.



      void foo(int n) {
      int* values = new int[n]; //Declare a variable length array
      [...]
      delete values;
      }





      share|improve this answer



















      • 11




        Yes, I know, the question is why is this available in C99 and not C++?
        – Andreas Brinck
        Dec 11 '09 at 10:23










      • C99 [ PDF @ open-std.org/JTC1/sc22/wg14/www/docs/n1401.pdf ] has VLAs (Variable Length Arrays).
        – pmg
        Dec 11 '09 at 10:27










      • A more current C99 draft is http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf (3.7 MB PDF); it includes the original C99 standard plus the three Technical Corrigenda.
        – Keith Thompson
        Sep 18 '11 at 2:01











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1887097%2fwhy-arent-variable-length-arrays-part-of-the-c-standard%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      13 Answers
      13






      active

      oldest

      votes








      13 Answers
      13






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      170














      There recently was a discussion about this kicked off in usenet: Why no VLAs in C++0x.



      I agree with those people that seem to agree that having to create a potential large array on the stack, which usually has only little space available, isn't good. The argument is, if you know the size beforehand, you can use a static array. And if you don't know the size beforehand, you will write unsafe code.



      C99 VLAs could provide a small benefit of being able to create small arrays without wasting space or calling constructors for unused elements, but they will introduce rather large changes to the type system (you need to be able to specify types depending on runtime values - this does not yet exist in current C++, except for new operator type-specifiers, but they are treated specially, so that the runtime-ness doesn't escape the scope of the new operator).



      You can use std::vector, but it is not quite the same, as it uses dynamic memory, and making it use one's own stack-allocator isn't exactly easy (alignment is an issue, too). It also doesn't solve the same problem, because a vector is a resizable container, whereas VLAs are fixed-size. The C++ Dynamic Array proposal is intended to introduce a library based solution, as alternative to a language based VLA. However, it's not going to be part of C++0x, as far as I know.






      share|improve this answer



















      • 16




        +1 and accepted. One comment though, I think the safety argument is a little bit weak since there are so many other ways to cause stack overflows. The safety argument could be used to support the position that you should never use recursion and that you should allocate all objects from the heap.
        – Andreas Brinck
        Dec 11 '09 at 10:46






      • 16




        So you're saying that because there are other ways to cause stack overflows, we might as well encourage more of them?
        – jalf
        Dec 11 '09 at 10:57






      • 2




        @Andreas, agreed about the weakness. But for recursion, it takes a huge number of calls until stack is eaten up, and if that can be the case, people would use iteration. As some people on the usenet thread say, though, this is not an argument against VLAs in all cases, since sometimes you definitely may know an upper bound. But in those cases, from what i see a static array can equally be sufficient, since it would not waste much space anyway (if it would, then you would actually have to ask whether the stack area is large enough again).
        – Johannes Schaub - litb
        Dec 11 '09 at 11:00






      • 10




        Also look at Matt Austern's answer in that thread: The language specification of VLAs would probably considerably more complex for C++, because of the stricter type matches in C++ (example: C allows assigning a T(*) to a T(*)[N] - in C++ this is not allowed, since C++ does not know about "type compatibility" - it requires exact matches), type parameters, exceptions, con- and destructors and stuffs. I'm not sure whether the benefits of VLAs would really pay off all that work. But then, i have never used VLAs in real life, so i probably don't know good use cases for them.
        – Johannes Schaub - litb
        Dec 11 '09 at 11:03






      • 1




        @AHelps: Perhaps what would be best for that would be a type that behaves somewhat like vector but requires a fixed LIFO usage pattern and maintains one or more per-thread statically-allocated buffers which are generally sized according to the largest total allocation the thread has ever used, but which could be explicitly trimmed. A normal "allocation" would in the common case require nothing more than a pointer copy, pointer-from-pointer subtraction, integer comparison, and pointer addition; de-allocation would simply require a pointer copy. Not much slower than a VLA.
        – supercat
        Jun 3 '15 at 16:15
















      170














      There recently was a discussion about this kicked off in usenet: Why no VLAs in C++0x.



      I agree with those people that seem to agree that having to create a potential large array on the stack, which usually has only little space available, isn't good. The argument is, if you know the size beforehand, you can use a static array. And if you don't know the size beforehand, you will write unsafe code.



      C99 VLAs could provide a small benefit of being able to create small arrays without wasting space or calling constructors for unused elements, but they will introduce rather large changes to the type system (you need to be able to specify types depending on runtime values - this does not yet exist in current C++, except for new operator type-specifiers, but they are treated specially, so that the runtime-ness doesn't escape the scope of the new operator).



      You can use std::vector, but it is not quite the same, as it uses dynamic memory, and making it use one's own stack-allocator isn't exactly easy (alignment is an issue, too). It also doesn't solve the same problem, because a vector is a resizable container, whereas VLAs are fixed-size. The C++ Dynamic Array proposal is intended to introduce a library based solution, as alternative to a language based VLA. However, it's not going to be part of C++0x, as far as I know.






      share|improve this answer



















      • 16




        +1 and accepted. One comment though, I think the safety argument is a little bit weak since there are so many other ways to cause stack overflows. The safety argument could be used to support the position that you should never use recursion and that you should allocate all objects from the heap.
        – Andreas Brinck
        Dec 11 '09 at 10:46






      • 16




        So you're saying that because there are other ways to cause stack overflows, we might as well encourage more of them?
        – jalf
        Dec 11 '09 at 10:57






      • 2




        @Andreas, agreed about the weakness. But for recursion, it takes a huge number of calls until stack is eaten up, and if that can be the case, people would use iteration. As some people on the usenet thread say, though, this is not an argument against VLAs in all cases, since sometimes you definitely may know an upper bound. But in those cases, from what i see a static array can equally be sufficient, since it would not waste much space anyway (if it would, then you would actually have to ask whether the stack area is large enough again).
        – Johannes Schaub - litb
        Dec 11 '09 at 11:00






      • 10




        Also look at Matt Austern's answer in that thread: The language specification of VLAs would probably considerably more complex for C++, because of the stricter type matches in C++ (example: C allows assigning a T(*) to a T(*)[N] - in C++ this is not allowed, since C++ does not know about "type compatibility" - it requires exact matches), type parameters, exceptions, con- and destructors and stuffs. I'm not sure whether the benefits of VLAs would really pay off all that work. But then, i have never used VLAs in real life, so i probably don't know good use cases for them.
        – Johannes Schaub - litb
        Dec 11 '09 at 11:03






      • 1




        @AHelps: Perhaps what would be best for that would be a type that behaves somewhat like vector but requires a fixed LIFO usage pattern and maintains one or more per-thread statically-allocated buffers which are generally sized according to the largest total allocation the thread has ever used, but which could be explicitly trimmed. A normal "allocation" would in the common case require nothing more than a pointer copy, pointer-from-pointer subtraction, integer comparison, and pointer addition; de-allocation would simply require a pointer copy. Not much slower than a VLA.
        – supercat
        Jun 3 '15 at 16:15














      170












      170








      170






      There recently was a discussion about this kicked off in usenet: Why no VLAs in C++0x.



      I agree with those people that seem to agree that having to create a potential large array on the stack, which usually has only little space available, isn't good. The argument is, if you know the size beforehand, you can use a static array. And if you don't know the size beforehand, you will write unsafe code.



      C99 VLAs could provide a small benefit of being able to create small arrays without wasting space or calling constructors for unused elements, but they will introduce rather large changes to the type system (you need to be able to specify types depending on runtime values - this does not yet exist in current C++, except for new operator type-specifiers, but they are treated specially, so that the runtime-ness doesn't escape the scope of the new operator).



      You can use std::vector, but it is not quite the same, as it uses dynamic memory, and making it use one's own stack-allocator isn't exactly easy (alignment is an issue, too). It also doesn't solve the same problem, because a vector is a resizable container, whereas VLAs are fixed-size. The C++ Dynamic Array proposal is intended to introduce a library based solution, as alternative to a language based VLA. However, it's not going to be part of C++0x, as far as I know.






      share|improve this answer














      There recently was a discussion about this kicked off in usenet: Why no VLAs in C++0x.



      I agree with those people that seem to agree that having to create a potential large array on the stack, which usually has only little space available, isn't good. The argument is, if you know the size beforehand, you can use a static array. And if you don't know the size beforehand, you will write unsafe code.



      C99 VLAs could provide a small benefit of being able to create small arrays without wasting space or calling constructors for unused elements, but they will introduce rather large changes to the type system (you need to be able to specify types depending on runtime values - this does not yet exist in current C++, except for new operator type-specifiers, but they are treated specially, so that the runtime-ness doesn't escape the scope of the new operator).



      You can use std::vector, but it is not quite the same, as it uses dynamic memory, and making it use one's own stack-allocator isn't exactly easy (alignment is an issue, too). It also doesn't solve the same problem, because a vector is a resizable container, whereas VLAs are fixed-size. The C++ Dynamic Array proposal is intended to introduce a library based solution, as alternative to a language based VLA. However, it's not going to be part of C++0x, as far as I know.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Sep 2 '15 at 21:11









      jchamp

      164311




      164311










      answered Dec 11 '09 at 10:28









      Johannes Schaub - litb

      404k997721107




      404k997721107








      • 16




        +1 and accepted. One comment though, I think the safety argument is a little bit weak since there are so many other ways to cause stack overflows. The safety argument could be used to support the position that you should never use recursion and that you should allocate all objects from the heap.
        – Andreas Brinck
        Dec 11 '09 at 10:46






      • 16




        So you're saying that because there are other ways to cause stack overflows, we might as well encourage more of them?
        – jalf
        Dec 11 '09 at 10:57






      • 2




        @Andreas, agreed about the weakness. But for recursion, it takes a huge number of calls until stack is eaten up, and if that can be the case, people would use iteration. As some people on the usenet thread say, though, this is not an argument against VLAs in all cases, since sometimes you definitely may know an upper bound. But in those cases, from what i see a static array can equally be sufficient, since it would not waste much space anyway (if it would, then you would actually have to ask whether the stack area is large enough again).
        – Johannes Schaub - litb
        Dec 11 '09 at 11:00






      • 10




        Also look at Matt Austern's answer in that thread: The language specification of VLAs would probably considerably more complex for C++, because of the stricter type matches in C++ (example: C allows assigning a T(*) to a T(*)[N] - in C++ this is not allowed, since C++ does not know about "type compatibility" - it requires exact matches), type parameters, exceptions, con- and destructors and stuffs. I'm not sure whether the benefits of VLAs would really pay off all that work. But then, i have never used VLAs in real life, so i probably don't know good use cases for them.
        – Johannes Schaub - litb
        Dec 11 '09 at 11:03






      • 1




        @AHelps: Perhaps what would be best for that would be a type that behaves somewhat like vector but requires a fixed LIFO usage pattern and maintains one or more per-thread statically-allocated buffers which are generally sized according to the largest total allocation the thread has ever used, but which could be explicitly trimmed. A normal "allocation" would in the common case require nothing more than a pointer copy, pointer-from-pointer subtraction, integer comparison, and pointer addition; de-allocation would simply require a pointer copy. Not much slower than a VLA.
        – supercat
        Jun 3 '15 at 16:15














      • 16




        +1 and accepted. One comment though, I think the safety argument is a little bit weak since there are so many other ways to cause stack overflows. The safety argument could be used to support the position that you should never use recursion and that you should allocate all objects from the heap.
        – Andreas Brinck
        Dec 11 '09 at 10:46






      • 16




        So you're saying that because there are other ways to cause stack overflows, we might as well encourage more of them?
        – jalf
        Dec 11 '09 at 10:57






      • 2




        @Andreas, agreed about the weakness. But for recursion, it takes a huge number of calls until stack is eaten up, and if that can be the case, people would use iteration. As some people on the usenet thread say, though, this is not an argument against VLAs in all cases, since sometimes you definitely may know an upper bound. But in those cases, from what i see a static array can equally be sufficient, since it would not waste much space anyway (if it would, then you would actually have to ask whether the stack area is large enough again).
        – Johannes Schaub - litb
        Dec 11 '09 at 11:00






      • 10




        Also look at Matt Austern's answer in that thread: The language specification of VLAs would probably considerably more complex for C++, because of the stricter type matches in C++ (example: C allows assigning a T(*) to a T(*)[N] - in C++ this is not allowed, since C++ does not know about "type compatibility" - it requires exact matches), type parameters, exceptions, con- and destructors and stuffs. I'm not sure whether the benefits of VLAs would really pay off all that work. But then, i have never used VLAs in real life, so i probably don't know good use cases for them.
        – Johannes Schaub - litb
        Dec 11 '09 at 11:03






      • 1




        @AHelps: Perhaps what would be best for that would be a type that behaves somewhat like vector but requires a fixed LIFO usage pattern and maintains one or more per-thread statically-allocated buffers which are generally sized according to the largest total allocation the thread has ever used, but which could be explicitly trimmed. A normal "allocation" would in the common case require nothing more than a pointer copy, pointer-from-pointer subtraction, integer comparison, and pointer addition; de-allocation would simply require a pointer copy. Not much slower than a VLA.
        – supercat
        Jun 3 '15 at 16:15








      16




      16




      +1 and accepted. One comment though, I think the safety argument is a little bit weak since there are so many other ways to cause stack overflows. The safety argument could be used to support the position that you should never use recursion and that you should allocate all objects from the heap.
      – Andreas Brinck
      Dec 11 '09 at 10:46




      +1 and accepted. One comment though, I think the safety argument is a little bit weak since there are so many other ways to cause stack overflows. The safety argument could be used to support the position that you should never use recursion and that you should allocate all objects from the heap.
      – Andreas Brinck
      Dec 11 '09 at 10:46




      16




      16




      So you're saying that because there are other ways to cause stack overflows, we might as well encourage more of them?
      – jalf
      Dec 11 '09 at 10:57




      So you're saying that because there are other ways to cause stack overflows, we might as well encourage more of them?
      – jalf
      Dec 11 '09 at 10:57




      2




      2




      @Andreas, agreed about the weakness. But for recursion, it takes a huge number of calls until stack is eaten up, and if that can be the case, people would use iteration. As some people on the usenet thread say, though, this is not an argument against VLAs in all cases, since sometimes you definitely may know an upper bound. But in those cases, from what i see a static array can equally be sufficient, since it would not waste much space anyway (if it would, then you would actually have to ask whether the stack area is large enough again).
      – Johannes Schaub - litb
      Dec 11 '09 at 11:00




      @Andreas, agreed about the weakness. But for recursion, it takes a huge number of calls until stack is eaten up, and if that can be the case, people would use iteration. As some people on the usenet thread say, though, this is not an argument against VLAs in all cases, since sometimes you definitely may know an upper bound. But in those cases, from what i see a static array can equally be sufficient, since it would not waste much space anyway (if it would, then you would actually have to ask whether the stack area is large enough again).
      – Johannes Schaub - litb
      Dec 11 '09 at 11:00




      10




      10




      Also look at Matt Austern's answer in that thread: The language specification of VLAs would probably considerably more complex for C++, because of the stricter type matches in C++ (example: C allows assigning a T(*) to a T(*)[N] - in C++ this is not allowed, since C++ does not know about "type compatibility" - it requires exact matches), type parameters, exceptions, con- and destructors and stuffs. I'm not sure whether the benefits of VLAs would really pay off all that work. But then, i have never used VLAs in real life, so i probably don't know good use cases for them.
      – Johannes Schaub - litb
      Dec 11 '09 at 11:03




      Also look at Matt Austern's answer in that thread: The language specification of VLAs would probably considerably more complex for C++, because of the stricter type matches in C++ (example: C allows assigning a T(*) to a T(*)[N] - in C++ this is not allowed, since C++ does not know about "type compatibility" - it requires exact matches), type parameters, exceptions, con- and destructors and stuffs. I'm not sure whether the benefits of VLAs would really pay off all that work. But then, i have never used VLAs in real life, so i probably don't know good use cases for them.
      – Johannes Schaub - litb
      Dec 11 '09 at 11:03




      1




      1




      @AHelps: Perhaps what would be best for that would be a type that behaves somewhat like vector but requires a fixed LIFO usage pattern and maintains one or more per-thread statically-allocated buffers which are generally sized according to the largest total allocation the thread has ever used, but which could be explicitly trimmed. A normal "allocation" would in the common case require nothing more than a pointer copy, pointer-from-pointer subtraction, integer comparison, and pointer addition; de-allocation would simply require a pointer copy. Not much slower than a VLA.
      – supercat
      Jun 3 '15 at 16:15




      @AHelps: Perhaps what would be best for that would be a type that behaves somewhat like vector but requires a fixed LIFO usage pattern and maintains one or more per-thread statically-allocated buffers which are generally sized according to the largest total allocation the thread has ever used, but which could be explicitly trimmed. A normal "allocation" would in the common case require nothing more than a pointer copy, pointer-from-pointer subtraction, integer comparison, and pointer addition; de-allocation would simply require a pointer copy. Not much slower than a VLA.
      – supercat
      Jun 3 '15 at 16:15













      171














      (Background: I have some experience implementing C and C++ compilers.)



      Variable-length arrays in C99 were basically a misstep. In order to support VLAs, C99 had to make the following concessions to common sense:




      • sizeof x is no longer always a compile-time constant; the compiler must sometimes generate code to evaluate a sizeof-expression at runtime.


      • Allowing two-dimensional VLAs (int A[x][y]) required a new syntax for declaring functions that take 2D VLAs as parameters: void foo(int n, int A[*]).


      • Less importantly in the C++ world, but extremely important for C's target audience of embedded-systems programmers, declaring a VLA means chomping an arbitrarily large chunk of your stack. This is a guaranteed stack-overflow and crash. (Anytime you declare int A[n], you're implicitly asserting that you have 2GB of stack to spare. After all, if you know "n is definitely less than 1000 here", then you would just declare int A[1000]. Substituting the 32-bit integer n for 1000 is an admission that you have no idea what the behavior of your program ought to be.)



      Okay, so let's move to talking about C++ now. In C++, we have the same strong distinction between "type system" and "value system" that C89 does… but we've really started to rely on it in ways that C has not. For example:



      template<typename T> struct S { ... };
      int A[n];
      S<decltype(A)> s; // equivalently, S<int[n]> s;


      If n weren't a compile-time constant (i.e., if A were of variably modified type), then what on earth would be the type of S? Would S's type also be determined only at runtime?



      What about this:



      template<typename T> bool myfunc(T& t1, T& t2) { ... };
      int A1[n1], A2[n2];
      myfunc(A1, A2);


      The compiler must generate code for some instantiation of myfunc. What should that code look like? How can we statically generate that code, if we don't know the type of A1 at compile time?



      Worse, what if it turns out at runtime that n1 != n2, so that !std::is_same<decltype(A1), decltype(A2)>()? In that case, the call to myfunc shouldn't even compile, because template type deduction should fail! How could we possibly emulate that behavior at runtime?



      Basically, C++ is moving in the direction of pushing more and more decisions into compile-time: template code generation, constexpr function evaluation, and so on. Meanwhile, C99 was busy pushing traditionally compile-time decisions (e.g. sizeof) into the runtime. With this in mind, does it really even make sense to expend any effort trying to integrate C99-style VLAs into C++?



      As every other answerer has already pointed out, C++ provides lots of heap-allocation mechanisms (std::unique_ptr<int> A = new int[n]; or std::vector<int> A(n); being the obvious ones) when you really want to convey the idea "I have no idea how much RAM I might need." And C++ provides a nifty exception-handling model for dealing with the inevitable situation that the amount of RAM you need is greater than the amount of RAM you have. But hopefully this answer gives you a good idea of why C99-style VLAs were not a good fit for C++ — and not really even a good fit for C99. ;)





      For more on the topic, see N3810 "Alternatives for Array Extensions", Bjarne Stroustrup's October 2013 paper on VLAs. Bjarne's POV is very different from mine; N3810 focuses more on finding a good C++ish syntax for the things, and on discouraging the use of raw arrays in C++, whereas I focused more on the implications for metaprogramming and the typesystem. I don't know if he considers the metaprogramming/typesystem implications solved, solvable, or merely uninteresting.






      share|improve this answer



















      • 13




        I agree VLAs were just wrong. The much more widely implemented, and far more useful, alloca() should have been standardized in C99 instead. VLAs are what happens when a standards committee jumps out ahead of implementations, instead of the other way around.
        – MadScientist
        Mar 25 '14 at 11:21






      • 1




        I didn't realise that C allowed VLAs as function parameters. I agree (for what that is worth, as I don't write compilers) that that is rather frightening. VLAs as local variables on the other hand do seem sensible to me. Granted you can overflow your stack, but you have to think about that any time you use local variables, so there is nothing really new there. And the "sizeof" thing - isn't that just the price for a feature?
        – michaeljt
        Sep 26 '14 at 12:56






      • 5




        The variably-modified type system is a great addition IMO, and none of your bullet points violate common sense. (1) the C standard does not distinguish between "compile-time" and "run-time" so this is a non-issue; (2) The * is optional, you can (and should) write int A[n]; (3) You can use the type system without actually declaring any VLAs. For example a function can accept array of variably modified type, and it can be called with non-VLA 2-D arrays of differing dimensions. However you make valid points in the latter part of your post.
        – M.M
        Mar 17 '15 at 4:52






      • 1




        "declaring a VLA means chomping an arbitrarily large chunk of your stack. This is a guaranteed stack-overflow and crash. (Anytime you declare int A[n], you're implicitly asserting that you have 2GB of stack to spare" is empirically false. I just ran a VLA program with a stack far less than 2GB without any stack overflow.
        – Jeff
        May 31 '16 at 20:54






      • 1




        @Jeff: What was the maximum value of n in your test case, and what was the size of your stack? I suggest you try inputting a value for n at least as large as the size of your stack. (And if there's no way for the user to control the value of n in your program, then I suggest you just propagate the maximum value of n straight into the declaration: declare int A[1000] or whatever it is you need. VLAs are only necessary, and only dangerous, when the maximum value of n isn't bounded by any small compile-time constant.)
        – Quuxplusone
        May 31 '16 at 21:41


















      171














      (Background: I have some experience implementing C and C++ compilers.)



      Variable-length arrays in C99 were basically a misstep. In order to support VLAs, C99 had to make the following concessions to common sense:




      • sizeof x is no longer always a compile-time constant; the compiler must sometimes generate code to evaluate a sizeof-expression at runtime.


      • Allowing two-dimensional VLAs (int A[x][y]) required a new syntax for declaring functions that take 2D VLAs as parameters: void foo(int n, int A[*]).


      • Less importantly in the C++ world, but extremely important for C's target audience of embedded-systems programmers, declaring a VLA means chomping an arbitrarily large chunk of your stack. This is a guaranteed stack-overflow and crash. (Anytime you declare int A[n], you're implicitly asserting that you have 2GB of stack to spare. After all, if you know "n is definitely less than 1000 here", then you would just declare int A[1000]. Substituting the 32-bit integer n for 1000 is an admission that you have no idea what the behavior of your program ought to be.)



      Okay, so let's move to talking about C++ now. In C++, we have the same strong distinction between "type system" and "value system" that C89 does… but we've really started to rely on it in ways that C has not. For example:



      template<typename T> struct S { ... };
      int A[n];
      S<decltype(A)> s; // equivalently, S<int[n]> s;


      If n weren't a compile-time constant (i.e., if A were of variably modified type), then what on earth would be the type of S? Would S's type also be determined only at runtime?



      What about this:



      template<typename T> bool myfunc(T& t1, T& t2) { ... };
      int A1[n1], A2[n2];
      myfunc(A1, A2);


      The compiler must generate code for some instantiation of myfunc. What should that code look like? How can we statically generate that code, if we don't know the type of A1 at compile time?



      Worse, what if it turns out at runtime that n1 != n2, so that !std::is_same<decltype(A1), decltype(A2)>()? In that case, the call to myfunc shouldn't even compile, because template type deduction should fail! How could we possibly emulate that behavior at runtime?



      Basically, C++ is moving in the direction of pushing more and more decisions into compile-time: template code generation, constexpr function evaluation, and so on. Meanwhile, C99 was busy pushing traditionally compile-time decisions (e.g. sizeof) into the runtime. With this in mind, does it really even make sense to expend any effort trying to integrate C99-style VLAs into C++?



      As every other answerer has already pointed out, C++ provides lots of heap-allocation mechanisms (std::unique_ptr<int> A = new int[n]; or std::vector<int> A(n); being the obvious ones) when you really want to convey the idea "I have no idea how much RAM I might need." And C++ provides a nifty exception-handling model for dealing with the inevitable situation that the amount of RAM you need is greater than the amount of RAM you have. But hopefully this answer gives you a good idea of why C99-style VLAs were not a good fit for C++ — and not really even a good fit for C99. ;)





      For more on the topic, see N3810 "Alternatives for Array Extensions", Bjarne Stroustrup's October 2013 paper on VLAs. Bjarne's POV is very different from mine; N3810 focuses more on finding a good C++ish syntax for the things, and on discouraging the use of raw arrays in C++, whereas I focused more on the implications for metaprogramming and the typesystem. I don't know if he considers the metaprogramming/typesystem implications solved, solvable, or merely uninteresting.






      share|improve this answer



















      • 13




        I agree VLAs were just wrong. The much more widely implemented, and far more useful, alloca() should have been standardized in C99 instead. VLAs are what happens when a standards committee jumps out ahead of implementations, instead of the other way around.
        – MadScientist
        Mar 25 '14 at 11:21






      • 1




        I didn't realise that C allowed VLAs as function parameters. I agree (for what that is worth, as I don't write compilers) that that is rather frightening. VLAs as local variables on the other hand do seem sensible to me. Granted you can overflow your stack, but you have to think about that any time you use local variables, so there is nothing really new there. And the "sizeof" thing - isn't that just the price for a feature?
        – michaeljt
        Sep 26 '14 at 12:56






      • 5




        The variably-modified type system is a great addition IMO, and none of your bullet points violate common sense. (1) the C standard does not distinguish between "compile-time" and "run-time" so this is a non-issue; (2) The * is optional, you can (and should) write int A[n]; (3) You can use the type system without actually declaring any VLAs. For example a function can accept array of variably modified type, and it can be called with non-VLA 2-D arrays of differing dimensions. However you make valid points in the latter part of your post.
        – M.M
        Mar 17 '15 at 4:52






      • 1




        "declaring a VLA means chomping an arbitrarily large chunk of your stack. This is a guaranteed stack-overflow and crash. (Anytime you declare int A[n], you're implicitly asserting that you have 2GB of stack to spare" is empirically false. I just ran a VLA program with a stack far less than 2GB without any stack overflow.
        – Jeff
        May 31 '16 at 20:54






      • 1




        @Jeff: What was the maximum value of n in your test case, and what was the size of your stack? I suggest you try inputting a value for n at least as large as the size of your stack. (And if there's no way for the user to control the value of n in your program, then I suggest you just propagate the maximum value of n straight into the declaration: declare int A[1000] or whatever it is you need. VLAs are only necessary, and only dangerous, when the maximum value of n isn't bounded by any small compile-time constant.)
        – Quuxplusone
        May 31 '16 at 21:41
















      171












      171








      171






      (Background: I have some experience implementing C and C++ compilers.)



      Variable-length arrays in C99 were basically a misstep. In order to support VLAs, C99 had to make the following concessions to common sense:




      • sizeof x is no longer always a compile-time constant; the compiler must sometimes generate code to evaluate a sizeof-expression at runtime.


      • Allowing two-dimensional VLAs (int A[x][y]) required a new syntax for declaring functions that take 2D VLAs as parameters: void foo(int n, int A[*]).


      • Less importantly in the C++ world, but extremely important for C's target audience of embedded-systems programmers, declaring a VLA means chomping an arbitrarily large chunk of your stack. This is a guaranteed stack-overflow and crash. (Anytime you declare int A[n], you're implicitly asserting that you have 2GB of stack to spare. After all, if you know "n is definitely less than 1000 here", then you would just declare int A[1000]. Substituting the 32-bit integer n for 1000 is an admission that you have no idea what the behavior of your program ought to be.)



      Okay, so let's move to talking about C++ now. In C++, we have the same strong distinction between "type system" and "value system" that C89 does… but we've really started to rely on it in ways that C has not. For example:



      template<typename T> struct S { ... };
      int A[n];
      S<decltype(A)> s; // equivalently, S<int[n]> s;


      If n weren't a compile-time constant (i.e., if A were of variably modified type), then what on earth would be the type of S? Would S's type also be determined only at runtime?



      What about this:



      template<typename T> bool myfunc(T& t1, T& t2) { ... };
      int A1[n1], A2[n2];
      myfunc(A1, A2);


      The compiler must generate code for some instantiation of myfunc. What should that code look like? How can we statically generate that code, if we don't know the type of A1 at compile time?



      Worse, what if it turns out at runtime that n1 != n2, so that !std::is_same<decltype(A1), decltype(A2)>()? In that case, the call to myfunc shouldn't even compile, because template type deduction should fail! How could we possibly emulate that behavior at runtime?



      Basically, C++ is moving in the direction of pushing more and more decisions into compile-time: template code generation, constexpr function evaluation, and so on. Meanwhile, C99 was busy pushing traditionally compile-time decisions (e.g. sizeof) into the runtime. With this in mind, does it really even make sense to expend any effort trying to integrate C99-style VLAs into C++?



      As every other answerer has already pointed out, C++ provides lots of heap-allocation mechanisms (std::unique_ptr<int> A = new int[n]; or std::vector<int> A(n); being the obvious ones) when you really want to convey the idea "I have no idea how much RAM I might need." And C++ provides a nifty exception-handling model for dealing with the inevitable situation that the amount of RAM you need is greater than the amount of RAM you have. But hopefully this answer gives you a good idea of why C99-style VLAs were not a good fit for C++ — and not really even a good fit for C99. ;)





      For more on the topic, see N3810 "Alternatives for Array Extensions", Bjarne Stroustrup's October 2013 paper on VLAs. Bjarne's POV is very different from mine; N3810 focuses more on finding a good C++ish syntax for the things, and on discouraging the use of raw arrays in C++, whereas I focused more on the implications for metaprogramming and the typesystem. I don't know if he considers the metaprogramming/typesystem implications solved, solvable, or merely uninteresting.






      share|improve this answer














      (Background: I have some experience implementing C and C++ compilers.)



      Variable-length arrays in C99 were basically a misstep. In order to support VLAs, C99 had to make the following concessions to common sense:




      • sizeof x is no longer always a compile-time constant; the compiler must sometimes generate code to evaluate a sizeof-expression at runtime.


      • Allowing two-dimensional VLAs (int A[x][y]) required a new syntax for declaring functions that take 2D VLAs as parameters: void foo(int n, int A[*]).


      • Less importantly in the C++ world, but extremely important for C's target audience of embedded-systems programmers, declaring a VLA means chomping an arbitrarily large chunk of your stack. This is a guaranteed stack-overflow and crash. (Anytime you declare int A[n], you're implicitly asserting that you have 2GB of stack to spare. After all, if you know "n is definitely less than 1000 here", then you would just declare int A[1000]. Substituting the 32-bit integer n for 1000 is an admission that you have no idea what the behavior of your program ought to be.)



      Okay, so let's move to talking about C++ now. In C++, we have the same strong distinction between "type system" and "value system" that C89 does… but we've really started to rely on it in ways that C has not. For example:



      template<typename T> struct S { ... };
      int A[n];
      S<decltype(A)> s; // equivalently, S<int[n]> s;


      If n weren't a compile-time constant (i.e., if A were of variably modified type), then what on earth would be the type of S? Would S's type also be determined only at runtime?



      What about this:



      template<typename T> bool myfunc(T& t1, T& t2) { ... };
      int A1[n1], A2[n2];
      myfunc(A1, A2);


      The compiler must generate code for some instantiation of myfunc. What should that code look like? How can we statically generate that code, if we don't know the type of A1 at compile time?



      Worse, what if it turns out at runtime that n1 != n2, so that !std::is_same<decltype(A1), decltype(A2)>()? In that case, the call to myfunc shouldn't even compile, because template type deduction should fail! How could we possibly emulate that behavior at runtime?



      Basically, C++ is moving in the direction of pushing more and more decisions into compile-time: template code generation, constexpr function evaluation, and so on. Meanwhile, C99 was busy pushing traditionally compile-time decisions (e.g. sizeof) into the runtime. With this in mind, does it really even make sense to expend any effort trying to integrate C99-style VLAs into C++?



      As every other answerer has already pointed out, C++ provides lots of heap-allocation mechanisms (std::unique_ptr<int> A = new int[n]; or std::vector<int> A(n); being the obvious ones) when you really want to convey the idea "I have no idea how much RAM I might need." And C++ provides a nifty exception-handling model for dealing with the inevitable situation that the amount of RAM you need is greater than the amount of RAM you have. But hopefully this answer gives you a good idea of why C99-style VLAs were not a good fit for C++ — and not really even a good fit for C99. ;)





      For more on the topic, see N3810 "Alternatives for Array Extensions", Bjarne Stroustrup's October 2013 paper on VLAs. Bjarne's POV is very different from mine; N3810 focuses more on finding a good C++ish syntax for the things, and on discouraging the use of raw arrays in C++, whereas I focused more on the implications for metaprogramming and the typesystem. I don't know if he considers the metaprogramming/typesystem implications solved, solvable, or merely uninteresting.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 17 '15 at 4:44

























      answered Feb 3 '14 at 3:01









      Quuxplusone

      11.5k34293




      11.5k34293








      • 13




        I agree VLAs were just wrong. The much more widely implemented, and far more useful, alloca() should have been standardized in C99 instead. VLAs are what happens when a standards committee jumps out ahead of implementations, instead of the other way around.
        – MadScientist
        Mar 25 '14 at 11:21






      • 1




        I didn't realise that C allowed VLAs as function parameters. I agree (for what that is worth, as I don't write compilers) that that is rather frightening. VLAs as local variables on the other hand do seem sensible to me. Granted you can overflow your stack, but you have to think about that any time you use local variables, so there is nothing really new there. And the "sizeof" thing - isn't that just the price for a feature?
        – michaeljt
        Sep 26 '14 at 12:56






      • 5




        The variably-modified type system is a great addition IMO, and none of your bullet points violate common sense. (1) the C standard does not distinguish between "compile-time" and "run-time" so this is a non-issue; (2) The * is optional, you can (and should) write int A[n]; (3) You can use the type system without actually declaring any VLAs. For example a function can accept array of variably modified type, and it can be called with non-VLA 2-D arrays of differing dimensions. However you make valid points in the latter part of your post.
        – M.M
        Mar 17 '15 at 4:52






      • 1




        "declaring a VLA means chomping an arbitrarily large chunk of your stack. This is a guaranteed stack-overflow and crash. (Anytime you declare int A[n], you're implicitly asserting that you have 2GB of stack to spare" is empirically false. I just ran a VLA program with a stack far less than 2GB without any stack overflow.
        – Jeff
        May 31 '16 at 20:54






      • 1




        @Jeff: What was the maximum value of n in your test case, and what was the size of your stack? I suggest you try inputting a value for n at least as large as the size of your stack. (And if there's no way for the user to control the value of n in your program, then I suggest you just propagate the maximum value of n straight into the declaration: declare int A[1000] or whatever it is you need. VLAs are only necessary, and only dangerous, when the maximum value of n isn't bounded by any small compile-time constant.)
        – Quuxplusone
        May 31 '16 at 21:41
















      • 13




        I agree VLAs were just wrong. The much more widely implemented, and far more useful, alloca() should have been standardized in C99 instead. VLAs are what happens when a standards committee jumps out ahead of implementations, instead of the other way around.
        – MadScientist
        Mar 25 '14 at 11:21






      • 1




        I didn't realise that C allowed VLAs as function parameters. I agree (for what that is worth, as I don't write compilers) that that is rather frightening. VLAs as local variables on the other hand do seem sensible to me. Granted you can overflow your stack, but you have to think about that any time you use local variables, so there is nothing really new there. And the "sizeof" thing - isn't that just the price for a feature?
        – michaeljt
        Sep 26 '14 at 12:56






      • 5




        The variably-modified type system is a great addition IMO, and none of your bullet points violate common sense. (1) the C standard does not distinguish between "compile-time" and "run-time" so this is a non-issue; (2) The * is optional, you can (and should) write int A[n]; (3) You can use the type system without actually declaring any VLAs. For example a function can accept array of variably modified type, and it can be called with non-VLA 2-D arrays of differing dimensions. However you make valid points in the latter part of your post.
        – M.M
        Mar 17 '15 at 4:52






      • 1




        "declaring a VLA means chomping an arbitrarily large chunk of your stack. This is a guaranteed stack-overflow and crash. (Anytime you declare int A[n], you're implicitly asserting that you have 2GB of stack to spare" is empirically false. I just ran a VLA program with a stack far less than 2GB without any stack overflow.
        – Jeff
        May 31 '16 at 20:54






      • 1




        @Jeff: What was the maximum value of n in your test case, and what was the size of your stack? I suggest you try inputting a value for n at least as large as the size of your stack. (And if there's no way for the user to control the value of n in your program, then I suggest you just propagate the maximum value of n straight into the declaration: declare int A[1000] or whatever it is you need. VLAs are only necessary, and only dangerous, when the maximum value of n isn't bounded by any small compile-time constant.)
        – Quuxplusone
        May 31 '16 at 21:41










      13




      13




      I agree VLAs were just wrong. The much more widely implemented, and far more useful, alloca() should have been standardized in C99 instead. VLAs are what happens when a standards committee jumps out ahead of implementations, instead of the other way around.
      – MadScientist
      Mar 25 '14 at 11:21




      I agree VLAs were just wrong. The much more widely implemented, and far more useful, alloca() should have been standardized in C99 instead. VLAs are what happens when a standards committee jumps out ahead of implementations, instead of the other way around.
      – MadScientist
      Mar 25 '14 at 11:21




      1




      1




      I didn't realise that C allowed VLAs as function parameters. I agree (for what that is worth, as I don't write compilers) that that is rather frightening. VLAs as local variables on the other hand do seem sensible to me. Granted you can overflow your stack, but you have to think about that any time you use local variables, so there is nothing really new there. And the "sizeof" thing - isn't that just the price for a feature?
      – michaeljt
      Sep 26 '14 at 12:56




      I didn't realise that C allowed VLAs as function parameters. I agree (for what that is worth, as I don't write compilers) that that is rather frightening. VLAs as local variables on the other hand do seem sensible to me. Granted you can overflow your stack, but you have to think about that any time you use local variables, so there is nothing really new there. And the "sizeof" thing - isn't that just the price for a feature?
      – michaeljt
      Sep 26 '14 at 12:56




      5




      5




      The variably-modified type system is a great addition IMO, and none of your bullet points violate common sense. (1) the C standard does not distinguish between "compile-time" and "run-time" so this is a non-issue; (2) The * is optional, you can (and should) write int A[n]; (3) You can use the type system without actually declaring any VLAs. For example a function can accept array of variably modified type, and it can be called with non-VLA 2-D arrays of differing dimensions. However you make valid points in the latter part of your post.
      – M.M
      Mar 17 '15 at 4:52




      The variably-modified type system is a great addition IMO, and none of your bullet points violate common sense. (1) the C standard does not distinguish between "compile-time" and "run-time" so this is a non-issue; (2) The * is optional, you can (and should) write int A[n]; (3) You can use the type system without actually declaring any VLAs. For example a function can accept array of variably modified type, and it can be called with non-VLA 2-D arrays of differing dimensions. However you make valid points in the latter part of your post.
      – M.M
      Mar 17 '15 at 4:52




      1




      1




      "declaring a VLA means chomping an arbitrarily large chunk of your stack. This is a guaranteed stack-overflow and crash. (Anytime you declare int A[n], you're implicitly asserting that you have 2GB of stack to spare" is empirically false. I just ran a VLA program with a stack far less than 2GB without any stack overflow.
      – Jeff
      May 31 '16 at 20:54




      "declaring a VLA means chomping an arbitrarily large chunk of your stack. This is a guaranteed stack-overflow and crash. (Anytime you declare int A[n], you're implicitly asserting that you have 2GB of stack to spare" is empirically false. I just ran a VLA program with a stack far less than 2GB without any stack overflow.
      – Jeff
      May 31 '16 at 20:54




      1




      1




      @Jeff: What was the maximum value of n in your test case, and what was the size of your stack? I suggest you try inputting a value for n at least as large as the size of your stack. (And if there's no way for the user to control the value of n in your program, then I suggest you just propagate the maximum value of n straight into the declaration: declare int A[1000] or whatever it is you need. VLAs are only necessary, and only dangerous, when the maximum value of n isn't bounded by any small compile-time constant.)
      – Quuxplusone
      May 31 '16 at 21:41






      @Jeff: What was the maximum value of n in your test case, and what was the size of your stack? I suggest you try inputting a value for n at least as large as the size of your stack. (And if there's no way for the user to control the value of n in your program, then I suggest you just propagate the maximum value of n straight into the declaration: declare int A[1000] or whatever it is you need. VLAs are only necessary, and only dangerous, when the maximum value of n isn't bounded by any small compile-time constant.)
      – Quuxplusone
      May 31 '16 at 21:41













      25














      You could always use alloca() to allocate memory on the stack at runtime, if you wished:



      void foo (int n)
      {
      int *values = (int *)alloca(sizeof(int) * n);
      }


      Being allocated on the stack implies that it will automatically be freed when the stack unwinds.



      Quick note: As mentioned in the Mac OS X man page for alloca(3), "The alloca() function is machine and compiler dependent; its use is dis-couraged." Just so you know.






      share|improve this answer

















      • 2




        Also, the scope for alloca() is the entire function, not just the block of code containing the variable. So using it inside of a loop it will continuously increase the stack. A VLA does not have this problem.
        – sashoalm
        Jul 1 '16 at 11:49






      • 2




        However, VLAs having the scope of the enclosing block means they are significantly less useful than alloca() with the scope of the entire function. Consider: if (!p) { p = alloca(strlen(foo)+1); strcpy(p, foo); } This cannot be done with VLAs, precisely because of their block scope.
        – MadScientist
        Jan 12 '17 at 16:35










      • That does not answer OP's why question. Moreover, this is a C-like solution, and not really C++-ish.
        – Adrian W
        Jun 25 at 15:37


















      25














      You could always use alloca() to allocate memory on the stack at runtime, if you wished:



      void foo (int n)
      {
      int *values = (int *)alloca(sizeof(int) * n);
      }


      Being allocated on the stack implies that it will automatically be freed when the stack unwinds.



      Quick note: As mentioned in the Mac OS X man page for alloca(3), "The alloca() function is machine and compiler dependent; its use is dis-couraged." Just so you know.






      share|improve this answer

















      • 2




        Also, the scope for alloca() is the entire function, not just the block of code containing the variable. So using it inside of a loop it will continuously increase the stack. A VLA does not have this problem.
        – sashoalm
        Jul 1 '16 at 11:49






      • 2




        However, VLAs having the scope of the enclosing block means they are significantly less useful than alloca() with the scope of the entire function. Consider: if (!p) { p = alloca(strlen(foo)+1); strcpy(p, foo); } This cannot be done with VLAs, precisely because of their block scope.
        – MadScientist
        Jan 12 '17 at 16:35










      • That does not answer OP's why question. Moreover, this is a C-like solution, and not really C++-ish.
        – Adrian W
        Jun 25 at 15:37
















      25












      25








      25






      You could always use alloca() to allocate memory on the stack at runtime, if you wished:



      void foo (int n)
      {
      int *values = (int *)alloca(sizeof(int) * n);
      }


      Being allocated on the stack implies that it will automatically be freed when the stack unwinds.



      Quick note: As mentioned in the Mac OS X man page for alloca(3), "The alloca() function is machine and compiler dependent; its use is dis-couraged." Just so you know.






      share|improve this answer












      You could always use alloca() to allocate memory on the stack at runtime, if you wished:



      void foo (int n)
      {
      int *values = (int *)alloca(sizeof(int) * n);
      }


      Being allocated on the stack implies that it will automatically be freed when the stack unwinds.



      Quick note: As mentioned in the Mac OS X man page for alloca(3), "The alloca() function is machine and compiler dependent; its use is dis-couraged." Just so you know.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Dec 11 '09 at 10:31









      PfhorSlayer

      1,113813




      1,113813








      • 2




        Also, the scope for alloca() is the entire function, not just the block of code containing the variable. So using it inside of a loop it will continuously increase the stack. A VLA does not have this problem.
        – sashoalm
        Jul 1 '16 at 11:49






      • 2




        However, VLAs having the scope of the enclosing block means they are significantly less useful than alloca() with the scope of the entire function. Consider: if (!p) { p = alloca(strlen(foo)+1); strcpy(p, foo); } This cannot be done with VLAs, precisely because of their block scope.
        – MadScientist
        Jan 12 '17 at 16:35










      • That does not answer OP's why question. Moreover, this is a C-like solution, and not really C++-ish.
        – Adrian W
        Jun 25 at 15:37
















      • 2




        Also, the scope for alloca() is the entire function, not just the block of code containing the variable. So using it inside of a loop it will continuously increase the stack. A VLA does not have this problem.
        – sashoalm
        Jul 1 '16 at 11:49






      • 2




        However, VLAs having the scope of the enclosing block means they are significantly less useful than alloca() with the scope of the entire function. Consider: if (!p) { p = alloca(strlen(foo)+1); strcpy(p, foo); } This cannot be done with VLAs, precisely because of their block scope.
        – MadScientist
        Jan 12 '17 at 16:35










      • That does not answer OP's why question. Moreover, this is a C-like solution, and not really C++-ish.
        – Adrian W
        Jun 25 at 15:37










      2




      2




      Also, the scope for alloca() is the entire function, not just the block of code containing the variable. So using it inside of a loop it will continuously increase the stack. A VLA does not have this problem.
      – sashoalm
      Jul 1 '16 at 11:49




      Also, the scope for alloca() is the entire function, not just the block of code containing the variable. So using it inside of a loop it will continuously increase the stack. A VLA does not have this problem.
      – sashoalm
      Jul 1 '16 at 11:49




      2




      2




      However, VLAs having the scope of the enclosing block means they are significantly less useful than alloca() with the scope of the entire function. Consider: if (!p) { p = alloca(strlen(foo)+1); strcpy(p, foo); } This cannot be done with VLAs, precisely because of their block scope.
      – MadScientist
      Jan 12 '17 at 16:35




      However, VLAs having the scope of the enclosing block means they are significantly less useful than alloca() with the scope of the entire function. Consider: if (!p) { p = alloca(strlen(foo)+1); strcpy(p, foo); } This cannot be done with VLAs, precisely because of their block scope.
      – MadScientist
      Jan 12 '17 at 16:35












      That does not answer OP's why question. Moreover, this is a C-like solution, and not really C++-ish.
      – Adrian W
      Jun 25 at 15:37






      That does not answer OP's why question. Moreover, this is a C-like solution, and not really C++-ish.
      – Adrian W
      Jun 25 at 15:37













      10














      There are situations where allocating heap memory is very expensive compared to the operations performed. An example is matrix math. If you work with smallish matrices say 5 to 10 elements and do a lot of arithmetics the malloc overhead will be really significant. At the same time making the size a compile time constant does seem very wasteful and inflexible.



      I think that C++ is so unsafe in itself that the argument to "try to not add more unsafe features" is not very strong. On the other hand, as C++ is arguably the most runtime efficient programming language features which makes it more so are always useful: People who write performance critical programs will to a large extent use C++, and they need as much performance as possible. Moving stuff from heap to stack is one such possibility. Reducing the number of heap blocks is another. Allowing VLAs as object members would one way to achieve this. I'm working on such a suggestion. It is a bit complicated to implement, admittedly, but it seems quite doable.






      share|improve this answer


























        10














        There are situations where allocating heap memory is very expensive compared to the operations performed. An example is matrix math. If you work with smallish matrices say 5 to 10 elements and do a lot of arithmetics the malloc overhead will be really significant. At the same time making the size a compile time constant does seem very wasteful and inflexible.



        I think that C++ is so unsafe in itself that the argument to "try to not add more unsafe features" is not very strong. On the other hand, as C++ is arguably the most runtime efficient programming language features which makes it more so are always useful: People who write performance critical programs will to a large extent use C++, and they need as much performance as possible. Moving stuff from heap to stack is one such possibility. Reducing the number of heap blocks is another. Allowing VLAs as object members would one way to achieve this. I'm working on such a suggestion. It is a bit complicated to implement, admittedly, but it seems quite doable.






        share|improve this answer
























          10












          10








          10






          There are situations where allocating heap memory is very expensive compared to the operations performed. An example is matrix math. If you work with smallish matrices say 5 to 10 elements and do a lot of arithmetics the malloc overhead will be really significant. At the same time making the size a compile time constant does seem very wasteful and inflexible.



          I think that C++ is so unsafe in itself that the argument to "try to not add more unsafe features" is not very strong. On the other hand, as C++ is arguably the most runtime efficient programming language features which makes it more so are always useful: People who write performance critical programs will to a large extent use C++, and they need as much performance as possible. Moving stuff from heap to stack is one such possibility. Reducing the number of heap blocks is another. Allowing VLAs as object members would one way to achieve this. I'm working on such a suggestion. It is a bit complicated to implement, admittedly, but it seems quite doable.






          share|improve this answer












          There are situations where allocating heap memory is very expensive compared to the operations performed. An example is matrix math. If you work with smallish matrices say 5 to 10 elements and do a lot of arithmetics the malloc overhead will be really significant. At the same time making the size a compile time constant does seem very wasteful and inflexible.



          I think that C++ is so unsafe in itself that the argument to "try to not add more unsafe features" is not very strong. On the other hand, as C++ is arguably the most runtime efficient programming language features which makes it more so are always useful: People who write performance critical programs will to a large extent use C++, and they need as much performance as possible. Moving stuff from heap to stack is one such possibility. Reducing the number of heap blocks is another. Allowing VLAs as object members would one way to achieve this. I'm working on such a suggestion. It is a bit complicated to implement, admittedly, but it seems quite doable.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 22 '11 at 19:33









          Bengt Gustafsson

          18928




          18928























              10














              In my own work, I've realized that every time I've wanted something like variable-length automatic arrays or alloca(), I didn't really care that the memory was physically located on the cpu stack, just that it came from some stack allocator that didn't incur slow trips to the general heap. So I have a per-thread object that owns some memory from which it can push/pop variable sized buffers. On some platforms I allow this to grow via mmu. Other platforms have a fixed size (usually accompanied by a fixed size cpu stack as well because no mmu). One platform I work with (a handheld game console) has precious little cpu stack anyway because it resides in scarce, fast memory.



              I'm not saying that pushing variable-sized buffers onto the cpu stack is never needed. Honestly I was surprised back when I discovered this wasn't standard, as it certainly seems like the concept fits into the language well enough. For me though, the requirements "variable size" and "must be physically located on the cpu stack" have never come up together. It's been about speed, so I made my own sort of "parallel stack for data buffers".






              share|improve this answer


























                10














                In my own work, I've realized that every time I've wanted something like variable-length automatic arrays or alloca(), I didn't really care that the memory was physically located on the cpu stack, just that it came from some stack allocator that didn't incur slow trips to the general heap. So I have a per-thread object that owns some memory from which it can push/pop variable sized buffers. On some platforms I allow this to grow via mmu. Other platforms have a fixed size (usually accompanied by a fixed size cpu stack as well because no mmu). One platform I work with (a handheld game console) has precious little cpu stack anyway because it resides in scarce, fast memory.



                I'm not saying that pushing variable-sized buffers onto the cpu stack is never needed. Honestly I was surprised back when I discovered this wasn't standard, as it certainly seems like the concept fits into the language well enough. For me though, the requirements "variable size" and "must be physically located on the cpu stack" have never come up together. It's been about speed, so I made my own sort of "parallel stack for data buffers".






                share|improve this answer
























                  10












                  10








                  10






                  In my own work, I've realized that every time I've wanted something like variable-length automatic arrays or alloca(), I didn't really care that the memory was physically located on the cpu stack, just that it came from some stack allocator that didn't incur slow trips to the general heap. So I have a per-thread object that owns some memory from which it can push/pop variable sized buffers. On some platforms I allow this to grow via mmu. Other platforms have a fixed size (usually accompanied by a fixed size cpu stack as well because no mmu). One platform I work with (a handheld game console) has precious little cpu stack anyway because it resides in scarce, fast memory.



                  I'm not saying that pushing variable-sized buffers onto the cpu stack is never needed. Honestly I was surprised back when I discovered this wasn't standard, as it certainly seems like the concept fits into the language well enough. For me though, the requirements "variable size" and "must be physically located on the cpu stack" have never come up together. It's been about speed, so I made my own sort of "parallel stack for data buffers".






                  share|improve this answer












                  In my own work, I've realized that every time I've wanted something like variable-length automatic arrays or alloca(), I didn't really care that the memory was physically located on the cpu stack, just that it came from some stack allocator that didn't incur slow trips to the general heap. So I have a per-thread object that owns some memory from which it can push/pop variable sized buffers. On some platforms I allow this to grow via mmu. Other platforms have a fixed size (usually accompanied by a fixed size cpu stack as well because no mmu). One platform I work with (a handheld game console) has precious little cpu stack anyway because it resides in scarce, fast memory.



                  I'm not saying that pushing variable-sized buffers onto the cpu stack is never needed. Honestly I was surprised back when I discovered this wasn't standard, as it certainly seems like the concept fits into the language well enough. For me though, the requirements "variable size" and "must be physically located on the cpu stack" have never come up together. It's been about speed, so I made my own sort of "parallel stack for data buffers".







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 21 '13 at 17:05









                  Eric

                  10112




                  10112























                      9














                      Seems it will be available in C++14:



                      https://en.wikipedia.org/wiki/C%2B%2B14#Runtime-sized_one_dimensional_arrays



                      Update: It did not make it into C++14.






                      share|improve this answer























                      • interesting. Herb Sutter discusses it here under Dynamic Arrays: isocpp.org/blog/2013/04/trip-report-iso-c-spring-2013-meeting (this is the reference for the wikipedia information)
                        – Default
                        Aug 13 '13 at 11:12






                      • 1




                        "Run-time sized arrays and dynarray have been moved to the Array Extensions technical specification" wrote 78.86.152.103 on Wikipedia on 18 January 2014‎: en.wikipedia.org/w/…
                        – strager
                        Feb 24 '14 at 3:26








                      • 9




                        Wikipedia isn't a normative reference :) This proposal did not make it into C++14.
                        – M.M
                        Nov 4 '14 at 2:37










                      • @M.M Will it ever?
                        – this
                        Nov 28 '15 at 19:43






                      • 1




                        @ViktorSehr: What's the status of this w.r.t. C++17?
                        – einpoklum
                        May 10 '16 at 23:37
















                      9














                      Seems it will be available in C++14:



                      https://en.wikipedia.org/wiki/C%2B%2B14#Runtime-sized_one_dimensional_arrays



                      Update: It did not make it into C++14.






                      share|improve this answer























                      • interesting. Herb Sutter discusses it here under Dynamic Arrays: isocpp.org/blog/2013/04/trip-report-iso-c-spring-2013-meeting (this is the reference for the wikipedia information)
                        – Default
                        Aug 13 '13 at 11:12






                      • 1




                        "Run-time sized arrays and dynarray have been moved to the Array Extensions technical specification" wrote 78.86.152.103 on Wikipedia on 18 January 2014‎: en.wikipedia.org/w/…
                        – strager
                        Feb 24 '14 at 3:26








                      • 9




                        Wikipedia isn't a normative reference :) This proposal did not make it into C++14.
                        – M.M
                        Nov 4 '14 at 2:37










                      • @M.M Will it ever?
                        – this
                        Nov 28 '15 at 19:43






                      • 1




                        @ViktorSehr: What's the status of this w.r.t. C++17?
                        – einpoklum
                        May 10 '16 at 23:37














                      9












                      9








                      9






                      Seems it will be available in C++14:



                      https://en.wikipedia.org/wiki/C%2B%2B14#Runtime-sized_one_dimensional_arrays



                      Update: It did not make it into C++14.






                      share|improve this answer














                      Seems it will be available in C++14:



                      https://en.wikipedia.org/wiki/C%2B%2B14#Runtime-sized_one_dimensional_arrays



                      Update: It did not make it into C++14.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 11 '16 at 7:49

























                      answered Aug 13 '13 at 10:40









                      Viktor Sehr

                      8,54124060




                      8,54124060












                      • interesting. Herb Sutter discusses it here under Dynamic Arrays: isocpp.org/blog/2013/04/trip-report-iso-c-spring-2013-meeting (this is the reference for the wikipedia information)
                        – Default
                        Aug 13 '13 at 11:12






                      • 1




                        "Run-time sized arrays and dynarray have been moved to the Array Extensions technical specification" wrote 78.86.152.103 on Wikipedia on 18 January 2014‎: en.wikipedia.org/w/…
                        – strager
                        Feb 24 '14 at 3:26








                      • 9




                        Wikipedia isn't a normative reference :) This proposal did not make it into C++14.
                        – M.M
                        Nov 4 '14 at 2:37










                      • @M.M Will it ever?
                        – this
                        Nov 28 '15 at 19:43






                      • 1




                        @ViktorSehr: What's the status of this w.r.t. C++17?
                        – einpoklum
                        May 10 '16 at 23:37


















                      • interesting. Herb Sutter discusses it here under Dynamic Arrays: isocpp.org/blog/2013/04/trip-report-iso-c-spring-2013-meeting (this is the reference for the wikipedia information)
                        – Default
                        Aug 13 '13 at 11:12






                      • 1




                        "Run-time sized arrays and dynarray have been moved to the Array Extensions technical specification" wrote 78.86.152.103 on Wikipedia on 18 January 2014‎: en.wikipedia.org/w/…
                        – strager
                        Feb 24 '14 at 3:26








                      • 9




                        Wikipedia isn't a normative reference :) This proposal did not make it into C++14.
                        – M.M
                        Nov 4 '14 at 2:37










                      • @M.M Will it ever?
                        – this
                        Nov 28 '15 at 19:43






                      • 1




                        @ViktorSehr: What's the status of this w.r.t. C++17?
                        – einpoklum
                        May 10 '16 at 23:37
















                      interesting. Herb Sutter discusses it here under Dynamic Arrays: isocpp.org/blog/2013/04/trip-report-iso-c-spring-2013-meeting (this is the reference for the wikipedia information)
                      – Default
                      Aug 13 '13 at 11:12




                      interesting. Herb Sutter discusses it here under Dynamic Arrays: isocpp.org/blog/2013/04/trip-report-iso-c-spring-2013-meeting (this is the reference for the wikipedia information)
                      – Default
                      Aug 13 '13 at 11:12




                      1




                      1




                      "Run-time sized arrays and dynarray have been moved to the Array Extensions technical specification" wrote 78.86.152.103 on Wikipedia on 18 January 2014‎: en.wikipedia.org/w/…
                      – strager
                      Feb 24 '14 at 3:26






                      "Run-time sized arrays and dynarray have been moved to the Array Extensions technical specification" wrote 78.86.152.103 on Wikipedia on 18 January 2014‎: en.wikipedia.org/w/…
                      – strager
                      Feb 24 '14 at 3:26






                      9




                      9




                      Wikipedia isn't a normative reference :) This proposal did not make it into C++14.
                      – M.M
                      Nov 4 '14 at 2:37




                      Wikipedia isn't a normative reference :) This proposal did not make it into C++14.
                      – M.M
                      Nov 4 '14 at 2:37












                      @M.M Will it ever?
                      – this
                      Nov 28 '15 at 19:43




                      @M.M Will it ever?
                      – this
                      Nov 28 '15 at 19:43




                      1




                      1




                      @ViktorSehr: What's the status of this w.r.t. C++17?
                      – einpoklum
                      May 10 '16 at 23:37




                      @ViktorSehr: What's the status of this w.r.t. C++17?
                      – einpoklum
                      May 10 '16 at 23:37











                      6














                      This was considered for inclusion in C++/1x, but was dropped (this is a correction to what I said earlier).



                      It would be less useful in C++ anyway since we already have std::vector to fill this role.






                      share|improve this answer



















                      • 40




                        No, we don't, std::vector doesn't allocate data on the stack. :)
                        – Kos
                        Aug 10 '11 at 16:34






                      • 6




                        "the stack" is an implementation detail; the compiler may allocate memory from anywhere so long as the guarantees about object lifetime are met.
                        – M.M
                        Mar 16 '14 at 21:20






                      • 1




                        @M.M: Fair enough, but in practice we still can't use std::vector instead of, say, alloca().
                        – einpoklum
                        May 10 '16 at 23:36










                      • @einpoklum in terms of getting correct output for your program , you can. Performance is a quality-of-implementation issue
                        – M.M
                        May 10 '16 at 23:50
















                      6














                      This was considered for inclusion in C++/1x, but was dropped (this is a correction to what I said earlier).



                      It would be less useful in C++ anyway since we already have std::vector to fill this role.






                      share|improve this answer



















                      • 40




                        No, we don't, std::vector doesn't allocate data on the stack. :)
                        – Kos
                        Aug 10 '11 at 16:34






                      • 6




                        "the stack" is an implementation detail; the compiler may allocate memory from anywhere so long as the guarantees about object lifetime are met.
                        – M.M
                        Mar 16 '14 at 21:20






                      • 1




                        @M.M: Fair enough, but in practice we still can't use std::vector instead of, say, alloca().
                        – einpoklum
                        May 10 '16 at 23:36










                      • @einpoklum in terms of getting correct output for your program , you can. Performance is a quality-of-implementation issue
                        – M.M
                        May 10 '16 at 23:50














                      6












                      6








                      6






                      This was considered for inclusion in C++/1x, but was dropped (this is a correction to what I said earlier).



                      It would be less useful in C++ anyway since we already have std::vector to fill this role.






                      share|improve this answer














                      This was considered for inclusion in C++/1x, but was dropped (this is a correction to what I said earlier).



                      It would be less useful in C++ anyway since we already have std::vector to fill this role.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jun 10 '13 at 15:31









                      artless noise

                      15.3k44679




                      15.3k44679










                      answered Dec 11 '09 at 10:26









                      philsquared

                      17.5k106094




                      17.5k106094








                      • 40




                        No, we don't, std::vector doesn't allocate data on the stack. :)
                        – Kos
                        Aug 10 '11 at 16:34






                      • 6




                        "the stack" is an implementation detail; the compiler may allocate memory from anywhere so long as the guarantees about object lifetime are met.
                        – M.M
                        Mar 16 '14 at 21:20






                      • 1




                        @M.M: Fair enough, but in practice we still can't use std::vector instead of, say, alloca().
                        – einpoklum
                        May 10 '16 at 23:36










                      • @einpoklum in terms of getting correct output for your program , you can. Performance is a quality-of-implementation issue
                        – M.M
                        May 10 '16 at 23:50














                      • 40




                        No, we don't, std::vector doesn't allocate data on the stack. :)
                        – Kos
                        Aug 10 '11 at 16:34






                      • 6




                        "the stack" is an implementation detail; the compiler may allocate memory from anywhere so long as the guarantees about object lifetime are met.
                        – M.M
                        Mar 16 '14 at 21:20






                      • 1




                        @M.M: Fair enough, but in practice we still can't use std::vector instead of, say, alloca().
                        – einpoklum
                        May 10 '16 at 23:36










                      • @einpoklum in terms of getting correct output for your program , you can. Performance is a quality-of-implementation issue
                        – M.M
                        May 10 '16 at 23:50








                      40




                      40




                      No, we don't, std::vector doesn't allocate data on the stack. :)
                      – Kos
                      Aug 10 '11 at 16:34




                      No, we don't, std::vector doesn't allocate data on the stack. :)
                      – Kos
                      Aug 10 '11 at 16:34




                      6




                      6




                      "the stack" is an implementation detail; the compiler may allocate memory from anywhere so long as the guarantees about object lifetime are met.
                      – M.M
                      Mar 16 '14 at 21:20




                      "the stack" is an implementation detail; the compiler may allocate memory from anywhere so long as the guarantees about object lifetime are met.
                      – M.M
                      Mar 16 '14 at 21:20




                      1




                      1




                      @M.M: Fair enough, but in practice we still can't use std::vector instead of, say, alloca().
                      – einpoklum
                      May 10 '16 at 23:36




                      @M.M: Fair enough, but in practice we still can't use std::vector instead of, say, alloca().
                      – einpoklum
                      May 10 '16 at 23:36












                      @einpoklum in terms of getting correct output for your program , you can. Performance is a quality-of-implementation issue
                      – M.M
                      May 10 '16 at 23:50




                      @einpoklum in terms of getting correct output for your program , you can. Performance is a quality-of-implementation issue
                      – M.M
                      May 10 '16 at 23:50











                      2














                      Use std::vector for this. For example:



                      std::vector<int> values;
                      values.resize(n);


                      The memory will be allocated on the heap, but this holds only a small performance drawback. Furthermore, it is wise not to allocate large datablocks on the stack, as it is rather limited in size.






                      share|improve this answer



















                      • 3




                        A major application for variable length arrays is evaluation of arbitrary degree polynomials. In that case, your "small performance drawback" means "the code runs five times slower in typical cases." That's not small.
                        – AHelps
                        Apr 10 '15 at 18:26
















                      2














                      Use std::vector for this. For example:



                      std::vector<int> values;
                      values.resize(n);


                      The memory will be allocated on the heap, but this holds only a small performance drawback. Furthermore, it is wise not to allocate large datablocks on the stack, as it is rather limited in size.






                      share|improve this answer



















                      • 3




                        A major application for variable length arrays is evaluation of arbitrary degree polynomials. In that case, your "small performance drawback" means "the code runs five times slower in typical cases." That's not small.
                        – AHelps
                        Apr 10 '15 at 18:26














                      2












                      2








                      2






                      Use std::vector for this. For example:



                      std::vector<int> values;
                      values.resize(n);


                      The memory will be allocated on the heap, but this holds only a small performance drawback. Furthermore, it is wise not to allocate large datablocks on the stack, as it is rather limited in size.






                      share|improve this answer














                      Use std::vector for this. For example:



                      std::vector<int> values;
                      values.resize(n);


                      The memory will be allocated on the heap, but this holds only a small performance drawback. Furthermore, it is wise not to allocate large datablocks on the stack, as it is rather limited in size.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 11 '09 at 14:20

























                      answered Dec 11 '09 at 10:22









                      Dimitri C.

                      11k187398




                      11k187398








                      • 3




                        A major application for variable length arrays is evaluation of arbitrary degree polynomials. In that case, your "small performance drawback" means "the code runs five times slower in typical cases." That's not small.
                        – AHelps
                        Apr 10 '15 at 18:26














                      • 3




                        A major application for variable length arrays is evaluation of arbitrary degree polynomials. In that case, your "small performance drawback" means "the code runs five times slower in typical cases." That's not small.
                        – AHelps
                        Apr 10 '15 at 18:26








                      3




                      3




                      A major application for variable length arrays is evaluation of arbitrary degree polynomials. In that case, your "small performance drawback" means "the code runs five times slower in typical cases." That's not small.
                      – AHelps
                      Apr 10 '15 at 18:26




                      A major application for variable length arrays is evaluation of arbitrary degree polynomials. In that case, your "small performance drawback" means "the code runs five times slower in typical cases." That's not small.
                      – AHelps
                      Apr 10 '15 at 18:26











                      1














                      C99 allows VLA. And it puts some restrictions on how to declare VLA. For details, refer to 6.7.5.2 of the standard. C++ disallows VLA. But g++ allows it.






                      share|improve this answer





















                      • Can you provide a link to the standard paragraph that you are pointing ?
                        – Vincent
                        May 26 '17 at 11:10
















                      1














                      C99 allows VLA. And it puts some restrictions on how to declare VLA. For details, refer to 6.7.5.2 of the standard. C++ disallows VLA. But g++ allows it.






                      share|improve this answer





















                      • Can you provide a link to the standard paragraph that you are pointing ?
                        – Vincent
                        May 26 '17 at 11:10














                      1












                      1








                      1






                      C99 allows VLA. And it puts some restrictions on how to declare VLA. For details, refer to 6.7.5.2 of the standard. C++ disallows VLA. But g++ allows it.






                      share|improve this answer












                      C99 allows VLA. And it puts some restrictions on how to declare VLA. For details, refer to 6.7.5.2 of the standard. C++ disallows VLA. But g++ allows it.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jul 31 '12 at 5:56









                      Jingguo Yao

                      2,64412936




                      2,64412936












                      • Can you provide a link to the standard paragraph that you are pointing ?
                        – Vincent
                        May 26 '17 at 11:10


















                      • Can you provide a link to the standard paragraph that you are pointing ?
                        – Vincent
                        May 26 '17 at 11:10
















                      Can you provide a link to the standard paragraph that you are pointing ?
                      – Vincent
                      May 26 '17 at 11:10




                      Can you provide a link to the standard paragraph that you are pointing ?
                      – Vincent
                      May 26 '17 at 11:10











                      0














                      Arrays like this are part of C99, but not part of standard C++. as others have said, a vector is always a much better solution, which is probably why variable sized arrays are not in the C++ standatrd (or in the proposed C++0x standard).



                      BTW, for questions on "why" the C++ standard is the way it is, the moderated Usenet newsgroup comp.std.c++ is the place to go to.






                      share|improve this answer



















                      • 5




                        -1 Vector is not always better. Often, yes. Always, no. If you only need a small array, are on a platform where heap space is slow, and your library's implementation of vector uses heap space, then this feature might very well be better if it existed.
                        – Patrick M
                        Jul 11 '13 at 1:14
















                      0














                      Arrays like this are part of C99, but not part of standard C++. as others have said, a vector is always a much better solution, which is probably why variable sized arrays are not in the C++ standatrd (or in the proposed C++0x standard).



                      BTW, for questions on "why" the C++ standard is the way it is, the moderated Usenet newsgroup comp.std.c++ is the place to go to.






                      share|improve this answer



















                      • 5




                        -1 Vector is not always better. Often, yes. Always, no. If you only need a small array, are on a platform where heap space is slow, and your library's implementation of vector uses heap space, then this feature might very well be better if it existed.
                        – Patrick M
                        Jul 11 '13 at 1:14














                      0












                      0








                      0






                      Arrays like this are part of C99, but not part of standard C++. as others have said, a vector is always a much better solution, which is probably why variable sized arrays are not in the C++ standatrd (or in the proposed C++0x standard).



                      BTW, for questions on "why" the C++ standard is the way it is, the moderated Usenet newsgroup comp.std.c++ is the place to go to.






                      share|improve this answer














                      Arrays like this are part of C99, but not part of standard C++. as others have said, a vector is always a much better solution, which is probably why variable sized arrays are not in the C++ standatrd (or in the proposed C++0x standard).



                      BTW, for questions on "why" the C++ standard is the way it is, the moderated Usenet newsgroup comp.std.c++ is the place to go to.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 11 '09 at 10:39

























                      answered Dec 11 '09 at 10:25







                      anon















                      • 5




                        -1 Vector is not always better. Often, yes. Always, no. If you only need a small array, are on a platform where heap space is slow, and your library's implementation of vector uses heap space, then this feature might very well be better if it existed.
                        – Patrick M
                        Jul 11 '13 at 1:14














                      • 5




                        -1 Vector is not always better. Often, yes. Always, no. If you only need a small array, are on a platform where heap space is slow, and your library's implementation of vector uses heap space, then this feature might very well be better if it existed.
                        – Patrick M
                        Jul 11 '13 at 1:14








                      5




                      5




                      -1 Vector is not always better. Often, yes. Always, no. If you only need a small array, are on a platform where heap space is slow, and your library's implementation of vector uses heap space, then this feature might very well be better if it existed.
                      – Patrick M
                      Jul 11 '13 at 1:14




                      -1 Vector is not always better. Often, yes. Always, no. If you only need a small array, are on a platform where heap space is slow, and your library's implementation of vector uses heap space, then this feature might very well be better if it existed.
                      – Patrick M
                      Jul 11 '13 at 1:14











                      -1














                      If you know the value at compile time you can do the following:



                      template <int X>
                      void foo(void)
                      {
                      int values[X];

                      }


                      Edit: You can create an a vector that uses a stack allocator (alloca), since the allocator is a template parameter.






                      share|improve this answer



















                      • 16




                        If you know the value at compile time, you don't need a template at all. Just use X directly in your non-template function.
                        – Rob Kennedy
                        Dec 11 '09 at 15:43






                      • 3




                        Sometimes the caller knows at compile-time and the callee does not, that's what templates are good for. Of course, in the general case, no one knows X until run-time.
                        – Qwertie
                        Jul 20 '12 at 0:50












                      • You can't use alloca in a STL allocator - allocated memory from alloca will be freed when the stack frame is destroyed - that's when the method who should allocate memory returns.
                        – Oliver
                        Oct 16 '12 at 12:58
















                      -1














                      If you know the value at compile time you can do the following:



                      template <int X>
                      void foo(void)
                      {
                      int values[X];

                      }


                      Edit: You can create an a vector that uses a stack allocator (alloca), since the allocator is a template parameter.






                      share|improve this answer



















                      • 16




                        If you know the value at compile time, you don't need a template at all. Just use X directly in your non-template function.
                        – Rob Kennedy
                        Dec 11 '09 at 15:43






                      • 3




                        Sometimes the caller knows at compile-time and the callee does not, that's what templates are good for. Of course, in the general case, no one knows X until run-time.
                        – Qwertie
                        Jul 20 '12 at 0:50












                      • You can't use alloca in a STL allocator - allocated memory from alloca will be freed when the stack frame is destroyed - that's when the method who should allocate memory returns.
                        – Oliver
                        Oct 16 '12 at 12:58














                      -1












                      -1








                      -1






                      If you know the value at compile time you can do the following:



                      template <int X>
                      void foo(void)
                      {
                      int values[X];

                      }


                      Edit: You can create an a vector that uses a stack allocator (alloca), since the allocator is a template parameter.






                      share|improve this answer














                      If you know the value at compile time you can do the following:



                      template <int X>
                      void foo(void)
                      {
                      int values[X];

                      }


                      Edit: You can create an a vector that uses a stack allocator (alloca), since the allocator is a template parameter.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 11 '09 at 10:37

























                      answered Dec 11 '09 at 10:26









                      Edouard A.

                      5,8422230




                      5,8422230








                      • 16




                        If you know the value at compile time, you don't need a template at all. Just use X directly in your non-template function.
                        – Rob Kennedy
                        Dec 11 '09 at 15:43






                      • 3




                        Sometimes the caller knows at compile-time and the callee does not, that's what templates are good for. Of course, in the general case, no one knows X until run-time.
                        – Qwertie
                        Jul 20 '12 at 0:50












                      • You can't use alloca in a STL allocator - allocated memory from alloca will be freed when the stack frame is destroyed - that's when the method who should allocate memory returns.
                        – Oliver
                        Oct 16 '12 at 12:58














                      • 16




                        If you know the value at compile time, you don't need a template at all. Just use X directly in your non-template function.
                        – Rob Kennedy
                        Dec 11 '09 at 15:43






                      • 3




                        Sometimes the caller knows at compile-time and the callee does not, that's what templates are good for. Of course, in the general case, no one knows X until run-time.
                        – Qwertie
                        Jul 20 '12 at 0:50












                      • You can't use alloca in a STL allocator - allocated memory from alloca will be freed when the stack frame is destroyed - that's when the method who should allocate memory returns.
                        – Oliver
                        Oct 16 '12 at 12:58








                      16




                      16




                      If you know the value at compile time, you don't need a template at all. Just use X directly in your non-template function.
                      – Rob Kennedy
                      Dec 11 '09 at 15:43




                      If you know the value at compile time, you don't need a template at all. Just use X directly in your non-template function.
                      – Rob Kennedy
                      Dec 11 '09 at 15:43




                      3




                      3




                      Sometimes the caller knows at compile-time and the callee does not, that's what templates are good for. Of course, in the general case, no one knows X until run-time.
                      – Qwertie
                      Jul 20 '12 at 0:50






                      Sometimes the caller knows at compile-time and the callee does not, that's what templates are good for. Of course, in the general case, no one knows X until run-time.
                      – Qwertie
                      Jul 20 '12 at 0:50














                      You can't use alloca in a STL allocator - allocated memory from alloca will be freed when the stack frame is destroyed - that's when the method who should allocate memory returns.
                      – Oliver
                      Oct 16 '12 at 12:58




                      You can't use alloca in a STL allocator - allocated memory from alloca will be freed when the stack frame is destroyed - that's when the method who should allocate memory returns.
                      – Oliver
                      Oct 16 '12 at 12:58











                      -4














                      I have a solution that actually worked for me. I did not want to allocate memory because of fragmentation on a routine that needed to run many times. The answer is extremely dangerous, so use it at your own risk, but it takes advantage of assembly to reserve space on the stack. My example below uses a character array (obviously other sized variable would require more memory).



                      void varTest(int iSz)
                      {
                      char *varArray;
                      __asm {
                      sub esp, iSz // Create space on the stack for the variable array here
                      mov varArray, esp // save the end of it to our pointer
                      }

                      // Use the array called varArray here...

                      __asm {
                      add esp, iSz // Variable array is no longer accessible after this point
                      }
                      }


                      The dangers here are many but I'll explain a few:
                      1. Changing the variable size half way through would kill the stack position
                      2. Overstepping the array bounds would destroy other variables and possible code
                      3. This does not work in a 64 bit build... need different assembly for that one (but a macro might solve that problem).
                      4. Compiler specific (may have trouble moving between compilers). I haven't tried so I really don't know.






                      share|improve this answer

















                      • 4




                        man alloca and save your sanity.
                        – Kevin Cox
                        Jul 31 '14 at 17:45










                      • ... and if you want to roll this yourself, maybe use a RAII class?
                        – einpoklum
                        May 10 '16 at 23:39










                      • You could simply use boost::container::static_vector thou.
                        – Viktor Sehr
                        May 11 '16 at 7:51










                      • This doesn't have equivalents for other compilers which have more raw assembly than MSVC. VC will likely understand that esp changed and will adjust its accesses to stack, but in e.g. GCC you'll just break it completely — at least if you use optimizations and -fomit-frame-pointer in particular.
                        – Ruslan
                        Jun 22 '16 at 13:29


















                      -4














                      I have a solution that actually worked for me. I did not want to allocate memory because of fragmentation on a routine that needed to run many times. The answer is extremely dangerous, so use it at your own risk, but it takes advantage of assembly to reserve space on the stack. My example below uses a character array (obviously other sized variable would require more memory).



                      void varTest(int iSz)
                      {
                      char *varArray;
                      __asm {
                      sub esp, iSz // Create space on the stack for the variable array here
                      mov varArray, esp // save the end of it to our pointer
                      }

                      // Use the array called varArray here...

                      __asm {
                      add esp, iSz // Variable array is no longer accessible after this point
                      }
                      }


                      The dangers here are many but I'll explain a few:
                      1. Changing the variable size half way through would kill the stack position
                      2. Overstepping the array bounds would destroy other variables and possible code
                      3. This does not work in a 64 bit build... need different assembly for that one (but a macro might solve that problem).
                      4. Compiler specific (may have trouble moving between compilers). I haven't tried so I really don't know.






                      share|improve this answer

















                      • 4




                        man alloca and save your sanity.
                        – Kevin Cox
                        Jul 31 '14 at 17:45










                      • ... and if you want to roll this yourself, maybe use a RAII class?
                        – einpoklum
                        May 10 '16 at 23:39










                      • You could simply use boost::container::static_vector thou.
                        – Viktor Sehr
                        May 11 '16 at 7:51










                      • This doesn't have equivalents for other compilers which have more raw assembly than MSVC. VC will likely understand that esp changed and will adjust its accesses to stack, but in e.g. GCC you'll just break it completely — at least if you use optimizations and -fomit-frame-pointer in particular.
                        – Ruslan
                        Jun 22 '16 at 13:29
















                      -4












                      -4








                      -4






                      I have a solution that actually worked for me. I did not want to allocate memory because of fragmentation on a routine that needed to run many times. The answer is extremely dangerous, so use it at your own risk, but it takes advantage of assembly to reserve space on the stack. My example below uses a character array (obviously other sized variable would require more memory).



                      void varTest(int iSz)
                      {
                      char *varArray;
                      __asm {
                      sub esp, iSz // Create space on the stack for the variable array here
                      mov varArray, esp // save the end of it to our pointer
                      }

                      // Use the array called varArray here...

                      __asm {
                      add esp, iSz // Variable array is no longer accessible after this point
                      }
                      }


                      The dangers here are many but I'll explain a few:
                      1. Changing the variable size half way through would kill the stack position
                      2. Overstepping the array bounds would destroy other variables and possible code
                      3. This does not work in a 64 bit build... need different assembly for that one (but a macro might solve that problem).
                      4. Compiler specific (may have trouble moving between compilers). I haven't tried so I really don't know.






                      share|improve this answer












                      I have a solution that actually worked for me. I did not want to allocate memory because of fragmentation on a routine that needed to run many times. The answer is extremely dangerous, so use it at your own risk, but it takes advantage of assembly to reserve space on the stack. My example below uses a character array (obviously other sized variable would require more memory).



                      void varTest(int iSz)
                      {
                      char *varArray;
                      __asm {
                      sub esp, iSz // Create space on the stack for the variable array here
                      mov varArray, esp // save the end of it to our pointer
                      }

                      // Use the array called varArray here...

                      __asm {
                      add esp, iSz // Variable array is no longer accessible after this point
                      }
                      }


                      The dangers here are many but I'll explain a few:
                      1. Changing the variable size half way through would kill the stack position
                      2. Overstepping the array bounds would destroy other variables and possible code
                      3. This does not work in a 64 bit build... need different assembly for that one (but a macro might solve that problem).
                      4. Compiler specific (may have trouble moving between compilers). I haven't tried so I really don't know.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 15 '14 at 8:40









                      Alan

                      3




                      3








                      • 4




                        man alloca and save your sanity.
                        – Kevin Cox
                        Jul 31 '14 at 17:45










                      • ... and if you want to roll this yourself, maybe use a RAII class?
                        – einpoklum
                        May 10 '16 at 23:39










                      • You could simply use boost::container::static_vector thou.
                        – Viktor Sehr
                        May 11 '16 at 7:51










                      • This doesn't have equivalents for other compilers which have more raw assembly than MSVC. VC will likely understand that esp changed and will adjust its accesses to stack, but in e.g. GCC you'll just break it completely — at least if you use optimizations and -fomit-frame-pointer in particular.
                        – Ruslan
                        Jun 22 '16 at 13:29
















                      • 4




                        man alloca and save your sanity.
                        – Kevin Cox
                        Jul 31 '14 at 17:45










                      • ... and if you want to roll this yourself, maybe use a RAII class?
                        – einpoklum
                        May 10 '16 at 23:39










                      • You could simply use boost::container::static_vector thou.
                        – Viktor Sehr
                        May 11 '16 at 7:51










                      • This doesn't have equivalents for other compilers which have more raw assembly than MSVC. VC will likely understand that esp changed and will adjust its accesses to stack, but in e.g. GCC you'll just break it completely — at least if you use optimizations and -fomit-frame-pointer in particular.
                        – Ruslan
                        Jun 22 '16 at 13:29










                      4




                      4




                      man alloca and save your sanity.
                      – Kevin Cox
                      Jul 31 '14 at 17:45




                      man alloca and save your sanity.
                      – Kevin Cox
                      Jul 31 '14 at 17:45












                      ... and if you want to roll this yourself, maybe use a RAII class?
                      – einpoklum
                      May 10 '16 at 23:39




                      ... and if you want to roll this yourself, maybe use a RAII class?
                      – einpoklum
                      May 10 '16 at 23:39












                      You could simply use boost::container::static_vector thou.
                      – Viktor Sehr
                      May 11 '16 at 7:51




                      You could simply use boost::container::static_vector thou.
                      – Viktor Sehr
                      May 11 '16 at 7:51












                      This doesn't have equivalents for other compilers which have more raw assembly than MSVC. VC will likely understand that esp changed and will adjust its accesses to stack, but in e.g. GCC you'll just break it completely — at least if you use optimizations and -fomit-frame-pointer in particular.
                      – Ruslan
                      Jun 22 '16 at 13:29






                      This doesn't have equivalents for other compilers which have more raw assembly than MSVC. VC will likely understand that esp changed and will adjust its accesses to stack, but in e.g. GCC you'll just break it completely — at least if you use optimizations and -fomit-frame-pointer in particular.
                      – Ruslan
                      Jun 22 '16 at 13:29













                      -4














                      You need a constant expression to declare an array in C/C++.



                      For dynamic size arrays, you need to allocate memory on heap, then manage the liftime of this memory.



                      void foo(int n) {
                      int* values = new int[n]; //Declare a variable length array
                      [...]
                      delete values;
                      }





                      share|improve this answer



















                      • 11




                        Yes, I know, the question is why is this available in C99 and not C++?
                        – Andreas Brinck
                        Dec 11 '09 at 10:23










                      • C99 [ PDF @ open-std.org/JTC1/sc22/wg14/www/docs/n1401.pdf ] has VLAs (Variable Length Arrays).
                        – pmg
                        Dec 11 '09 at 10:27










                      • A more current C99 draft is http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf (3.7 MB PDF); it includes the original C99 standard plus the three Technical Corrigenda.
                        – Keith Thompson
                        Sep 18 '11 at 2:01
















                      -4














                      You need a constant expression to declare an array in C/C++.



                      For dynamic size arrays, you need to allocate memory on heap, then manage the liftime of this memory.



                      void foo(int n) {
                      int* values = new int[n]; //Declare a variable length array
                      [...]
                      delete values;
                      }





                      share|improve this answer



















                      • 11




                        Yes, I know, the question is why is this available in C99 and not C++?
                        – Andreas Brinck
                        Dec 11 '09 at 10:23










                      • C99 [ PDF @ open-std.org/JTC1/sc22/wg14/www/docs/n1401.pdf ] has VLAs (Variable Length Arrays).
                        – pmg
                        Dec 11 '09 at 10:27










                      • A more current C99 draft is http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf (3.7 MB PDF); it includes the original C99 standard plus the three Technical Corrigenda.
                        – Keith Thompson
                        Sep 18 '11 at 2:01














                      -4












                      -4








                      -4






                      You need a constant expression to declare an array in C/C++.



                      For dynamic size arrays, you need to allocate memory on heap, then manage the liftime of this memory.



                      void foo(int n) {
                      int* values = new int[n]; //Declare a variable length array
                      [...]
                      delete values;
                      }





                      share|improve this answer














                      You need a constant expression to declare an array in C/C++.



                      For dynamic size arrays, you need to allocate memory on heap, then manage the liftime of this memory.



                      void foo(int n) {
                      int* values = new int[n]; //Declare a variable length array
                      [...]
                      delete values;
                      }






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 12 at 16:53









                      Arnav Borborah

                      7,99222347




                      7,99222347










                      answered Dec 11 '09 at 10:20









                      Tryum

                      573618




                      573618








                      • 11




                        Yes, I know, the question is why is this available in C99 and not C++?
                        – Andreas Brinck
                        Dec 11 '09 at 10:23










                      • C99 [ PDF @ open-std.org/JTC1/sc22/wg14/www/docs/n1401.pdf ] has VLAs (Variable Length Arrays).
                        – pmg
                        Dec 11 '09 at 10:27










                      • A more current C99 draft is http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf (3.7 MB PDF); it includes the original C99 standard plus the three Technical Corrigenda.
                        – Keith Thompson
                        Sep 18 '11 at 2:01














                      • 11




                        Yes, I know, the question is why is this available in C99 and not C++?
                        – Andreas Brinck
                        Dec 11 '09 at 10:23










                      • C99 [ PDF @ open-std.org/JTC1/sc22/wg14/www/docs/n1401.pdf ] has VLAs (Variable Length Arrays).
                        – pmg
                        Dec 11 '09 at 10:27










                      • A more current C99 draft is http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf (3.7 MB PDF); it includes the original C99 standard plus the three Technical Corrigenda.
                        – Keith Thompson
                        Sep 18 '11 at 2:01








                      11




                      11




                      Yes, I know, the question is why is this available in C99 and not C++?
                      – Andreas Brinck
                      Dec 11 '09 at 10:23




                      Yes, I know, the question is why is this available in C99 and not C++?
                      – Andreas Brinck
                      Dec 11 '09 at 10:23












                      C99 [ PDF @ open-std.org/JTC1/sc22/wg14/www/docs/n1401.pdf ] has VLAs (Variable Length Arrays).
                      – pmg
                      Dec 11 '09 at 10:27




                      C99 [ PDF @ open-std.org/JTC1/sc22/wg14/www/docs/n1401.pdf ] has VLAs (Variable Length Arrays).
                      – pmg
                      Dec 11 '09 at 10:27












                      A more current C99 draft is http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf (3.7 MB PDF); it includes the original C99 standard plus the three Technical Corrigenda.
                      – Keith Thompson
                      Sep 18 '11 at 2:01




                      A more current C99 draft is http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf (3.7 MB PDF); it includes the original C99 standard plus the three Technical Corrigenda.
                      – Keith Thompson
                      Sep 18 '11 at 2:01


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1887097%2fwhy-arent-variable-length-arrays-part-of-the-c-standard%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Lugert, Oklahoma