Confused about dynamic and static memory allocation!

Suppose we declared an array:- int arr[30] . Similarly, we will allocate memory to an array dynamically - int *ptr=new int[30]. In both the cases, we have fixed the array size, then what is the basic conceptual difference between these two. Please help me out, it is really confusing!

  1. There are two types of memory.
    One is the static memory or the compile-time memory that is used for variables and arrays declared in the program , those which the compiler can identify and allocate memory at runtime.
    int a;
    int b[1000];
    int b[n];

  2. The second memory is the heap memory or the dynamic memory. It can only be allocated at the runtime. There are various ways to allocate dynamic memory such as by using C functions like alloc( ) , calloc( ) etc. or by using the C++ ‘new’ operator.
    int* a = new int[100];
    int n=1000;
    int* a = new int[n];

  3. in both ways you can create an array of variable size or fixed size. but the later method using new has variety of uses than first one. you can deallocate the memory created by new by using delete. this might not seem useful to you but in memory limited environment, its very useful.

  4. Not every question can always be solved using static array. There are much more complex data structures than array like Trees and Linked Lists that you will learn later in this course. All of them use dynamic memory allocation.

suggest to read:

please rate and resolve if satisfied.
thanks

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.