Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is array size limited when declared at compile time?

for example I can do

int *arr;
arr = (int *)malloc(sizeof(int) * 1048575);

but I cannot do this without the program crashing:

int arr[1048575];

why is this so?

like image 348
ladookie Avatar asked Jun 29 '11 03:06

ladookie


1 Answers

Assuming arr is a local variable, declaring it as an array uses memory from the (relatively limited) stack, while malloc() uses memory from the (comparatively limitless) heap.

like image 172
Adam Liss Avatar answered Oct 27 '22 01:10

Adam Liss