3
#include <iostream>

    using namespace std;

    int main(int argc, const char * argv[])
    {

        int size;
        cin >> size;
        int myArray[size]; //this shouldn't compile , right ? 

        return 0;
    }

I thought this wouldn't compile, but it does actually (using the g++ command).

What I found out later is that GCC actually allows variable-size arrays even if by standard C++ doesn't support variable-size arrays, Which is weird ! Because I hear everyone saying that the only way to create a variable-size array is to use dynamic allocation like int* array = new int[size]; or better std::vector. I thought GCC wouldn't allow that piece of code !

Anyway, My theoretical question is, the myArray array is allocated in the heap or stack area ?

Zack Braksa
  • 1,628
  • 18
  • 26
  • 1
    You're a bit too late: http://stackoverflow.com/questions/12484588/differences-with-ide-compilers-array-handling. Your answer is in the linked post there as well. – chris Sep 18 '12 at 21:18
  • btw, the memory is allocated on stack. compiler just needs to generate instructions to increase stack pointer for size * sizeof(int). – Zdeslav Vojkovic Sep 18 '12 at 21:22
  • @chris: *How* it is done is different from *why* is it allowed in a compiler. Admittedly it looks like the intention might not actually have been getting to understand the implementation, but rather a *why* does it compile... – David Rodríguez - dribeas Sep 18 '12 at 21:23
  • 1
    @DavidRodríguez-dribeas, It's explained in the question linked to from my link, though. Quoting the accepted answer, *gcc allocates the array on the stack, just like it does with int array[100] by just adjusting the stack pointer. No heap allocation is done. It's pretty much like _alloca.* – chris Sep 18 '12 at 21:27
  • possible duplicate of [In C++ books, array bound must be constant expression, but why the following code works?](http://stackoverflow.com/questions/5947661/in-c-books-array-bound-must-be-constant-expression-but-why-the-following-cod) – Bo Persson Sep 18 '12 at 21:52
  • @chris: That in fact provides the answer, but not the question you linked :) – David Rodríguez - dribeas Sep 19 '12 at 02:21
  • @DavidRodríguez-dribeas, I guess I worded it a bit badly. That's what I meant in the second part of my first comment. – chris Sep 19 '12 at 02:25

3 Answers3

2

That is an extension on the compiler side. How does it work? Well, it works only to some extent.

The implementation basically moves the stack pointer a quantity that is dependent on the size of the array, and calls the memory in the middle by the name of the array. It only works to some extent because in VLAs the size is not an integral part of the type, which means that many constructs that can be used on regular arrays cannot be done with this type, like for example passing the array to a template by reference... sizeof is usually supplied but implemented as a runtime construct.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

myArray in your example is allocated on the stack. g++ has an extension that allows this trick. The heap isn't necessary to do this, the compiler just generates code to increase the stack pointer by an amount that is computed at run time.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
0

VLAs are an extension, many implementations have there own extensions to the C++ language. If you want g++ to complain that you are not adhering to the standard, pass -pedantic flag. VLAs are on the stack which is really fast, however, this is also the cause of problems since you have limited stack space. In C++ however, we have constructs like std::vector, etc., so there is really no need for there use.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166