0

I want to create an array on the stack that varies in size at runtime. As far as I know, this is always illegal in c++:

void local_array(unsigned int i) {
    int arr[i];
}

However, it is possible to dynamically allocate memory on the stack using recursion. Is there a different way to accomplish this without using recursion? If not, what are the technical limitations preventing the example above from being realized?

12
  • 3
    @Blacktempel std::vector allocates memory on the heap. Commented Jan 13, 2014 at 8:36
  • I don't know what you are trying to do and I am not sure you know it either. In B you always have an array of 1 element in the stack unless it is not in the stack (arr != null). Commented Jan 13, 2014 at 8:36
  • 1
    Without an implementation extension like gcc has (which supports VLA's like you apparently want) you're not going to get this in runtime support out of a standard-only compliant implementation. At least not right now. (and B is nothing even close to a contiguous array). Commented Jan 13, 2014 at 8:38
  • @user270349 I'm not sure what you mean here. The function is called recursively, and every time it's called it adds an integer on the stack. That adds up to an array of integers. An int pointer can point to things on the stack - in this case it points to the integer pushed on the first call to local_array_recursive. Once i is 0, the array has been fully constructed and can be read and written to like any other array. Commented Jan 13, 2014 at 8:42
  • I don't believe you have any guarantee about the bits of that array being contiguous. Commented Jan 13, 2014 at 8:43

2 Answers 2

2

There's no technical limitation.

You can do this in C++14, and it's already implemented in clang & gcc lastest version.

void test(int n)
{
   int a[n]; //ok now
   cout << a[0] << endl; //output a random value on stack!
}
Sign up to request clarification or add additional context in comments.

4 Comments

What was the reasoning for it being accepted so late, though?
Apart from the undefined behavior of indeterminate data evaluation (which is acknowledged in-comment), this is correct.
@NmdMystery The reason is that, C++ got vector and there's no reason to use stack dynamic array. It's accepted as standard recently mainly for the compatibility of the latest C language standard C11. Since C has no vector, so C has motivation for it, and it's accepted as C11.
@user534498 Visual-C++ 2013 doesn't allow it, and it's supposed to be near-compliant to C11. I guess that's one of the things it's missing. EDIT: C11
1

Such functionality was suggested (N3639 - Runtime-sized arrays with automatic storage duration) on 4/2013 and was accepted.

It is now part of the C++ 2013-05-15 Committee Draft

From 8.3.4:

void f(unsigned int n)
{
    int a[n]; // type of a is “array of runtime bound of int”
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.