Use dynamic memory allocation if you do not know exactly how much memory your program will need when compiling.
int a[n] , for example, will limit your array size to n. In addition, it allocates nx 4 bytes of memory regardless of whether you use it or not. This is allocated on the stack, and the variable n must be known at compile time.
int *a = (int *)malloc(n * sizeof (int)) , on the other hand, assigned at runtime on the heap, and n should only be known at runtime, not necessarily at compile time.
It also ensures that you allocate as much memory as you really need. However, since you allocated it at run time, the cleanup should be done by you with free .
Anirudh ramanathan
source share