We can directly ask from the user about the number of elements ought to be stored in an array. Why do we have to dynamically allocate the memory?
Why do we use dynamic memory allocation?
Hey @aryaman.kumar26 sometimes we are working in an environment where it is impossible to predict the total number of elements that’ll be stored, one can estimate but if the estimate overshoots the real number it would result in wasted memory and if estimate undershoots real number we would have to change the code to incorporate new numbers. For example say you are using structure to store information of patients in hospital, would it be possible to tell how many patients will come?. No, so in that case we would have to use dynamic memory allocation.
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.