Dynamic and static arrays

whats the differnce between dynamic and simple array as in simple array also we can take its size as run time and cant increase it same goes for dynamic array like cin>>n; int a[n]

Hey @Namanjain123 first difference is that while static array are allocated memory during compile time dynamic array are allocated memory during run time.and the second difference is that while the local arrays are created on stack and have fixed storage duration (i.e the scope of the block in which they are declared) so, they get destroyed when the function end. The dynamic arrays are created on heap, So you need to allocate and free them as well. While we are on the subject of changing the size, If you have created a dynamic array using malloc then you can increase the size of that array using realloc, so image being in a system where you dont know how many elements you are going to encounter, it wouldn’t be memory efficient to create an array of size and 10^6 as most of it could go unused however with the help of dynamic memory you can easily create a block of small memory and if space gets used up reallocate to new location with different size.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.