Ravneet Taneja
Ravneet Taneja

Reputation: 21

Memory allocation of variable arrays in c

Is the memory to variable arrays allocated during run-time or compile-time in c?

int n; 
printf("Enter size of the array: ");
scanf("%d",&n);

int a[n];
for(int i=0; i<n; i++)
   {
      a[i] = 0;
   }
}

Upvotes: 1

Views: 70

Answers (2)

JuniorCompressor
JuniorCompressor

Reputation: 20005

Since the size n of the array is defined at runtime, then also the allocation happens in the runtime.

The memory is allocated from the stack, which is faster than allocating from the heap. But how much memory you can reserve is much lower.

Upvotes: 2

haccks
haccks

Reputation: 105992

It is allocated on run time but on stack not on heap.

Upvotes: 1

Related Questions