0

It is my understanding that in C and C++ , we create data-structures whose size is known at compile time on the stack and we use the heap (malloc-free / new-delete) for things whose size is not known at compile time and is decided at run-time.Why then , does my g++ compiler allow me to do something like the following code snippet.

int main(void)
{
    int n ;
    cin >> n ; // get the size of array.
    int arr[n] ; // create a variable sized array.
    .... do other stuff ...
}

Specifically , in the case of an array:
An array is assigned a contiguous block of memory on the stack and there are variables above and below it on the stack , so the array's size must be known so that the variable above the array on the stack , the array itself , and the variables below the array on the stack can all fit into memory neatly. How then are variable sized arrays on the stack implemented ? Why are they even necessary? Why can't we just use the heap for variable sized buffers?

EDIT:
I understand from the comments , that C and C++ have different rules regarding whether VLA's are standard or not and also Neil Butterworth's comment that it is generally not a good idea to ask a question about two languages at once. Thank you people , so I am removing the C tag from my question , as I intended to mainly ask about C++ , as evident by the code snippet syntax. Sorry for the confusion , and thanks for the responses.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Neeraj
  • 137
  • 1
  • 2
  • 9
  • 1
    C++ does not support VLA. – haccks May 08 '17 at 21:35
  • 2
    Such arrays are not part of standard c++. –  May 08 '17 at 21:35
  • https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html – Iłya Bursov May 08 '17 at 21:36
  • same reason as having alloca? – felix May 08 '17 at 21:37
  • "(malloc-free / new-delete) " - we don't do that - we use std::vector. –  May 08 '17 at 21:37
  • I think the C standard allows for variable sized arrays since C99, but might be wrong. It's just the same as using `alloca`, but more comfortable. – Myst May 08 '17 at 21:38
  • 1
    @felix alloca is not part of standard C++ either. –  May 08 '17 at 21:38
  • VLAs are different to alloca as they finish when the current block finishes, whereas alloca remains for the duration of the function – M.M May 08 '17 at 21:39
  • 1
    "Why are they even necessary? " --> why not do all your programming in assembly? same reason... – M.M May 08 '17 at 21:39
  • "I think the C standard allows for variable sized arrays since C99, but might be wrong" - you are right, and this is why you should not ask one question about two completely different languages. –  May 08 '17 at 21:39
  • @NeilButterworth - Thanks :) ... I ignored the C++ part because it's been years since I touched that language and I find it hard to keep up with. That's part of the reason I didn't post an answer... You're right, having a single question reference two languages is easily a source for confusion. – Myst May 08 '17 at 21:43
  • @NeilButterworth no, it's not. But why everyone keeping saying that it is not part of the standard? It is not supposed to be asked with c++ tag? – felix May 08 '17 at 21:52
  • @felix VLA's are not C++, that's why a C++ tag is questionable. Also, a compiler, if they did implement VLA's, can implement them in any way they feel is ok since it isn't standard. So even the question about "stack usage" is questionable. – PaulMcKenzie May 09 '17 at 01:06

1 Answers1

7

They aren't allowed in standard C++, but they are allowed in standard C, and g++ allows them in C++ as well, as a language extension.

How then are variable sized arrays on the stack implemented?

See this question. The gist is that the size (n) gets evaluated and stored in a compiler-generated local variable, and then that much stack space is allocated. There's no law of physics that says the size of a stack variable has to be known at compile-time - it's merely simpler that way.

Why are they even necessary?

They aren't. As you aid, you can do the same thing with dynamic allocation.

Why can't we just use the heap for variable sized buffers?

Stack allocation is more efficient.

Community
  • 1
  • 1
user253751
  • 57,427
  • 7
  • 48
  • 90