Coupon Accepted Successfully!


Heap Consumption

The heap is used to allocate memory during program execution, that is, run time. In assembly language, there is no such thing as a heap. All memory is for the programmer, and he/she can use it directly. In various ways, C/C++ is a better programming environment than assembly language. However, a cost has to be paid to use either C or C++. The cost is separation from the machine. We cannot use memory anywhere; we need to ask for it. The memory from which we receive information is called the heap.
Fig: Heap review
The C/C++ compiler may place automatic variables on the stack. It may store static variables earlier than loading the program. The heap is the piece of memory that we allocate. Figure-Heap review shows a view of the heap.
Local variables are stored in the stack, and the code is in code space. The local variables are destroyed when the function returns. Global variables are stored in the data area, and they are accessible by all functions. The heap can be considered a huge section of memory in which large numbers of memory locations are placed sequentially.
As stated earlier, all local variables are stored in the stack. As soon as the function execution is completed, local variables are destroyed, and the stack becomes empty. The heap is not cleared until the program execution is completed. It is the user’s task to free the memory. The memory allocated from the heap remains available until the user explicitly deallocates it.
While solving problems related to memory allocation, we always believe that large memory is available, and the heap is at no time short of memory. It is bad programming to depend on such guesses, which may cause an error in application. In C, the function malloc() is used to allocate the memory, and if this function fails to allocate the memory, it returns NULL pointer. By checking the return value of the function malloc(), failure or success of the memory allocation is tested, and appropriate sub-routines are executed.
C++ allows us to apply similar logic with new operators. C++ allows us two function pointers known as _new_handeleri and set_new_handler. The _new_handler holds a pointer to a function. It requires no arguments and returns void.
If a new operator fails to allocate the memory requested, it will invoke the function *_new_handler and again, it will attempt the memory allocation. By default, the function *_new_handler directly closes the program. It is also possible to substitute this handler with a function that releases memory. This can be directly accomplished by executing the function set_new_handler that returns a pointer to the handler.

14.8 Write a program to use set_new_handler function.





void m_warn()


cerr << “\n Cannot allocate!”;



int main()




char *k=new char[50];

cout<<“\n First allocation:k=” << hex << long(k);

k=new char[64000U];

cout<<“\Second allocation:k=” << hex << long(k);
set_new_handler(0); // Reset to default.
return 0;


First allocation: k = 8fa40d48
Cannot allocate!

In the above program, the set_new_handler is initialized with the function m_warn(). The m_wanr() displays a warning message. The variable p is a character pointer and using the new operator memory for 50 characters, it is allocated to the pointer p. The cout statement displays the starting address of the memory allocated.
Consider the statement p = new char [64000U]. In this statement, the new operator attempts to allocate memory to the pointer p. In case the new operator fails to allocate the memory, it calls the setnew_handler, and m_warn() function is executed.

Test Your Skills Now!
Take a Quiz now
Reviewer Name