0

In the below code,is the arr[n] is allocated from stack or heap?

I am confused since in general the array size is determined at compile time. How the below code working ?

#include<stdio.h>
int main(){
    int n;
    scanf("%d",&n);
    int arr[n];
    for(int arr_i = 0; arr_i < n; arr_i++){
       scanf("%d",&arr[arr_i]);
    }
    for(int arr_i = (n-1); arr_i >= 0; arr_i--){
       printf("%d ",arr[arr_i]);
    }
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Vignesh
  • 37
  • 7

6 Answers6

4

That is Variable Length Array. It is required in C99 standard an has be made optional for C11 conformant compilers - and as said by others in comments it is not supported in C++ any version.

It declares an array with automatic storage duration with is commonly implemented with stack storage. But remember, the concept of stack and heap, even if used by all compilers is only a implementation detail.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
2

In your case, what you're using is called variable length array. This feature was introduced in C99 but again made optional in C11.

Actually, the C standard does not impose any specification for the allocation of VLAs. This decision is left to the compiler.

The widely-used gcc allocates VLAs on stack memory.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

Variable length arrays were added in C99. In C11 they have been reduced to optional.

The C standard doesn't specify where variables are stored so it is up to the compiler manufacturer.

gcc stores VLA:s on the stack.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
1

Prior to the advent of variable length arrays (VLAs) with C99, the size had to be an integer constant, which includes the possibility of an expression formed from constant integer values.

Having said that, the memory allocated for a[n] is usually static as in the case of gcc but there is no formal specification about how memory should be allocated for VLA

Interesting links

  1. Which Compiler Should I trust?

  2. GNU-GCC note on VLA.

  3. Diary of a graphics programmer(See What's missing)

  4. Enabling VLAs in MS Visual C++

Community
  • 1
  • 1
sjsam
  • 21,411
  • 5
  • 55
  • 102
0

GCC (GNU Compiler Collection) compiler has added many extensions to C that were commonly overlooked. These added extensions can help us to simplify the development of C applications. One of the extension was addition of variable length arrays and zero length array.

In the below code,is the arr[n] is allocated from stack or heap.

GCC permits the declaration of arrays using non-constant expressions. This is possible in ISO C99, but not in C89. The GNU C Compiler allocates memory for Variable Length Arrays on the stack. VLAs, like all objects in C, are limited to SIZE_MAX bytes.

Source - https://en.wikipedia.org/wiki/Variable-length_array#cite_note-7

abhiarora
  • 9,743
  • 5
  • 32
  • 57
0

When you declare the array int arr[n]; it will have the automatic scope i.e., it will have the scope only within that function. And the memory for this will get allocated from the stack. Only those variables which are allocated using malloc, calloc, etc. will go to heap. For more information you can have a look into memory layout of c program & automatic variable