By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

How to redefine operator delete/delete[] via macro?

P: n/a
Amy
Hello,

We are developing C++ appplications for PDAs where memory is limited,
so we want to do memory management by ourselves --- pre-allocated a big
chunk and overwrite new and delete to call our APIs.

The tricky thing simply redefine operator new and delete because the
OS(PDA platform OS)library we need to link with in our application also
redefined operator new/delete for some purpose. Simply redefine
new/delete as following won't work as we get errors of "multiple
definition", which comes from the OS library we have to link to:

inline void* operator new(size_t size )
{ return MyMalloc( size ); }
inline void* operator new[]( size_t size )
{ return MyMalloc( size ); }
inline void operator delete(void* p )
{ MyFree( p ); }
inline void operator delete[]( void* p )
{ MyFree( p ); }

So we have to use a work around as following:

enum EMyNewType { EMyNew };
inline void* operator new( size_t size,
EMyNewType t)
{ return MyMalloc( size ); }

inline void* operator new[]( size_t size,
EMyNewType t)
{ return MyMalloc( size ); }

inline void operator delete( void* p,
EMyNewType t)
{ MyFree( p ); }

inline void operator delete[]( void* p,
EMyNewType t)
{ MyFree( p ); }

#define THENEW new (EMyNew)
#define new THENEW
#define THEDELETE delete (EMyNew)
#define delete THEDELETE

However, above work-around only works for "new", I got compiling errors
on code such as " delete []m_s; ", seems the compiler doesn't recognize
the delete[] we redefined. But there is no problem with code such as
"new char[10];" Do you happen to know why?

Thanks for any feedback!

Jul 23 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
supposing that you're using c++ (because of your posting in this ng), my
advice is: DO NOT USE MACROS. use the c++ feature "overloading of
operators" to achieve what you need.

On 21 Feb 2005 22:56:12 -0800, Amy <am******@yahoo.com> wrote:
Hello,

We are developing C++ appplications for PDAs where memory is limited,
so we want to do memory management by ourselves --- pre-allocated a big
chunk and overwrite new and delete to call our APIs.

The tricky thing simply redefine operator new and delete because the
OS(PDA platform OS)library we need to link with in our application also
redefined operator new/delete for some purpose. Simply redefine
new/delete as following won't work as we get errors of "multiple
definition", which comes from the OS library we have to link to:

inline void* operator new(size_t size )
{ return MyMalloc( size ); }
inline void* operator new[]( size_t size )
{ return MyMalloc( size ); }
inline void operator delete(void* p )
{ MyFree( p ); }
inline void operator delete[]( void* p )
{ MyFree( p ); }

So we have to use a work around as following:

enum EMyNewType { EMyNew };
inline void* operator new( size_t size,
EMyNewType t)
{ return MyMalloc( size ); }

inline void* operator new[]( size_t size,
EMyNewType t)
{ return MyMalloc( size ); }

inline void operator delete( void* p,
EMyNewType t)
{ MyFree( p ); }

inline void operator delete[]( void* p,
EMyNewType t)
{ MyFree( p ); }

#define THENEW new (EMyNew)
#define new THENEW
#define THEDELETE delete (EMyNew)
#define delete THEDELETE

However, above work-around only works for "new", I got compiling errors
on code such as " delete []m_s; ", seems the compiler doesn't recognize
the delete[] we redefined. But there is no problem with code such as
"new char[10];" Do you happen to know why?

Thanks for any feedback!


--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 23 '05 #2

P: n/a
Amy wrote:
#define THENEW new (EMyNew)
#define new THENEW
#define THEDELETE delete (EMyNew)
#define delete THEDELETE

However, above work-around only works for "new", I got compiling errors on code such as " delete []m_s; ", seems the compiler doesn't recognize the delete[] we redefined. But there is no problem with code such as
"new char[10];" Do you happen to know why?


There is no placement notation for 'operator delete()': if you call
a placement form of "new" (other than the 'std::nothrow' form) you
need to explicitly destroy your object and call 'operator delete()'
directly. Personally, I would also not define the keywords, at least
due to two reasons:
- The standard makes no provision allowing defines to change the
meaning of keywords.
- Changing known semantics to become something different is always
problematic.
I would use an appropriate wrapping of respective functions, e.g. in
the form of an allocator object.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #3

P: n/a
"ulrich" <ua********@aon.at> wrote in message
news:opsmlarj06n2mgp5@innsbruck-neu...
supposing that you're using c++ (because of your posting in this ng), my
advice is: DO NOT USE MACROS. use the c++ feature "overloading of
operators" to achieve what you need.


Did you even read his question?
Jul 23 '05 #4

P: n/a
On Tue, 22 Feb 2005 02:45:15 -0600, James Aguilar <jf**@cec.wustl.edu>
wrote:
"ulrich" <ua********@aon.at> wrote in message
news:opsmlarj06n2mgp5@innsbruck-neu...
supposing that you're using c++ (because of your posting in this ng), my
advice is: DO NOT USE MACROS. use the c++ feature "overloading of
operators" to achieve what you need.


Did you even read his question?


only until "macros" :)
Jul 23 '05 #5

P: n/a
On 2005-02-22, Amy <am******@yahoo.com> wrote:
Hello,

We are developing C++ appplications for PDAs where memory is limited,
so we want to do memory management by ourselves --- pre-allocated a big
chunk and overwrite new and delete to call our APIs.

The tricky thing simply redefine operator new and delete because the
OS(PDA platform OS)library we need to link with in our application also
redefined operator new/delete for some purpose. Simply redefine
new/delete as following won't work as we get errors of "multiple
definition", which comes from the OS library we have to link to:


Why not define it inside the namespace that your library uses ? That's
assuming that you're using a namespace and not polluting the global namespace.
If the latter is the case, the real problem you have is a namespace pollution
problem (both you and the vendor polluting the global namespace, and fighting
over it via ugly hacks)

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #6

P: n/a
Donovan Rebbechi wrote:

Why not define it inside the namespace that your library uses ? That's
assuming that you're using a namespace and not polluting the global namespace.
If the latter is the case, the real problem you have is a namespace pollution
problem (both you and the vendor polluting the global namespace, and fighting
over it via ugly hacks)


I'm pretty sure I read in the standard somewhere that overloading
new/delete in a different namespace does not work.
Jul 23 '05 #7

P: n/a
"ulrich" <ua********@aon.at> wrote in message
news:opsmlcjynan2mgp5@innsbruck-neu...
On Tue, 22 Feb 2005 02:45:15 -0600, James Aguilar <jf**@cec.wustl.edu>
wrote:
"ulrich" <ua********@aon.at> wrote in message
news:opsmlarj06n2mgp5@innsbruck-neu...
supposing that you're using c++ (because of your posting in this ng), my
advice is: DO NOT USE MACROS. use the c++ feature "overloading of
operators" to achieve what you need.


Did you even read his question?


only until "macros" :)


At least your heart is in the right place! =D =D
Jul 23 '05 #8

P: n/a
Amy

Kurt Stutsman wrote:
Donovan Rebbechi wrote:

Why not define it inside the namespace that your library uses ? That's assuming that you're using a namespace and not polluting the global namespace. If the latter is the case, the real problem you have is a namespace pollution problem (both you and the vendor polluting the global namespace, and fighting over it via ugly hacks)


I'm pretty sure I read in the standard somewhere that overloading
new/delete in a different namespace does not work.


I have tried the namespace solution, which doesn't work. So my basic
question is how to overload/redefine operator delete --- it is not
possible in the scenario I mentioned above (they have been already
redefined/overloaded in a lib that our application has to link to)?

Thanks,

Thanks,

Jul 23 '05 #9

P: n/a
Amy
I have tried the namespace solution, which doesn't work. So my basic
question is how to overload/redefine operator delete --- it is not
possible in the scenario I mentioned above (they have been already
redefined/overloaded in a lib that our application has to link to)?

Thanks,

Jul 23 '05 #10

P: n/a
On 2005-02-22, Kurt Stutsman <ks*******@NOSPAM.sbcglobal.net> wrote:
Donovan Rebbechi wrote:

Why not define it inside the namespace that your library uses ? That's
assuming that you're using a namespace and not polluting the global namespace.
If the latter is the case, the real problem you have is a namespace pollution
problem (both you and the vendor polluting the global namespace, and fighting
over it via ugly hacks)


I'm pretty sure I read in the standard somewhere that overloading
new/delete in a different namespace does not work.


Doh! Don't have my copy here but it looks like this is the case from the
websites I just browsed (despite the fact that gcc accepts the code and
runs it "correctly" with all the standards-compliance flags turned on)

--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #11

P: n/a
Amy
Hi Donovan,

Is it possible for you to share the code? Since you mentioned "it looks
like this is the case from the websites I just browsed". We are
searching for solution desperately.

Thanks,

Jul 23 '05 #12

P: n/a
On 2005-02-22, Amy <am******@yahoo.com> wrote:
Hi Donovan,

Is it possible for you to share the code? Since you mentioned "it looks
like this is the case from the websites I just browsed". We are
searching for solution desperately.


I'm happy to share the code -- but this is not correct code as far as I can
tell (even though it compiles and runs on gcc). I checked my copy of the
standard tonight and it mentions class specific and global versions, but
nothing about namespace.

#include <iostream>

void * operator new (unsigned int x) throw () {
std::cout << "THERE" << std::endl;
return 0;
}
namespace foo{
void * operator new (unsigned int x) throw () {
std::cout << "HERE" << std::endl;
return 0;
}

void bar() {
int * x = new int;
}

};

int main()
{
foo::bar();
int * x = new int;
}

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #13

P: n/a
Donovan Rebbechi wrote:

Doh! Don't have my copy here but it looks like this is the case from the
websites I just browsed (despite the fact that gcc accepts the code and
runs it "correctly" with all the standards-compliance flags turned on)

Well, since it seems somewhat important, I looked through the standard.
Section 3.7.3.1 says:
An allocation function shall be a class member function or a
global function; a program is ill-formed if an allocation
function is declared in a namespace scope other than global
scope or declared static in global scope.

Really, the library you're using shouldn't have overloaded new/delete.
It's presumptious to think only they and no othre library or client code
would need to overload them. Well maybe there's a reason, but I don't
know of one off hand. Is there no way you can modify the source of the
library?
Jul 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.