473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 10229
gr****@gmail.com 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_log_delete_obj_mem(memblock, 0);
}
inline void operator delete[](void* memblock)
{
free(memblock);
leaktracker61_log_delete_obj_mem(memblock, 1);
}
inline void * operator new(size_t bytes, const char *file, int line,
const char* FunctionName){
return leaktracker61_log_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_log_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**********************@g43g2000cwa.googlegroups .com>,
"Axter" <go****@axter.com> 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**********************@g43g2000cwa.googlegroups .com>,
"Axter" <go****@axter.com> 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*********************@o13g2000cwo.googlegroups. com>,
"Axter" <go****@axter.com> wrote:
Howard Hinnant wrote:
In article <11**********************@g43g2000cwa.googlegroups .com>,
"Axter" <go****@axter.com> 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*********************@o13g2000cwo.googlegroups. com>,
"Axter" <go****@axter.com> wrote:
Howard Hinnant wrote:
In article <11**********************@g43g2000cwa.googlegroups .com>,
"Axter" <go****@axter.com> 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**********************@f14g2000cwb.googlegroups .com>,
"Axter" <go****@axter.com> wrote:
Howard Hinnant wrote:
In article <11*********************@o13g2000cwo.googlegroups. com>,
"Axter" <go****@axter.com> wrote:
Howard Hinnant wrote:
> In article <11**********************@g43g2000cwa.googlegroups .com>,
> "Axter" <go****@axter.com> 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_buffer 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.com 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 "replaceable". 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**********************@f14g2000cwb.googlegroups .com>,
"Axter" <go****@axter.com> wrote:
Howard Hinnant wrote:
In article <11*********************@o13g2000cwo.googlegroups. com>,
"Axter" <go****@axter.com> wrote:

> Howard Hinnant wrote:
> > In article <11**********************@g43g2000cwa.googlegroups .com>,
> > "Axter" <go****@axter.com> 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_buffer 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
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...
1
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...
3
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...
6
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...
3
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*...
2
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...
4
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...
0
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) { ...
1
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 {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.