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