Using the heap segment in C++
(or, the fancy term, Dynamic memory allocation)
Before reading this you are expected to know and understand pointers, and the indirect selection operator ( -> ). If anyone needs help on these two things, just tell me and I can post a tutorial on them too.
1)What's the HEAP?
2)Allocating space on the HEAP
3)Deallocating space on the HEAP
4)Arrays on the HEAP
5)Deallocating arrays on the HEAP
1) What's the HEAP?
Basically, every program gets 3 spaces in the computer's memory. One is the data segment, the other one is the stack segment, and the third (but not least) is the heap. In these three areas you will basically find the program's variables.
So, by using dynamic memory allocation we can allocate variables on the heap. This happens while the program is running.
Advantages:
-The program will use as much memory as needed. If a variable is not helpful anymore, we can always free the memory area it's using. This leads to an economy on the usage of the computer's resources.
-Some data structures are very easy to implement using heap memory. An example would be lists (about which I can make a tutorial if anyone's interested)
2) Allocating space on the HEAP
You can't just put variables in the heap segment. You first have to allocate space (kind of like declaring variables, but fancier).
First, you need something to point the address of the variable. Here is where a pointer comes in handy. After you declare a pointer, you have to "lead" him to the HEAP segment, so you use the new operator. The syntax is simple: pointer_name=new pointer_type.
And, if you want to make things shorter, you can always point a pointer to heap when you declare it, like this: pointer_type (*name)=new pointer_type. For example, to create a pointer to an integer on the heap, you would do:
int *myInt = new int;
3) Deallocating space on the HEAP
You must remember that a variable stays in heap until the program stops running, or you use the delete operator.
This is simple, you just have to type delete variable_name and it's done. Again, for example:
delete myInt;
4) Arrays on the heap
To do this, you need to know what an array's name is. It's a constant that identifies the first element in the array, and then for example, for int a[4] , if we wanna walk about a[2] the computer thinks like this: "Ok, now i just do &a+(size_of_int * 2)". So, to declare an array on the heap we use this: type_name *array_identifier=new type_name [array_size]. Again, to create an array of integers on the heap, one would do this:
int *myInts = new int[10];
And you can now access the elements of myInts just like an array:
for (int i = 0; i < 10; ++i)
{
myInts[i] = i * 2;
}
As simple as that (Not! Believe me
5) Deleting arrays on the heap
After allocating an array on the heap with new, you must use a slightly different syntax in order to delete all the memory associated with the array. That syntax is delete [] array_identifier. Again, by way of example:
delete [] myInts
PS: I've done this tutorial to like get everything i learned (I'm on heap too
PS: Sorry if i have a bad grammar/suck at English
Additional info: Anatomy of a Program in Memory
This post has been edited by JackOfAllTrades: 10 November 2010 - 01:05 PM
Reason for edit:: Tightening up, grammar and spelling fixes, additional info






MultiQuote





|