473,404 Members | 2,179 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,404 software developers and data experts.

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

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
13 5856
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
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
"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
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
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
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
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Glen Able | last post by:
I'd like to use a few different memory arenas in my current application and to be able to specify for each allocation where it comes from. Is there any way I can make operator new take another...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
5
by: kUfa.scoopex | last post by:
Hi there! I have a small problem, and i really dont see any convenient way to fix it. Basically, i'd like to overload global new operators, into something like this: void *operator new(...
3
by: ganesh.tambat | last post by:
Hi, Please see below a piece of code. Here I am trying to create a linked list by attaching two linked list together. I have overloaded operator + for this. Now the output always says that the...
16
by: Prayag Narula | last post by:
Hi, I want to redefine fprintf for debugging purposes. That is I want that all the output that is going to the stdout should be logged in a file. I tried something like #define fprintf...
4
by: Chris | last post by:
Hello all, Using g++ 3.3.1 on Linux ("Linux From Scratch") I have a data structure SCSIParams_t that I wish to print out, field-by-field. Rather than code a long line of the form std::cout <<...
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
3
by: Bob Alston | last post by:
Anyone know how to disable or redefine the Ctrl - hotkey which deletes a record? I have a multi page form that uses 14 records, each record handles 1-2 pages of the 18 page form. Occasionally ,...
3
by: Ernie.Pasveer | last post by:
Hi All, Is there a way to create a macro called #redefine that will allow redefinition without having to use #ifdef/#undef For example, I'd like to do something like this : #define THING ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
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,...
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...
0
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...
0
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...

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.