There are three different ways to allocate memory:
Static allocation:
int data[5000000]; /* allocate outside functions */
or
static int data[5000000]; /* anywhere */
The memory is set up as the program starts.
On the heap:
int *data = malloc(5000000*sizeof int);
The memory is set up when malloc
is called. If there isn't enough free space in the heap, the program will request more memory from the OS.
On the stack:
int data[5000000]; /* allocate inside function */
The memory will be allocated from the stack. If there isn't enough space left on the stack the program will fail.
Static allocation and heap allocation can usually handle large arrays while stack allocation is more limited.