I am looking into writing C++ memory management using plain new and delete in C++. I am using
this classic article as reference.
Here is the code snippet defining how new is overridden. The various parts of this snippet are not necessarily in the same .cpp file.
class Heap
{
protected:
virtual ~Heap();
public:
virtual void* allocate(size_t) = 0;
static Heap& whatHeap(void*);
};
extern Heap* __global_heap;
inline void* operator new(size_t sz)
{
return ::__global_heap->allocate(sz);
}
extern class HeapAny __THE_global_heap;
Heap* __global_heap = &__THE_global_heap;
Omitted from the snippet is the definition of the class HeapAny. This derives from Heap and the details of what it does aren't important to my query. Please also ignore the whatHeap() function in the Heap class.
My query revolves around the instantiation of __THE_global_heap and assignment of it to __global_heap as in the last two lines of the program. As these are globally defined, they are created before main() is called and this invokes the overloaded new().
As the overloaded new() accesses __global_heap, yet __global_heap, or at least what it is pointing to hasn't been set up yet and is in fact the reason why the new() is being called, I can't see how this code works.
I've tried this to see how it works out. Basically __global_heap appears to be NULL and the program crashes in the new statement, before main().
If I change the new to...
inline void* operator new(size_t sz)
{
if( __global_heap == NULL ) return malloc( sz );
return ::__global_heap->allocate(sz);
}
...then the program works as planned. However, I don't want every occurence of my overloaded new to be slowed down with 'if NULL' style checks.
Does anyone have any familiarity with the article I am looking at, or perhaps some comment on how I might be able to control the problems I am looking at?