|C++| Dynamic Memory.
Short explanation and examples of dynamic memory allocation in c++.
Generally, when you are coding, you have to specify the amount of variables that will be in your program, but, after compiling, you cannot change the quantity of them. The dynamic memory allows us to allocate memory (from the heap) and free it while our program is running.
For that, we are going to use the new and delete operators and some pointers. Always remember to free the memory allocated, otherwise there will be a memory leak. The types must be the same.
float* ptrNewVariable = new int; //Wrong.
float* ptrNewVariable = new float; //Right.
int* ptrNewVariable = new int; //We have a new int variabledelete ptrNewVariable; //We free it.
Example: We are doing a game, so we ask the player how many monsters he would want to fight with.
nt main()
{cout << “How many monsters?” << endl;
int nNumber;
//Store the number in a variable
cin >> nNumber;//We declare a pointer that will point to a CMonster object. CMonster is an hypothetical class.
CMonster* ptrMonsters = new CMonster[nNumber];//To access the objects:
ptrMonsters[ index ]->Fight(); //Just like a normal array.
//When we are finished we free the memory.
delete[] ptrMonsters; //Remember the array form of delete.
ptrMonsters = NULL; //We set it to null so it will not be pointing to memory that is not allocated.return 0;
}
Remember :
- When we use new , we must use delete.
- When we use new[], we must use delete[].
Avoid memory leaks.
Situation 1.
void Search()
{int* ptrNewVariable = new int[5]; //New dynamic allocated variable.
//We didn't use delete[] ptrNewVariable. Oopss.
//End of function. The pointer is deleted and the memory address is lost. Memory leak.
}
Situation 2.
int* ptrNewVariable = new int; //The pointer holds the variable address.ptrNewVariable = new int; //Memory leak here, the pointer holds the new variable address,
//but lost the previous one, so you will not be able to free it.delete ptrNewVariable; //Delete the last one allocated memory, but not the first one.
-
Did Asset Allocation Fail Investors in 2008? (Morningstar)
| By 5min | in Coaching
PIMCO subadvisor and asset allocation specialist Robert Arnott talks to Morningstar about how investors should view...
-
Consider A CTA Managed Fund For Balanced Asset Allocation
| By dstrocen | in Investing
There are many investment strategies for both the novice and sophisticated investor. The CTA managed fund has been...
-
Sony Ericsson XPERIA X1, High allocation of Intelligent Machines
| By linshaoearn | in General
We show the actual contact at X1, it was found that X1 is not really the body, almost can be used to describe the s...
-
How to Manage Your Income Wisely?
| By Indra | in Personal Finance
It is important to allocate your money well in managing your income. And it is also important to control your expen...
-
How to Hack or Crack a Windows XP Administrator Password | By pkumarb89 | in Programming
This is provided only for educational purpose it is a simple way to Recover, Hack or Crack the Window XP Administra...
-
How to Create a Search Feature with PHP and MySQL | By Junedseo | in Programming
Developing a robust, interactive and engaging Web site involves many different avenues, such as interactive pop-out...
-
What Is Hacking? | By faris_jayz | in Programming
It is not that difficult to hack into a computer system. The most popular definition for hacking is the act of obta...
-
How do I enable JavaScript in browser? | By myjobs | in Programming
way to enable the javascript in different types of browser....
-
iPhone Core Data Tutorial Part 3.2 | By eh9212 | in Programming
How to use To-Many relationships in core data...
Nothing Found!
Why not submit your own content? Signup here.








No comments yet.