int main()
{
int p;
scanf("%d",&p);
fun()
{
int arr[p]; // isn't this similar to dynamic memory allocation??
}
}
//if not then what other objective is achieved using malloc and calloc??
//Someone please throw some light 🙂
Creating an arbitrary amount on the stack is dangerous. You do not know how large the stack is (Well you can probably set it from the compiler) but if you create a 256Meg array as you have defined you WILL cause a stack overflow. IF you do it with malloc then you will create on the heap. This won’t, then, touch the stack and won’t affect the running of your application. Creating on the heap means that you are limited by the free memory on your system. If there is none the malloc returns NULL, if there is enough you are good to go. You also have the bonus of being able to free the memory whenever you like rather then when the allocation drops out of scope (ie passes the next ‘}’).