C++/concept of heap
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!!
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;
cout << "Variable is located at heap address: " << p << " with value: " << (*p);
// Now we are done with p, so give it back
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.