Writing your own 'new' operator ?

Started by
6 comments, last by Skid 24 years ago
Are there any implementations of the ''new'' operator I could take a look at to find out how it works and how to go about writing a custom one ? Any links to memory management techniques would be great as well. Thanks
Advertisement
whats wrong with the ''new'' keyword?
Here''s a brainless approach just so you can see the syntax:

void* operator new (unsigned int num_bytes)
{
return malloc(num_bytes);
}

void operator delete (void* p)
{
if (p) free(p);
}


www.gameprojects.com - Share Your Games and Other Projects
Most overloads of new I''ve seen are simply calls to custom mallocs. One relatively non-trivial set of overloads for new can be seen in Boehm''s garbage collection library available from Xerox. But even that seems to be just special if statements to handle various placement options to it''s malloc version.
I think it''s not necessary to write an own implementation of the new operator, you could call malloc() instead of using it. The memory managment is done by system so you don''t need to care so much about it.

Visit our homepage: www.rarebyte.de.st

GA
Visit our homepage: www.rarebyte.de.stGA
Actually, ga, overloading the new operator is useful when implementing garbage collection or leak tracking transparently. Not to mention the syntax is cleaner to call new rather than a call to malloc and then a call to the constructor.
Overloading new can be useful in many situations:

1) Using domain-specific knowledge to optimize memory allocation. For instance, if you are using buffers of a known size you can preallocate a large block of buffers and use an overload of new to divy them out. Note that in general, using a lot of fixed-size allocators can explicitly fragment the heap.

2) You may need to override new to allocate memory from a specific area - i.e. shared memory.

3) You can overload new to include more domain-specific debugging information in your debug builds.

4) I once overloaded new so I could automatically detect if an object had been allocated from the heap or allocated statically on the stack or as part of another object. I needed this for a reference counting mechanism I developed - I would turn off reference counting on statically allocated objects.

As far as reading about memory allocation in general, any good Operating Systems book is going to have a stuff about heap management. I recommend "Modern Operating Systems" by Andrew Tanenbaum.

-vince


-vince


Actually, I heard you can explicitly state the memory location to create the object in, like so:
Data = new(Location) Type(Parameters);


So instead of pushing data onto the stack:
RECT* pRect = new RECT;


You allocate it at a specified location, dwLocation, like this:
RECT* pRect = new(dwLocation) RECT;

I''ve never actually tried it, so I may have gotten the syntax wrong.

~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement