What is dynamic memory allocation?? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

What is dynamic memory allocation??

25th Oct 2016, 9:23 PM
Mayank Kumar
Mayank Kumar - avatar
3 Réponses
+ 3
It's memory allocated at run time, on the heap. In C++, you do that with the keyword new, and free the memory later with the keyword delete. Example: int *ans = new int(42); cout << *ans << endl; delete ans;
25th Oct 2016, 11:45 PM
Zen
Zen - avatar
0
Say you have a certain number of player objects from a class, but you don't know how many will be playing. Using dynamic memory, you can declare however many player needed after the user inputs how many will be playing: cout << "How many players are there? << endl; cout << ">> "; cin >> nPlayers; PlayerClass *pPlayer = new PlayerClass[ nPlayers ];
26th Oct 2016, 4:40 AM
Zeke Williams
Zeke Williams - avatar
0
Did not get the example , plz elaborate
26th Oct 2016, 7:37 AM
Akshay Kumar Tak
Akshay Kumar Tak - avatar