473,804 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overriding new and delete.

Hi list.

Simple question:
is it possible to override the global new/delete operators, without
using malloc/free?
I mean something in the ideea of the code below, which doesnt work
cause of
the recursivenes.
What I'm trying to say is how to override new/delete using the original
global
operators? ( if this is possible )
#include <stdio.h>
#include <malloc.h>
#include <new>

void*
operator new( size_t sz )
{
printf( "new\n" ) ;
return ::operator new( sz );
//return malloc( sz );
}

void
operator delete( void* p )
{
printf( "delete\n" ) ;
::operator delete( p );
//free( p ) ;
}
int
main()
{
int* i = new(int) ;
delete i;
return 0;
}

Dec 28 '05 #1
9 10285
gr****@gmail.co m wrote:
Hi list.

Simple question:
is it possible to override the global new/delete operators, without
using malloc/free?
I mean something in the ideea of the code below, which doesnt work
cause of
the recursivenes.
What I'm trying to say is how to override new/delete using the original
global
operators? ( if this is possible )
#include <stdio.h>
#include <malloc.h>
#include <new>

void*
operator new( size_t sz )
{
printf( "new\n" ) ;
return ::operator new( sz );
//return malloc( sz );
}

void
operator delete( void* p )
{
printf( "delete\n" ) ;
::operator delete( p );
//free( p ) ;
}
int
main()
{
int* i = new(int) ;
delete i;
return 0;
}


Are you trying to do this because you're having problems when you call
malloc and free.
Be aware that if you try to use the above code for some type of
debugging logic, you could experience problems if you connect to a
module that uses a different library with different allocators that are
not compatible.
The best way to handle this, is to move the operators into a header,
and make them an inline function.
Example:
inline void operator delete(void* memblock)
{
free(memblock);
leaktracker61_l og_delete_obj_m em(memblock, 0);
}
inline void operator delete[](void* memblock)
{
free(memblock);
leaktracker61_l og_delete_obj_m em(memblock, 1);
}
inline void * operator new(size_t bytes, const char *file, int line,
const char* FunctionName){
return leaktracker61_l og_new_obj_mem( malloc(bytes), 0, file, line,
FunctionName);
}

inline void * operator new[](size_t bytes, const char *file, int line,
const char* FunctionName){
return leaktracker61_l og_new_obj_mem( malloc(bytes), 1, file, line,
FunctionName);
}
Notice in the above code, that the new operators call malloc(), and
pass the result to the real debugging function.
By using this inline method, you can insure that the right allocators
and deallocators are called, and still be able to pass on the debugging
information to your target debugging logic.
For more info, and a full example, check out the following link, which
has a free leak tracking program:
http://code.axter.com/leaktracker.h

You can download the associated DLL & LIB file via following link:
http://code.axter.com/leaktracker.zip

Dec 28 '05 #2
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
"Axter" <go****@axter.c om> wrote:
The best way to handle this, is to move the operators into a header,
and make them an inline function.
Example:
inline void operator delete(void* memblock)


Be aware that the standards committee thinks this is a bad idea:

http://www.open-std.org/jtc1/sc22/wg...fects.html#404

and has already voted to make it illegal for C++0X.

-Howard
Dec 28 '05 #3

Howard Hinnant wrote:
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
"Axter" <go****@axter.c om> wrote:
The best way to handle this, is to move the operators into a header,
and make them an inline function.
Example:
inline void operator delete(void* memblock)


Be aware that the standards committee thinks this is a bad idea:

http://www.open-std.org/jtc1/sc22/wg...fects.html#404

and has already voted to make it illegal for C++0X.


Exactly what is a bad idea?
Are you referring to item 9 in above link? If not, which item?

Dec 28 '05 #4
In article <11************ *********@o13g2 000cwo.googlegr oups.com>,
"Axter" <go****@axter.c om> wrote:
Howard Hinnant wrote:
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
"Axter" <go****@axter.c om> wrote:
The best way to handle this, is to move the operators into a header,
and make them an inline function.
Example:
inline void operator delete(void* memblock)


Be aware that the standards committee thinks this is a bad idea:

http://www.open-std.org/jtc1/sc22/wg...fects.html#404

and has already voted to make it illegal for C++0X.


Exactly what is a bad idea?
Are you referring to item 9 in above link? If not, which item?


Item 404. Sorry, my browser took me right there with the trailing #404.
I thought it would for you too.

-Howard
Dec 28 '05 #5

Howard Hinnant wrote:
In article <11************ *********@o13g2 000cwo.googlegr oups.com>,
"Axter" <go****@axter.c om> wrote:
Howard Hinnant wrote:
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
"Axter" <go****@axter.c om> wrote:

> The best way to handle this, is to move the operators into a header,
> and make them an inline function.
> Example:
> inline void operator delete(void* memblock)

Be aware that the standards committee thinks this is a bad idea:

http://www.open-std.org/jtc1/sc22/wg...fects.html#404

and has already voted to make it illegal for C++0X.


Exactly what is a bad idea?
Are you referring to item 9 in above link? If not, which item?


Item 404. Sorry, my browser took me right there with the trailing #404.
I thought it would for you too.

-Howard

After I clicked it the second time, it took me there.
I don't completely agree with the rationale on that item.
Specifically, the last part of the Rationale:
*************** *************** *************** *******
, and is believed to be of limited value.
*************** *************** *************** *******

I've seen this used in other debug or leak tracking implementations .

Dec 28 '05 #6
In article <11************ **********@f14g 2000cwb.googleg roups.com>,
"Axter" <go****@axter.c om> wrote:
Howard Hinnant wrote:
In article <11************ *********@o13g2 000cwo.googlegr oups.com>,
"Axter" <go****@axter.c om> wrote:
Howard Hinnant wrote:
> In article <11************ **********@g43g 2000cwa.googleg roups.com>,
> "Axter" <go****@axter.c om> wrote:
>
> > The best way to handle this, is to move the operators into a header,
> > and make them an inline function.
> > Example:
> > inline void operator delete(void* memblock)
>
> Be aware that the standards committee thinks this is a bad idea:
>
> http://www.open-std.org/jtc1/sc22/wg...fects.html#404
>
> and has already voted to make it illegal for C++0X.
>

Exactly what is a bad idea?
Are you referring to item 9 in above link? If not, which item?


Item 404. Sorry, my browser took me right there with the trailing #404.
I thought it would for you too.

-Howard

After I clicked it the second time, it took me there.
I don't completely agree with the rationale on that item.
Specifically, the last part of the Rationale:
*************** *************** *************** *******
, and is believed to be of limited value.
*************** *************** *************** *******

I've seen this used in other debug or leak tracking implementations .


It sure would be easy to forget to include your inlined operator new in
one of your translation units, and silently get the system operator new.
That pointer could then be passed to another translation unit which did
remember to include the inlined operators and subsequently be deleted by
your (possibly incompatible) inlined operator delete.

By making your operators not inlined, the linker will guarantee that you
have only one definition of these operators in your program.

If you're concerned about different operator definitions across shared
library boundaries, join the club. So am I. :-) But making them inline
now means you need to worry about different operator definitions across
translation unit boundaries. Imho it makes the problem worse.

What happens if you don't have access to all translation units from
which you're taking ownership of a new'd pointer? This could easily
happen with use of <strstream> (for example), or possibly even
get_temporary_b uffer from <memory>. Your std::lib may hand you a
pointer allocated from the system new and you'll deallocate it with your
custom delete.

I've written leak checkers myself. They're an incredibly useful tool.
In my experience they work fine with non-inlined operators.

-Howard
Dec 28 '05 #7
After looking at libstdc++ and the 1998 c++ draft, it is recomended
that
new and delete should use malloc and free, so I would answer myself :P,
that the global operators can't be overloaded makeing use of the
original ones.

Another thing with Axter example is that when you overload this type of
"new":
void * operator new[](size_t bytes, const char *file, int line,
const char* FunctionName)
that is new (2,f) T ;
you might get some errors at statements that allready have their
(new-placement) used.

Anyway it would be nice if some compilers/libraries provided debug
versions
of new and delete.

Dec 29 '05 #8
gr****@gmail.co m wrote:
After looking at libstdc++ and the 1998 c++ draft, it is recomended
that
new and delete should use malloc and free, so I would answer myself :P,
that the global operators can't be overloaded makeing use of the
original ones.


The other thing that's important to notice in the standard is that these
functions are described as "replaceabl e". When you provide your own
(with the particular signatures at issue), you "displace" the ones
provided by the library.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Dec 29 '05 #9

Howard Hinnant wrote:
In article <11************ **********@f14g 2000cwb.googleg roups.com>,
"Axter" <go****@axter.c om> wrote:
Howard Hinnant wrote:
In article <11************ *********@o13g2 000cwo.googlegr oups.com>,
"Axter" <go****@axter.c om> wrote:

> Howard Hinnant wrote:
> > In article <11************ **********@g43g 2000cwa.googleg roups.com>,
> > "Axter" <go****@axter.c om> wrote:
> >
> > > The best way to handle this, is to move the operators into a header,
> > > and make them an inline function.
> > > Example:
> > > inline void operator delete(void* memblock)
> >
> > Be aware that the standards committee thinks this is a bad idea:
> >
> > http://www.open-std.org/jtc1/sc22/wg...fects.html#404
> >
> > and has already voted to make it illegal for C++0X.
> >
>
> Exactly what is a bad idea?
> Are you referring to item 9 in above link? If not, which item?

Item 404. Sorry, my browser took me right there with the trailing #404.
I thought it would for you too.

-Howard After I clicked it the second time, it took me there.
I don't completely agree with the rationale on that item.
Specifically, the last part of the Rationale:
*************** *************** *************** *******
, and is believed to be of limited value.
*************** *************** *************** *******

I've seen this used in other debug or leak tracking implementations .


It sure would be easy to forget to include your inlined operator new in
one of your translation units, and silently get the system operator new.
That pointer could then be passed to another translation unit which did
remember to include the inlined operators and subsequently be deleted by
your (possibly incompatible) inlined operator delete.

By making your operators not inlined, the linker will guarantee that you
have only one definition of these operators in your program.

If you're concerned about different operator definitions across shared
library boundaries, join the club. So am I. :-) But making them inline
now means you need to worry about different operator definitions across
translation unit boundaries. Imho it makes the problem worse.

What happens if you don't have access to all translation units from
which you're taking ownership of a new'd pointer? This could easily
happen with use of <strstream> (for example), or possibly even
get_temporary_b uffer from <memory>. Your std::lib may hand you a
pointer allocated from the system new and you'll deallocate it with your
custom delete.


Experts like Herb Sutter, recommend that if you create it, then you
should delete it.
In other words, if your translation unit creates an object, then it
should be responsible for deleting the object.
You should not pass it on to another translation unit.

I've written leak checkers myself. They're an incredibly useful tool.
In my experience they work fine with non-inlined operators.

-Howard


It's been my experience, that they don't work when not using inline
method.
In order for it to work in a non-inline mode, then you would have to
compile the leaktracker.dll with the same runtime library.
I have not had any problems using the inline method, and I've tested it
in multiple compilers, and multiple platforms.

Dec 29 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1694
by: Andrew Wilkinson | last post by:
Hi, This is probably more of a gcc question than a python one, but hopefully someone out their will know that answer. I've written a C++ Python extension, and as part of it I override the operator new and operator delete functions to provide memory leak tracking from within the extension. When I use the code as part of a complete C++ program everything works
1
2315
by: Jesper Madsen | last post by:
Can I confine overrides of new and delete to certain namesspaces, f.ex. by just introducing the modfied operator new in namespace GUI?? Or should I ask, if I declare operator new in a namespace, is it still global operator new I define?? This is just for doing some memory tracking, so it would be nice if I could keep namespace std:: out of the game.. and avoid STL. I do not have any using std namespace; or such in my code, so this will...
3
7806
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot declare the function a static linkage. Why? C++ is so complex and so many situation should be considered.
6
5896
by: shoosh | last post by:
hi for my application under VC6 I need to implement my own memory manager which overrides the global new and delete operators and which Do Not use the normal free() and malloc(). it seemed to work fine on its own but when I tried to use say 'cout' or 'cerr' from STL the tester crashed upon termination. using some breakpoints I found out that when I make use of 'cerr' in the program, there is a call to my override of delete which do not...
3
1679
by: Gonçalo Rodrigues | last post by:
Hi all, I have a base class, call it Object, that implements operators new and delete. Now suppose there is also a class, call it Derived, deriving from Object. It has the feature that *all* its instances are statically allocated so I have to override new and delete. Since *all* instances are statically allocated (and already fully
2
2026
by: byoukstetter | last post by:
So, I have an interface with several overriding methods: using System; using System.Collections.Specialized; namespace some.name.space { public interface IVrsPersistenceProvider { string Select(string idName, string tableName, string whereClause);
4
11463
by: tomlong | last post by:
Hi, I have a tabular form that each TR has an onclick event to edit the row. I also have a div in one of the cells on each row that is a delete button. My problem is that both onclicks are being fired when I click on the delete div-button, and as a result the item is getting deleted, then is opened up for editing. How do I prevent the TR onclick from happening when the delete button is used?
0
1747
by: jerinjoy | last post by:
I'm trying to track memory leaks by keeping track of the new and delete operators are called. I've declared new operator as: void* operator new (size_t size, const char *file, int line) { //code to store info abt the new call. } I want to define macros that can replace new and delete calls in existing code with a new that is passed object type, current line number and file name. eg:
1
11451
by: SarahT | last post by:
Hi folks, I am doing something Very Bad and Wrong (which I'll spare you the details of) that requires overloading new for some specific classes. So, for example: class MyWeirdThingy { public: ...
0
9712
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10089
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9171
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.