Big Problem! How to overload operator delete?

Discussion in 'C++' started by Lighter, Aug 10, 2006.

  1. Lighter

    Lighter Guest

    Big Problem! How to overload operator delete?

    According to C++ standard, "A deallocation function can have more than
    one parameter."(see 3.7.3.2); however, I don't know how to use an
    overloaded delete operator. Let me use an example to illustrate this:

    /********************************************************/
    #include <new>
    #include <iostream>

    using namespace std;

    void operator delete(void* p, const nothrow_t&)
    {
    cout << "Hello" << endl;
    } // (1)

    void operator delete(void* p, int a, int b)
    {
    cout << "World" << endl;
    } // (2)

    int main()
    {
    int* p = new(nothrow) int;

    delete p; // This cannot render to show 'Hello' or 'World'
    }
    /********************************************************/

    Even if I use 'delete(nothrow, p);', it cannot render to show 'Hello'
    or 'World' either. My problem just lies here: Although I can write my
    own operator delete, I cannot use it. As far as I know, the C++
    standard doesn't give an example to illustrate the usage of delete (The
    usage of new is given.).

    An ugly way to do this is to use function call:

    operator delete(nothrow, p); // This can render to show 'Hello'

    However, I don't think this is the answer to my question. Who know the
    correct one?

    Any help will be appreciatied. Thanks in advance.
     
    Lighter, Aug 10, 2006
    #1
    1. Advertising

  2. Lighter

    Butterfly Guest

    Lighter wrote:
    > Big Problem! How to overload operator delete?
    >
    > According to C++ standard, "A deallocation function can have more than
    > one parameter."(see 3.7.3.2); however, I don't know how to use an
    > overloaded delete operator. Let me use an example to illustrate this:
    >
    > /********************************************************/
    > #include <new>
    > #include <iostream>
    >
    > using namespace std;
    >
    > void operator delete(void* p, const nothrow_t&)
    > {
    > cout << "Hello" << endl;
    > } // (1)
    >
    > void operator delete(void* p, int a, int b)
    > {
    > cout << "World" << endl;
    > } // (2)
    >
    > int main()
    > {
    > int* p = new(nothrow) int;
    >
    > delete p; // This cannot render to show 'Hello' or 'World'
    > }
    > /********************************************************/
    >
    > Even if I use 'delete(nothrow, p);', it cannot render to show 'Hello'
    > or 'World' either. My problem just lies here: Although I can write my
    > own operator delete, I cannot use it. As far as I know, the C++
    > standard doesn't give an example to illustrate the usage of delete (The
    > usage of new is given.).
    >
    > An ugly way to do this is to use function call:
    >
    > operator delete(nothrow, p); // This can render to show 'Hello'
    >
    > However, I don't think this is the answer to my question. Who know the
    > correct one?
    >
    > Any help will be appreciatied. Thanks in advance.


    Try this

    void operator delete(void* ptr)
    {
    }
     
    Butterfly, Aug 10, 2006
    #2
    1. Advertising

  3. Lighter

    Lighter Guest

    Butterfly wrote:
    > Try this
    >
    > void operator delete(void* ptr)
    > {
    > }


    This is not what I want.

    My question is how to overload the operator delete with multiple
    parameters.
     
    Lighter, Aug 10, 2006
    #3
  4. Lighter

    Alan Johnson Guest

    Lighter wrote:
    > Butterfly wrote:
    >> Try this
    >>
    >> void operator delete(void* ptr)
    >> {
    >> }

    >
    > This is not what I want.
    >
    > My question is how to overload the operator delete with multiple
    > parameters.
    >


    You can't, really. The only time an overloaded version of operator
    delete is called is when an exception is thrown in the constructor of
    the object being constructed. The following example shows the
    relationship between them:
    #include <cstddef>
    #include <new>
    #include <iostream>

    void * operator new(std::size_t sz)
    throw(std::bad_alloc)
    {
    std::cout << "Normal operator new called." << std::endl ;

    void * p = std::malloc(sz) ;
    if (!p)
    throw std::bad_alloc() ;
    return p ;
    }

    void operator delete(void * p) throw()
    {
    std::cout << "Normal operator delete called." << std::endl ;
    if (p)
    std::free(p) ;
    }

    void * operator new(std::size_t sz, std::eek:stream & out)
    throw(std::bad_alloc)
    {
    out << "Custom operator new called." << std::endl ;
    return ::eek:perator new(sz) ;
    }

    void operator delete(void * p, std::eek:stream & out) throw()
    {
    out << "Custom operator delete called." << std::endl ;
    ::eek:perator delete(p) ;
    }

    class T
    {
    public:
    T(bool should_throw) { if (should_throw) throw 1 ; }
    } ;

    int main()
    {
    // Calls normal new, normal delete.
    T * p = new T(false) ;
    delete p ;
    std::cout << std::endl ;

    // Calls custom new, normal delete.
    p = new(std::cout) T(false) ;
    delete p ;
    std::cout << std::endl ;

    // Calls normal new, normal delete.
    try
    {
    T * p = new T(true) ;
    delete p ;
    }
    catch (...)
    {}
    std::cout << std::endl ;

    // Calls custom new, custom delete.
    try
    {
    T * p = new(std::cout) T(true) ;
    delete p ;
    }
    catch (...)
    {}
    std::cout << std::endl ;
    }

    --
    Alan Johnson
     
    Alan Johnson, Aug 10, 2006
    #4
  5. Lighter

    Lighter Guest

    To Alan Johnson:

    Thank you very very much! Your answer is concise and instructive. You
    enlightened me.
     
    Lighter, Aug 10, 2006
    #5
  6. Lighter

    sarathy Guest

    Hi,
    I just have a small doubt. Can new/delete be overloaded with
    any number of parameters (of any types) or is it just "ostream" type
    must be used.

    Regards,
    Sarathy
     
    sarathy, Aug 10, 2006
    #6
  7. Lighter

    Alan Johnson Guest

    sarathy wrote:
    > Hi,
    > I just have a small doubt. Can new/delete be overloaded with
    > any number of parameters (of any types) or is it just "ostream" type
    > must be used.
    >
    > Regards,
    > Sarathy


    I think that the first parameter to new must always be std::size_t, and
    the first parameter to delete a void *. Other than that you can do
    whatever you'd like with the rest of the parameters. There are a few
    overloads that people will expect to behave in certan ways.

    void * operator new(std::size_t sz, const std::nothrow_t &) throw() ;

    People expect that to allocate memory without throwing exceptions, and
    return NULL if it can't.

    void * operator new(std::size_t sz, void * p) throw()
    {
    return p ;
    }

    People expect "placement new" to act as above. That is, it just
    returns the pointer provided without actually allocating any memory.
    Likewise they'll expect the corresponding operator delete to not free
    any memory.

    --
    Alan Johnson
     
    Alan Johnson, Aug 10, 2006
    #7
    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. Shaguf
    Replies:
    0
    Views:
    534
    Shaguf
    Dec 24, 2008
  2. Shaguf
    Replies:
    0
    Views:
    492
    Shaguf
    Dec 26, 2008
  3. Shaguf
    Replies:
    0
    Views:
    268
    Shaguf
    Dec 26, 2008
  4. Shaguf
    Replies:
    0
    Views:
    242
    Shaguf
    Dec 24, 2008
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    273
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page