Posts tagged new
How to hook memory allocation failures by malloc()?
1![]()
C have malloc() and C++ have more advanced “new” for allocating memory. One of the advantages of new over malloc is that its tailored to report memory allocation failures where malloc() is not. For instance, in C++ we can set a “Allocation failure handler” function by calling _set_new_handler(). When ever “new” fails to allocate function, this function will be called. Its a nice feature that malloc() don’t have by default.
There are several maintenance projects which are actually done in C but ported to C++. Usually instead of rewriting entire source, the legacy code exists in C itself and new feature additions will be done in C++, since they co-exist with each other. So in the old C code base, the malloc() will be used extensively. So hooking memory failures by malloc() are necessary and how can we do it?
![]()
By default malloc() won’t call the New Handler function during memory failure. But you can specify to do so by calling _set_new_mode(). See the sample code snippet below.
#include "new.h"
// Memory Allocation Failure Handler.
int AllocationFailureHandler( size_t )
{
// Do what ever you wish.
return 0;
}
// The one and only Main.
void main()
{
// Set the NewHandler and try "new" allocation failures.
// The NewHandler will be called.
_set_new_handler( AllocationFailureHandler);
char* pAllocatedByNew = new char[1000000000000];
// Check malloc. By default it won't call
// newHandler on failure.
char* pAllocatedByMalloc = (char*)malloc( 1000000000000 );
// Enable allocation failure reporting for malloc.
// and try once again. This time, NewHandler is called!
_set_new_mode( 1 );
pAllocatedByMalloc = (char*)malloc( 1000000000000 );
}
Now hook the memory allocation failures from you legacy code too…
![]()
If you have MFC support, then the NewHandler provided by you will be overridden and that of MFC will handle the allocation failure. So take care…
And _set_new_mode() is not the part of C++ language standard. Its Microsoft specific.
![]()
Targeted Audiance – Intermediate.
Construct C++ objects at predefined Memory Locations.
0![]()
Usually we use new operator to allocate objects. The new operator will allocate memory for object and calls the object’s constructor. We can construct our object in predefined memory location by using Placement new.
![]()
In placement new, you can pass the preferred memory location to the new operator at which the object to be constructed. During placement new, the new operator just calls the constructor. Responsibility of memory allocation is ours. After usage we should call the destructor manually to delete the object. Please see the following code block about usage of placement new.
// Allocate memory ourself char* pMemory = new char[ sizeof(MyClass)]; // Construct the object ourself MyClass* pMyClass = new( pMemory ) MyClass(); // TODO: Use pMyClass and enjoy!!! // The destruction of object is our duty. pMyClass->~MyClass();
![]()
The advantages of placement new are as follows -
1) Can allocate a pool of memory and construct objects on our wish.
2) This prevents memory allocation failure fear in runtime, because pool of memory is already allocated.
3) Object creation will be a bit faster, because memory is already allocated and only the constructor is to be called.
4) Since you can place your objects at your preferred memory locations, its very useful in embedded programming. For instance the hardware may be placing 255 bytes of data at memory location – 0×1000. In that case, write a class with an array of 255 Bytes as member variable and construct the object at 0×1000. Now you can access the data by using that object.
And one more interesting bit of information – The placement new is the only scenario in which we are legally allowed to call destructor explicitly as per C++ standards. The “New” New informations are interesting… isn’t it?

Targeted Audience – Intermediate.