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

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!
 
U

ulrich

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.

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!
 
D

Dietmar Kuehl

Amy said:
#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.
 
J

James Aguilar

ulrich said:
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?
 
D

Donovan Rebbechi

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,
 
K

Kurt Stutsman

Donovan said:
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.
 
A

Amy

Kurt said:
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,
 
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,
 
D

Donovan Rebbechi

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)
 
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,
 
D

Donovan Rebbechi

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,
 
K

Kurt Stutsman

Donovan said:
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?
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top