Thursday, February 25, 2010

Memory Allocation

to Allocate memory is an easy process but to allocate memory properly is a tricky part and it helps make you're program more efficient.
So how to allocate memory properly :
1- create a pointer to use it to pint at the memory.
2- Allocate memory and set it or assign it to what you need .
3- Use the Memory in you're program.
4- De allocate the memory allocated.
that seems pretty forward and easy but what really hard is when you find the memory is too big or too small for your data so you create another array ! wrong that causes a memory leak. The programmer needs to reallocate memory.
How to reallocate memory :
1-Create temporary pointer
2- allocate the size of memory you need to use .
3- copy old data to new array .
4- de allocate old memory.
5- use the original pointer point to the new memory.
6- Don't delete temporary pointer it's gonna be deleted automatically.
The point of doing all of these is keeping your program out of pugs.

0 comments: