Re: Corresponding forms of new and delete
From: Dhruv (dhruvbird_at_gmx.net)
Date: 10/22/03
- Previous message: Oplec: "Re: How would Mr. Stroustrup implement his solution to 12.7[2]"
- In reply to: Dave: "Corresponding forms of new and delete"
- Next in thread: Roger Orr: "Re: Corresponding forms of new and delete"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 21 Oct 2003 21:29:08 -0400
On Mon, 20 Oct 2003 17:11:51 -0400, Dave wrote:
>
> Hello all,
>
> In the code below, I see the following output:
>
> base::operator new(size_t, int)
> base::base()
> base::~base()
> base::operator delete(void *)
>
> In the case of an exception being thrown during construction, a form
of
> delete with a signature that corresponds to that of the "new" being
used
> will be called. However, during a "normal" deallocation, there is no
way to
> know which version of "new" was previously used, so the usual
deallocation
> routine is used (I hope I'm using the term "usual" properly here).
This is
> demonstrated in the output shown above.
>
> When the statement "delete ptr;" is reached, is there a way to cause
it to
> use "operator delete(void*, int)" as the underlying deallocation
routine
> rather than "operator delete(void *)"? After all, the usual
dellaocation
> routine may not do what's right given that the memory was not
allocated with
> the usual allocation routine! (Again, I hope I'm using the term
"usual"
> properly.)
>
No, there is no way that I am aware of that can make the operator delete
that you want to be called to get called. This operator delete exists
primarily because of potential failure of the object construction while
calling operator new, as you have already mentioned, but if you want to
call the operator new that takes extra parameters, then while deleting
that object you have to manually do the destroy-deallocate dance. The
reason for this is that you may pass pointers to functions that might
delete this passed pointer, so how can the functions know which operator
delete to call. So, you have to manually deallocate and delete the
memory.
> I am aware that I could do the following in place of "delete ptr;":
>
> ptr->~base();
> base::operator delete(ptr, 243);
Yes.
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
- Previous message: Oplec: "Re: How would Mr. Stroustrup implement his solution to 12.7[2]"
- In reply to: Dave: "Corresponding forms of new and delete"
- Next in thread: Roger Orr: "Re: Corresponding forms of new and delete"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|