+ 1

Why dynamic memory allocation???

Hey guys I've been up learning c++ and I came across Dynamic memory allocation. And solo-learn taught me everything except why to use this feature I mean I honestly don't know what's the use or advantage of dynamic memory allocation what's the the point of learning it??("don't take it too hard on me guys sorry i'm just a beginner").Thanks in advance guys!!!

14th May 2017, 3:51 PM
Rahul
Rahul - avatar
4 Respostas
+ 2
Dynamic allocation is particularly important in situations where you have varying sizes of data to work with. It lets you tune how much space the program takes up. For example, I wrote a preprocessing program for a data analysis tool. The problem is that the input chunks varied from a few tens of thousands of 4-bit samples, to several million 2-bit samples. Now consider that the larger chunks were noticeably slower to run on our machines, the fine tuning meant I could test later parts of the data analysis faster (and save the slow runs to go overnight).
15th May 2017, 3:20 AM
Jim
Jim - avatar
+ 2
Say you want to the user to input the number of players in a game or something. Personally, I made a program that scores a scrabble game and to run the code, I need to be able to input how many players there are. The compiler won't let you do something like this: int nPlayers; cin >> nPlayers; PlayerClass allPlayers[nPlayers]; So you have to use dynamic memory allocation: int nPlayers; cin >> nPlayers; Player* pAllPlayers = new Player[nPlayers]; // do stuff delete [ ] pAllPlayers;
16th May 2017, 12:43 AM
Zeke Williams
Zeke Williams - avatar
+ 1
Dynamic memory allocation allows the program to request and use computer memory just when needed, and then release it so that other processes can also use it. If a program only uses 'static memory allocation' instead, that chunk of memory of in the computer is all used up as long as the program is running, so other processes may run out of memory and crash.
14th May 2017, 5:00 PM
ifl
ifl - avatar
0
thanks guys
16th May 2017, 5:25 AM
Rahul
Rahul - avatar