+ 5
What's dynamic memory allocation?
9 Respostas
+ 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/
+ 4
It is basically memory allocation at run time
+ 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.
+ 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. 😊
+ 1
Yes
+ 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.
+ 1
You should use malloc or calloc functions to allocate memory dynamically.
0
Can u give me the example
0
Of how to dynamically allocate memory ?