Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to use parameter in operator delete?

Reply
Thread Tools

How to use parameter in operator delete?

 
 
Nephi Immortal
Guest
Posts: n/a
 
      07-06-2012
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;
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-06-2012
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
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      07-08-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Equivalence in use of bitwise | operator and + operator Ioannis Vranos C++ 8 11-14-2008 11:03 PM
without declare parameter [double square(parameter)] return 0 in main WanHongbin@gmail.com C Programming 5 10-01-2008 03:31 AM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
Parameter List / Parameter Block / Anything patterns... mast2as@yahoo.com C++ 4 03-29-2007 09:37 PM



Advertisments