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

Discussion in 'C++' started by Amy, Feb 22, 2005.

  1. Amy

    Amy Guest

    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!
    Amy, Feb 22, 2005
    #1
    1. Advertising

  2. Amy

    ulrich Guest

    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 <> 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/
    ulrich, Feb 22, 2005
    #2
    1. Advertising

  3. 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:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
    Dietmar Kuehl, Feb 22, 2005
    #3
  4. "ulrich" <> wrote in message
    news:eek:psmlarj06n2mgp5@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?
    James Aguilar, Feb 22, 2005
    #4
  5. Amy

    ulrich Guest

    On Tue, 22 Feb 2005 02:45:15 -0600, James Aguilar <>
    wrote:

    > "ulrich" <> wrote in message
    > news:eek:psmlarj06n2mgp5@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" :)
    ulrich, Feb 22, 2005
    #5
  6. On 2005-02-22, Amy <> 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/
    Donovan Rebbechi, Feb 22, 2005
    #6
  7. 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.
    Kurt Stutsman, Feb 22, 2005
    #7
  8. "ulrich" <> wrote in message
    news:eek:psmlcjynan2mgp5@innsbruck-neu...
    > On Tue, 22 Feb 2005 02:45:15 -0600, James Aguilar <>
    > wrote:
    >
    >> "ulrich" <> wrote in message
    >> news:eek:psmlarj06n2mgp5@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
    James Aguilar, Feb 22, 2005
    #8
  9. Amy

    Amy Guest

    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,
    Amy, Feb 22, 2005
    #9
  10. Amy

    Amy Guest

    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,
    Amy, Feb 22, 2005
    #10
  11. On 2005-02-22, 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.


    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/
    Donovan Rebbechi, Feb 22, 2005
    #11
  12. Amy

    Amy Guest

    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,
    Amy, Feb 22, 2005
    #12
  13. On 2005-02-22, Amy <> 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/
    Donovan Rebbechi, Feb 23, 2005
    #13
  14. 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?
    Kurt Stutsman, Feb 23, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,111
    John Harrison
    Jul 14, 2004
  2. Replies:
    3
    Views:
    359
    Keith Thompson
    Apr 14, 2008
  3. Replies:
    3
    Views:
    417
    Keith Thompson
    Apr 14, 2008
  4. PerlFAQ Server
    Replies:
    0
    Views:
    100
    PerlFAQ Server
    Feb 9, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    155
    PerlFAQ Server
    Apr 28, 2011
Loading...

Share This Page