Consider the following C code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int arrSize;
scanf("%d", &arrSize);
printf("%d\n",arrSize);
int *dynArr = (int *)malloc(sizeof(int)*arrSize);
int arr1[arrSize];
return 0;
}
In the code above, arrSize is size of an array taken from the user input. I want to know if the following observations are correct:
dynArr is a dynamic array which is allocated in the memory during runtime on heap. Size of dynArr can be modified using realloc function.
arr1 is also allocated in the memory during runtime but is not dynamic (i.e. their size cannot be modified) and it is allocated on Stack.
It allocates memory from free memory store. Now there is nothing called heap and stack in memory in case of C..it is something logically we consider in case of
C
.(In implementation of C)]Only thing we are bothered about whether we need something which you want to be alive even if the scope of where it is declared ends or not.
For heap it is the case .. for stack it is not.
In your case
int arr1[arrSize];
is allocated on the same frame on which this main function local variables are stored.Dynamic allocation
Actually...
Heap
Stack
Resources