473,218 Members | 1,474 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,218 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 10218
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 {...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.