How to use parameter in operator delete?

Discussion in 'C++' started by Nephi Immortal, Jul 6, 2012.

  1. I implement operator new and operator delete by adding an extra parameter. If I want to implement one heap class and create more than one heap object, then both operator new and operator delete are able to select correctheap object. How can I do that?
    I do not want to create operator new and operator delete inside class body. It is independent to work with all types.

    class Heap
    {
    public:
    Heap() {}
    void run() {}
    };

    void* operator new(
    size_t size,
    Heap& heap
    )
    {
    heap.run();
    return 0;
    }

    void operator delete(
    void* p,
    Heap& heap
    )
    {
    heap.run();
    }

    int main()
    {
    Heap heap;
    char* byte = new( heap ) char;
    delete( heap ) byte; // ? does not compile -- how?

    return 0;
    }
    Nephi Immortal, Jul 6, 2012
    #1
    1. Advertising

  2. On 7/5/2012 9:42 PM, Nephi Immortal wrote:
    > class Heap
    > {
    > public:
    > Heap() {}
    > void run() {}
    > };
    >
    > void* operator new(
    > size_t size,
    > Heap& heap
    > )
    > {
    > heap.run();
    > return 0;
    > }
    >
    > void operator delete(
    > void* p,
    > Heap& heap
    > )


    While your compiler allows you to define this form, you can't call it
    directly. There is no syntax for that.

    > {
    > heap.run();
    > }
    >
    > int main()
    > {
    > Heap heap;
    > char* byte = new( heap ) char;
    > delete( heap ) byte; // ? does not compile -- how?


    There is no "placement delete". The special operator delete can only be
    defined to be called by the compiler in case the constructor that was
    called in the placement new form throws an exception.

    >
    > return 0;
    > }


    You can't pass an unrelated object to the operator delete function. You
    will only pass your object to the operator delete function that has been
    defined in the class of that object, not the global one. If you can
    live with your own global heap, though, this works:

    #include <iostream>
    #include <cstdlib>

    class Heap
    {
    public:
    Heap() {}
    void run(void* p = 0) {
    std::cout << "Heap::run was called with p=" << p << "\n";
    }
    };

    Heap heap;

    void* operator new(
    size_t size,
    Heap& heap
    )
    {
    heap.run();
    return std::malloc(size);
    }

    void operator delete(
    void* p)
    {
    heap.run(p);
    std::free(p);
    }

    int main()
    {
    char* byte = new( heap ) char;
    delete byte;

    return 0;
    }

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 6, 2012
    #2
    1. Advertising

  3. On 06.07.12 03.42, Nephi Immortal wrote:
    > I implement operator new and operator delete by adding an extra parameter. If I want to implement one heap class and create more than one heap object, then both operator new and operator delete are able to select correct heap object. How can I do that?


    For operator new this is straight forward, as you have seen.
    For operator delete the important question is in which heap is the
    object to be deleted and where do you get this information from. You do
    not want to do this manually, i.e. by passing the (hopefully) correct
    heap to operator delete. This would be very error prone.

    The heap needs to be tied to the object at the invocation of operator
    new. You could do this intrusive or non-intrusive.
    You eliminated the intrusive Version with class individual new/delete
    operators already.
    So the non-intrusive version is left:

    class Heap
    {public:
    void* allocate(size_t size);
    void free(void*);
    };

    void* operator new(size_t size, Heap& heap)
    { Heap** ptr = heap.allocate(size + sizeof(Heap*));
    *ptr = &heap;
    return ptr + 1;
    }

    void* operator new(size_t size)
    { Heap** ptr = (Heap**)std::malloc(size + sizeof(Heap*));
    *ptr = NULL;
    return ptr + 1;
    }

    void operator delete(void* p)
    { Heap** hp = (Heap**)p;
    if (*hp)
    (*hp)->free(hp);
    else
    std::free(hp);
    }

    Optionally the same for new[]/delete[].

    The price of the non-intrusive solution is that /every/ allocation takes
    space for an additional pointer. Unless you have an embedded environment
    with low memory this should not cause much concern.


    > I do not want to create operator new and operator delete inside class body. It is independent to work with all types.


    A common base class could do the job.


    Marcel
    Marcel Müller, Jul 8, 2012
    #3
    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. Sandeep Grover

    delete on delete !

    Sandeep Grover, Jul 12, 2003, in forum: C++
    Replies:
    19
    Views:
    609
    Chris \( Val \)
    Jul 22, 2003
  2. Jef Driesen
    Replies:
    1
    Views:
    492
    Gernot Frisch
    Jan 19, 2005
  3. Amy
    Replies:
    13
    Views:
    5,994
    Kurt Stutsman
    Feb 23, 2005
  4. tom
    Replies:
    5
    Views:
    398
  5. Ioannis Vranos
    Replies:
    8
    Views:
    327
    James Kanze
    Nov 14, 2008
Loading...

Share This Page