3

For example, I have an array with dynamic size based on user input:

int n;
cin >> n;
int items[n];

Is this array allocated on stack? Or is it on heap like if I have written:

int n, *items;
cin >> n;
items = new int[n];
...
delete [] items;

Edit: I understand what the second code does. I'm asking whether the first code does the same thing like the second, but with less amount of lines.

kravemir
  • 10,636
  • 17
  • 64
  • 111

3 Answers3

7

Your first example isn't using a dynamic array at all - it's using a stack-allocated variable length array (which usually behind the scenes is equivalent to an alloca call, with the exception of the sizeof operator), which is a feature of C99, not C++.

Your second array, of course, is allocated on the heap via new.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • 7
    Note that variable-length arrays are not supported in standard C++, so the first code snippet would be a syntax error in both C99 (no `iostream`s) and C++ (no VLAs). Nevertheless, some compilers support VLAs in C++ as an extension. – In silico Mar 16 '13 at 19:29
  • @Insilico correct, but many C++ compilers support C99 features as an extension (at least GCC, Clang, and maybe MSVC does). – Richard J. Ross III Mar 16 '13 at 19:30
  • 1
    @Insilico I will compile without warning. Tested with g++ without any build parameters. – kravemir Mar 16 '13 at 19:32
  • 1
    It works because [variable-length arrays](http://en.wikipedia.org/wiki/Variable-length_array) (which is what the first code snippet requires) are [supported by GCC as an extension](http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html). It's not supported in standard C++. – In silico Mar 16 '13 at 19:33
  • @Insilico Yes, I have read them all now. But why it compiles without any warning? – kravemir Mar 16 '13 at 19:37
  • 2
    @Miro: A compiler is not required to issue warnings when you use extensions. The compiler writers of GCC chose not to have it issue warnings in this case (although you might if you compile with all warnings enabled). – In silico Mar 16 '13 at 19:38
3

You use new for allocating memory, so your array is stored in heap

nabroyan
  • 3,225
  • 6
  • 37
  • 56
  • I'm asking what is behind the dynamic sized arrays, not what does the second code :) – kravemir Mar 16 '13 at 19:27
  • 2
    Yes, the heap is what serves as the backing store for dynamically sized arrays in this case. What else would you like to know? – In silico Mar 16 '13 at 19:28
  • If you create an array without "new" or dynamic memory allocation functions, with a constant size, then it is stored in stack. If you allocate a memory for your array (and size may also be a non-const expression), then it's in heap. – nabroyan Mar 16 '13 at 19:34
-1

Your first block of code will fail to compile, you cannot allocate a dynamically sized array without the use of a memory allocation with either new or malloc/calloc/realloc.

In order to do what you want you will need to do what you have in the second block, which will be allocated on the heap always.

Dynamic = Heap. Non-dynamic = Stack.

Always remember to free your memory!

csteifel
  • 2,854
  • 6
  • 35
  • 61
  • 1
    @Miro: It works because [variable-length arrays](http://en.wikipedia.org/wiki/Variable-length_array) (which is what the first code snippet requires) are [supported by GCC as an extension](http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html). It's not supported in standard C++. – In silico Mar 16 '13 at 19:32
  • 1
    Please understand that the VLA shown in the first part of the question is a C99 extension, and is supported by most modern compilers. – Richard J. Ross III Mar 16 '13 at 19:32