How to overwrite 'new' operation in c++

X

Xianzheng Zhou

Hi all,

I heard new/delete operations could be overwritten with customed functions.

Just wonderring how this could be achieved. Could any one post some
suggestions please

Thanks:)

Joe
 
B

Barry

Xianzheng said:
Hi all,

I heard new/delete operations could be overwritten with customed functions.

Just wonderring how this could be achieved. Could any one post some
suggestions please

Most of the cases, you *overload* /operator new/ to manage memory
yourself, as the heap offered by CRT is created to meet common needs,
not for small object allocation, etc.
And on some embedded platform, they even don't provide dynamic memory
allocation, you can only acquire a large trunk, then manage it yourself.
And you can even overload operator new for memory leak detection.
And as a rule of thumb, overload operator new and operator delete in
pair.

And some overloaded operator new is invented to meet special need, a
famous one is ELeave operator new on Symbian.

How to overload operator new? Find it in a textbook.
 
J

James Kanze

I heard new/delete operations could be overwritten with
customed functions.

I presume you mean "overridden", not "overwritten".
Just wonderring how this could be achieved.

In a very real way, it depends on the implementation, but for
all of the implementations I know, if you define an operator new
and an operator delete function, and arrange for them to be
linked in before the standard library is linked in, then your
versions will be used instead of the standard ones.

Note that you should never do this except at the final
application level (e.g. to use a debugging new/delete, instead
of the standard one).
 

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
474,201
Messages
2,571,048
Members
47,651
Latest member
VeraPiw932

Latest Threads

Top