Why use dynamically allocated memory

i had seen that almost each time either creating a node for a data structure or making a helper array to solve a particular problem we use dynamic memory allovcation, what is the reason for it and what are the various advantages of doing so

Hey @Parmeet-Kalsi-1631789033630118
There are two memory spaces available to you :stack & heap
Stack has limited size and u have to start with predefined max space. All the arrays and variables u create in your code are allocated in heap unless u create them dynamically.
But while dynamically allocating nodes that memory is allocated on heap and can be decided on runtime.
So some advantages of Dynamic allocation are:

  • Data structures can grow and shrink according to the requirement.
    • We can allocate (create) additional storage whenever we need them.
    • We can de-allocate (free/delete) dynamic space whenever we are
      done with them.
  • Dynamic Allocation is done at run time.

I hope this resolves ur query :slight_smile:

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.