+ 1
What is dynamic memory allocation??
3 Answers
+ 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;
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 ];
0
Did not get the example , plz elaborate