What's dynamic memory allocation? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What's dynamic memory allocation?

14th Feb 2019, 5:10 AM
Manav Malhotra
Manav Malhotra - avatar
9 Answers
+ 12
● 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/
14th Feb 2019, 5:56 AM
Ipang
+ 4
It is basically memory allocation at run time
14th Feb 2019, 9:09 AM
shalini jha
shalini jha - avatar
+ 2
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.
14th Feb 2019, 5:36 AM
Sahil Bhakat
Sahil Bhakat - avatar
+ 2
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. 😊
16th Feb 2019, 6:18 PM
Jay asodariya
Jay asodariya - avatar
+ 1
Yes
14th Feb 2019, 5:54 AM
Manav Malhotra
Manav Malhotra - avatar
+ 1
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.
14th Feb 2019, 6:08 AM
Sahil Bhakat
Sahil Bhakat - avatar
+ 1
You should use malloc or calloc functions to allocate memory dynamically.
22nd Feb 2019, 2:45 PM
Bibhuti Bhusan Pradhan
Bibhuti Bhusan Pradhan - avatar
0
Can u give me the example
14th Feb 2019, 5:52 AM
Manav Malhotra
Manav Malhotra - avatar
0
Of how to dynamically allocate memory ?
14th Feb 2019, 5:53 AM
Sahil Bhakat
Sahil Bhakat - avatar