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
You control the exact size and the lifetime of these memory
locations. If you don't free it, you'll run into memory leaks, which
may cause your application to crash, since it, at some point cannot
allocation more memory. (dynArr
)
Actually...
Heap
The heap is a region of your computer's memory that is not managed
automatically for you, and is not as tightly managed by the CPU. It is
a more free-floating region of memory (and is larger). To allocate
memory on the heap, you must use malloc() or calloc(), which are
built-in C functions.
Once you have allocated memory on the heap, you
are responsible for using free() to deallocate that memory once you
don't need it any more. If you fail to do this, your program will have
what is known as a memory leak. That is, memory on the heap will still
be set aside (and won't be available to other processes).
Stack
It's a special region of your computer's memory that stores temporary
variables created by each function (including the main() function).
The stack is a "LIFO" (last in, first out) data structure, that is
managed and optimized by the CPU quite closely. Every time a function
declares a new variable, it is "pushed" onto the stack. Then every
time a function exits, all of the variables pushed onto the stack by
that function, are freed (that is to say, they are deleted). Once a
stack variable is freed, that region of memory becomes available for
other stack variables.
Resources
malloc()
and family inC
.. – Subnormal