Possible to access standard global new operator after it's beenoverridden?

B

Billy

Is it possible to access the standard global new operator after it's been
overridden? For instance, I have a situation like this

void* operator new( size_t numBytes )
{
return sDefaultHeap->Allocate( numBytes );
}

Heap::Heap( unsigned int memSize )
{
// allocate memory for the heap
m_memory = new char[ memSize ];
}

void InitDefaultHeap( void )
{
// this function doesn't currently work because new has been
// overridden :(
char* defaultMem = new char[1024];
sDefaultHeap( defaultMem, 1024 );
}

The overridden new allocates from the default heap, but the problem is
that default heap must be allocated using the standard new operator and
not the overridden one. Is this possible? My heap class (not the one
shown) can take external allocations to take care of the default heap
case, but I can't seem to figure out how to make this all work.
 
A

Alf P. Steinbach

* Billy:
Is it possible to access the standard global new operator after it's been
overridden?

It's generally not a good idea to override the global new operators.

Many library functions assume very specific behavior from the global new
operators, and they are also customization points for applications that
handle out-of-memory conditions.

Instead, you can override per class.

For instance, I have a situation like this

void* operator new( size_t numBytes )
{
return sDefaultHeap->Allocate( numBytes );
}

Heap::Heap( unsigned int memSize )
{
// allocate memory for the heap
m_memory = new char[ memSize ];
}

void InitDefaultHeap( void )
{
// this function doesn't currently work because new has been
// overridden :(
char* defaultMem = new char[1024];
sDefaultHeap( defaultMem, 1024 );
}

I guess it's possible that sDefaultHeap is defined like this:

struct DefaultHeap
{
struct Foo
{
void* Allocate( size_t ) { ... }
};

Foo bar;

Foo* operator->() { return &bar; }

void operator()( char* p, int i ) { ... }
};

DefaultHeap sDefaultHeap;

but more likely I think the code you have presented is just some fantasy
code.

Please read the FAQ item on how to get help with code that doesn't work,
in particular, how to present such code.


Cheers, & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top