You are here:

C++/concept of heap

Advertisement


Question
hello!
im a BS-information technology student.i have a problem with my assignment.my topic is all about "the concept of heap in c++"??can you tell me what is the concept of heap in c++ all about?.please give me also examples of easy program that is related using the concept of heap??im going to report it in our class using powerpoint.

thanx in advance!!

Answer
Hello sean, thank you for the question.

The heap is a block of memory that can be managed by the programmer directly, as opposed to the stack which is managed by the computer itself.

If you take some memory from the heap, you have to explicitly put it back. Otherwise you leak memory. You take memory from the heap with the keyword new, and put it back with the keyword delete. Here is a C++ example.

// Variable p is on the stack, points to the new memory in the heap
int *p = new int;

// Assign a value to p
(*p) = 5;

// Print
cout << "Variable is located at heap address: " << p << " with value: " << (*p);

// Now we are done with p, so give it back
delete p;

Note that if we didn't delete the variable p, the memory that p pointed to would be lost until you restarted the computer.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.