Page 1 of 1

The HEAP segment in C++ Rate Topic: -----

#1 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Posted 29 October 2010 - 01:34 PM

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 :) ). Hope it helped someone. The next thing are lists (in heap, in arrays, even working with simple arrays as with lists). So, after i finish them, I could make a tut.This way i can revise everything, and help DIC too :)

PS: Sorry if i have a bad grammar/suck at English :) It's not my native language :)

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


Is This A Good Question/Topic? 1
  • +

Replies To: The HEAP segment in C++

#2 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 689
  • Joined: 16-February 09

Posted 10 November 2010 - 12:21 PM

I have a question. If a programmer had a solid understanding about pointers wouldn't they know about the heap?
Was This Post Helpful? 0
  • +
  • -

#3 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Posted 12 November 2010 - 12:21 PM

I'm not sure, i'm learning too :), and i don't really think that i have a solid understanding of pointers :) .But, if it helps you, i knew pointers before getting into heap.I mean, in what i've read, the heap segment and pointers are different things (in different chapters, i mean).But, you have to "call"(or use, i hope you get the point, i'm not a native english speaker :) ) some certain bits in heap, and that's when pointers are helpful.I hope it helped :)

PS: For a shorter version, you can't use heap without using pointers.But you can use pointers in many other ways, not only for the heap segment.

This post has been edited by Patrunjel: 12 November 2010 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#4 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Posted 29 March 2011 - 09:07 PM

View PostPatrunjel, on 29 October 2010 - 09:34 PM, said:

Using the heap segment in C++
(or, the fancy term, Dynamic memory allocation)

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.


This is factually incorrect. Generally speaking, each C++ program address space consists of data segments (read/write), code segments (read/execute), constant segments (ready only) and a stack segment (read/write). The heap is not a segment, but a read/write area of the program address space used by the operating system a special way. A program is at liberty to create and delete heaps as they wish, own multiple heaps and each heap can be of a fixed size or growable. The C++ new operator uses a heap for it's operation.

View PostPatrunjel, on 29 October 2010 - 09:34 PM, said:

Using the heap segment in C++
(or, the fancy term, Dynamic memory allocation)
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)


Heaps are a necessary evil in C++. They are grossly inefficient, and developers have to be very careful in their allocation and deallocation strategies. Leaving dangling pointers that point to areas on the heap that have been freed is a major source of errors and can be very time-consuming to locate and correct.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1