-3
int main(){

        int n;

        scanf("%d",&n);

        int a[n];
}

In the above where does the space for array a[] , get allocated ? In stack or heap ?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Achyuta Aich
  • 569
  • 1
  • 5
  • 13
  • 6
    OT: Variable length arrays aren't allowed in standard C++. What compiler? What operating system? –  Dec 02 '14 at 08:10
  • I am asking a general question regarding memory allocation , not specifically about very large arrays – Achyuta Aich Dec 02 '14 at 08:12
  • 4
    @jaig it's not valid C++ code (although it is valid C), so it gives you a compiler error and doesn't get allocated anywhere (some compilers allow this as an extension, but it's not standard C++) – PeterT Dec 02 '14 at 08:14
  • @PeterT Diagnostics are implementation-specific, and a compiler (neither GCC or Clang by default) doesn't have to give a fatal error. So the question still has a valid answer. –  Dec 02 '14 at 08:16
  • 2
    but this program runs well in ideone and also on my pc which has standard gcc compiler , I run this as a .cpp file – Achyuta Aich Dec 02 '14 at 08:17
  • 1
    @jaig and that doesn't change the fact it's invalid. – Bartek Banachewicz Dec 02 '14 at 08:20
  • My problem is: 1. We know that a compiler allocates memory statically during compilation ( the memory size on stack will be decided by the size of the array 'n'- which is however not known at that time ) – Achyuta Aich Dec 02 '14 at 08:21
  • 2. Memory allocation is not on heap as we not using new keyword – Achyuta Aich Dec 02 '14 at 08:21
  • 1 and 2 implies neither in heap nor in stack , then where? – Achyuta Aich Dec 02 '14 at 08:22
  • @BartekBanachewicz it seems to be valid C, even if it looks very strange at first glance. – Wolf Dec 02 '14 at 08:22
  • @Wolf And the question is tagged C++. – Bartek Banachewicz Dec 02 '14 at 08:23
  • @Bartek , your justification does not satisfy me – Achyuta Aich Dec 02 '14 at 08:24
  • 2
    @jaig that's too bad, then. [read about VLAs](http://en.wikipedia.org/wiki/Variable-length_array) in the meantime. – Bartek Banachewicz Dec 02 '14 at 08:26
  • 2
    @jaig: Compile with the option `-pedantic-errors` in order to enforce strict language conformance. – Benjamin Lindley Dec 02 '14 at 08:28
  • 1
    Downvoters: please explain! The question seems absurd only at first glance. – Wolf Dec 02 '14 at 08:41
  • @BenjaminLindley `-std=c++11` or similar would be more effective. `-pedantic` only catches the puristic things. – rubenvb Dec 02 '14 at 08:42
  • Shouldn't there be new scope for the declaration of `a` in C? – Wolf Dec 02 '14 at 08:44
  • 1
    Its a pity to see a question receive negative votes ... People ask question or doubt when they are not clear about a topic , the questioin may seem irrelevant to few people but it would clear up the doubts of many people new in this field ... shame to u guys who down voted this question .. – Achyuta Aich Dec 02 '14 at 08:46
  • @rubenvb: More effective in what? I'm talking about catching non-standard extensions (such as VLAs). `-std=c++11` will not do that. – Benjamin Lindley Dec 02 '14 at 08:47
  • 1
    @Benjamin wow, that sucks indeed. `-pedantic` it is. The standard option should give an error IMHO. – rubenvb Dec 02 '14 at 09:08
  • @jaig I think some comments in your code could make it easier to understand your actual question. Most people (including myself) don't realize the array definition in this place. – Wolf Dec 02 '14 at 10:17
  • possible duplicate of [Variable length arrays in C++?](http://stackoverflow.com/questions/1887097/variable-length-arrays-in-c) – Bartek Banachewicz Dec 02 '14 at 17:15

3 Answers3

3

If your compiler compiles that, it's most likely going to be on the stack. In standard parlance, if you care to apply that to a construct that's not actually standard-conforming, it has automatic storage duration, meaning you don't have to clean it up yourself and it will become invalid at the end of the scope.

What you have there is a VLA (variable length array), a construct from C that allows you to have arrays whose dimensions are known only at runtime. Usually, the way they work is similar to the "function" alloca, which decreases the stack pointer by an amount known at runtime and "returns" the pointer to it. I put "function" in quotes because doing this requires some low-level hackery that's not provided for by normal function scope semantics.

VLAs don't exist in C++, so you're using a compiler extension, and the precise semantics of VLAs in that extension depend on your compiler. Since this is likely gcc, I'll leave you a link to the relevant part of its documentation.

Wintermute
  • 42,983
  • 5
  • 77
  • 80
  • if on stack , how much memory will a compiler allocate without knowing its size? – Achyuta Aich Dec 02 '14 at 08:51
  • 1
    The stack size remains fixed. If your VLA would be larger than the space left on stack, you get a stack overflow. The problem is roughly equivalent to a runaway recursive function that requires more and more stack space until it is exhausted -- the size there is also not known at compile time. – Wintermute Dec 02 '14 at 09:03
2

In the C11 standard, it describes in §6.7.6.2/4 what actually makes an array a variable length array:

If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

Yet in N3337 (C++11 draft), [dcl.array] says:

If the constant-expression (5.19) is present, it shall be an integral constant expression and its value shall be greater than zero.

The language about "variable length array"s are completely missing, so they don't exist in C++.

The draft talks about object lifetime in [basic.life]. In C, VLAs cannot have static or thread storage duration. §6.2.4/7 then says:

For such an object that does have a variable length array type, its lifetime extends from the declaration of the object until execution of the program leaves the scope of the declaration.35) If the scope is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate.

GCC, which allows VLAs in C++ as an extension, mimics the same semantics:

Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++. These arrays are declared like any other automatic arrays, but with a length that is not a constant expression. The storage is allocated at the point of declaration and deallocated when the block scope containing the declaration exits.

Clang also allows VLAs for compatibility with some restrictions.

So more than likely, VLAs are allocated on the stack.

  • "Secondly, the "heap" and "stack" are misnomers" - not really. Dynamic objects come from a "free store", which is just another name for a "heap". Automatic storage behaves like a stack, so we might as well call it a "stack" (as the C++ standard does in various places). – Mike Seymour Dec 02 '14 at 08:48
  • @MikeSeymour So, it's allocated on a stack on the heap? – Wolf Dec 02 '14 at 08:51
  • @Wolf VLAs have automatic storage duration like the other answer says. –  Dec 02 '14 at 08:56
0

Compiler must know what is the size of all array int the code before execute the project. You cant assign it while the program runs.

batuhan
  • 5
  • 3
  • This argument is outdated. The question is about variable-length arrays, a relatively new feature in C (and an extension in some C++ compilers). – Wolf Dec 02 '14 at 09:04