Different behavior overloaded global and local operator delete

Discussion in 'C++' started by Alex Vinokur, Jun 14, 2012.

  1. Alex Vinokur

    Alex Vinokur Guest

    Hi,

    Different behavior overloaded global and local operator delete.

    We can see see that overloaded global operator delete(void *i_p,
    std::size_t i_size) is not recognized.

    Any suggestions.

    Thanks


    // ===========================================
    // File foo.cpp
    #include <memory>
    #include <iostream>

    #define SHOW_FUNC std::cout << "Line-" << __LINE__ << ": " <<
    __PRETTY_FUNCTION__ << std::endl

    // ---------------------------------
    // Global new & delete
    // ---------------------------------
    void* operator new(std::size_t i_size)
    {
    SHOW_FUNC;
    void* p = malloc(i_size);
    // Do something
    return p;

    }
    void* operator new[] (std::size_t i_size)
    {
    SHOW_FUNC;
    void* p = malloc(i_size);
    // Do something
    return p;

    }


    void operator delete(void *i_p, std::size_t i_size)
    {
    SHOW_FUNC;
    // Do something
    free(i_p);
    }



    void operator delete[](void *i_p, std::size_t i_size)
    {
    SHOW_FUNC;
    // Do something
    free(i_p);
    }


    // ---------------------------------
    struct Foo
    {
    void* operator new(std::size_t i_size)
    {
    SHOW_FUNC;
    void* p = malloc(i_size);
    // Do something
    return p;

    }
    void* operator new[] (std::size_t i_size)
    {
    SHOW_FUNC;
    void* p = malloc(i_size);
    // Do something
    return p;

    }


    void operator delete(void *i_p, std::size_t i_size)
    {
    SHOW_FUNC;
    // Do something
    free(i_p);
    }



    void operator delete[](void *i_p, std::size_t i_size)
    {
    SHOW_FUNC;
    // Do something
    free(i_p);
    }

    };



    // ====================
    int main()
    {

    Foo* p1 = new Foo;
    delete p1;

    Foo* p2 = new Foo[2];
    delete [] p2;

    Foo* p3 = ::new Foo;
    ::delete p3;

    Foo* p4 = ::new Foo[2];
    ::delete [] p4;

    return 0;
    }
    // ===========================================


    Linux
    gcc version 4.1.2 20080704 (Red Hat 4.1.2-52)

    > g++ foo.cpp

    // No errors


    Output
    Line-49: static void* Foo::eek:perator new(size_t)
    Line-67: static void Foo::eek:perator delete(void*, size_t)
    Line-57: static void* Foo::eek:perator new [](size_t)
    Line-76: static void Foo::eek:perator delete [](void*, size_t)
    Line-11: void* operator new(size_t)
    // Not printed -> Line-29: void operator delete(void*, size_t)
    Line-19: void* operator new [](size_t)
    // Not printed -> Line-38: void operator delete [](void*, size_t)
     
    Alex Vinokur, Jun 14, 2012
    #1
    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. Dodo
    Replies:
    1
    Views:
    371
    Ron Natalie
    Aug 26, 2003
  2. tban
    Replies:
    1
    Views:
    274
    Ian Collins
    Jul 18, 2006
  3. John Goche
    Replies:
    2
    Views:
    363
    Frederick Gotham
    Sep 4, 2006
  4. Tristan Wibberley
    Replies:
    18
    Views:
    767
    James Kanze
    Sep 25, 2007
  5. Alex Vinokur
    Replies:
    3
    Views:
    508
    Zoltan Juhasz
    Jun 15, 2012
Loading...

Share This Page