What's dynamic memory allocation? | Sololearn: Learn to code for FREE!


What's dynamic memory allocation?

2/14/2019 5:10:03 AM

Manav Malhotra

9 Answers

New Answer


● Static memory allocation: Memory allocation size is defined before the program runs, the size of memory needed is hard coded in the source code. ● Dynamic memory allocation: Memory allocation size is unknown until the program runs, at which point the size of memory needed is acquired, possibly through user input. And there's a chapter covering that very subject: https://www.sololearn.com/learn/CPlusPlus/1632/


It is basically memory allocation at run time


Dynamic memory allocation happens when you try allocate memory via new As for details Dynamic memory allocation is a process in which you can allocate memory a program requires and this come handy in many cases like suppose if you are making a notebook but you need to declare the array before you could have known how much the user will give input well here you can use use dynamic memory allocation as like the user will enter the characters your program will allocate memory for entering a new one and thus you will save many memory also.


Array basically work static memory allocation. It means we can't extend size. But arraylist work dynamic memory allocation. It allow to increase size at runtime. And give the space only it requires. I hope u understand. 😊



int main { int var[3]= {1,2,3}; //static memory allocation } int main { int var = new int[3]; int var1 = new int; //dynamic memory allocation /*var will be a array which can contain 3 variables and var1 can store only one*/ } I am sorry Manav Malhotra i know this is not that much good example but i will give you a good one later.


You should use malloc or calloc functions to allocate memory dynamically.


Can u give me the example


Of how to dynamically allocate memory ?