C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

placement operator delete

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Andrei Alexandrescu (See
Guest





PostPosted: Wed Mar 22, 2006 4:19 am    Post subject: placement operator delete Reply with quote



Consider the code:

struct T {
void * operator new(size_t, void * p) {
return p;
}
void operator delete(void *, size_t, void *) {
...
}
};

So T's author implements the placement new as a member. Fine. Then, it
also implements sort of a delete counterpart, but one with three
parameters, not two. This is because T's implementer looked at 3.7.3.2
para 2 in the 1998 standard. That paragraph says that you can add size_t
as an optional second argument to the default deallocation function.

That suggests that void T::operator delete(void*, size_t, void*) is an
acceptable form of the placement delete operator. Unfortunately, I
couldn't find anything in the standard to clarify that, and 18.4.1.3
only mentions the global void ::operator delete(void*, void*).

Any clarification would be welcome. Thanks!


Andrei

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Anders Dalvander
Guest





PostPosted: Thu Mar 23, 2006 3:15 am    Post subject: Re: placement operator delete Reply with quote



Andrei Alexandrescu (See Website For Email) wrote:
Quote:
That suggests that void T::operator delete(void*, size_t, void*) is an
acceptable form of the placement delete operator.

From: Bjarne Stroustrup's C++ Style and Technique FAQ
http://public.research.att.com/~bs/bs_faq2.html#placement-delete

Q: Is there a "placement delete"?
A: No but if you need one you can write your own. <snip>

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Krzysztof Zelechowski
Guest





PostPosted: Thu Mar 23, 2006 4:06 am    Post subject: Re: placement operator delete Reply with quote



Uzytkownik "Andrei Alexandrescu (See Website For Email)"
<SeeWebsiteForEmail (AT) erdani (DOT) org> napisal w wiadomosci
news:4420CA07.9030804 (AT) erdani (DOT) org...
Quote:
Consider the code:

struct T {
void * operator new(size_t, void * p) {
return p;
}
void operator delete(void *, size_t, void *) {
...
}
};

So T's author implements the placement new as a member. Fine. Then, it
also implements sort of a delete counterpart, but one with three
parameters, not two. This is because T's implementer looked at 3.7.3.2
para 2 in the 1998 standard. That paragraph says that you can add size_t
as an optional second argument to the default deallocation function.

That suggests that void T::operator delete(void*, size_t, void*) is an
acceptable form of the placement delete operator. Unfortunately, I
couldn't find anything in the standard to clarify that, and 18.4.1.3 only
mentions the global void ::operator delete(void*, void*).

Any clarification would be welcome. Thanks!


It may actually be much worse IIRC. Consider a class with both placement
new and ordinary new declared:

class T { public: static void *operator new(size_t), operator new(size_t,
size_t); static void *operator delete(void *, size_t); };

Can T::operator delete(void *, size_t) serve both as ordinary delete and
unwinding delete?

Chris


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Andrei Alexandrescu (See
Guest





PostPosted: Thu Mar 23, 2006 7:06 am    Post subject: Re: placement operator delete Reply with quote

Anders Dalvander wrote:
Quote:
Andrei Alexandrescu (See Website For Email) wrote:

That suggests that void T::operator delete(void*, size_t, void*) is an
acceptable form of the placement delete operator.


From: Bjarne Stroustrup's C++ Style and Technique FAQ
http://public.research.att.com/~bs/bs_faq2.html#placement-delete

Q: Is there a "placement delete"?
A: No but if you need one you can write your own. <snip

Thanks for the link. Unfortunately, it doesn't address my question,
which refers to the ambiguity of the second size_t parameter that
T::operator delete might define.


Andrei

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Guest






PostPosted: Thu Mar 23, 2006 4:32 pm    Post subject: Re: placement operator delete Reply with quote

Andrei Alexandrescu (See Website For Email) wrote:
Quote:
Consider the code:

struct T {
void * operator new(size_t, void * p) {
return p;
}
void operator delete(void *, size_t, void *) {
...
}
};

So T's author implements the placement new as a member. Fine. Then, it
also implements sort of a delete counterpart, but one with three
parameters, not two. This is because T's implementer looked at 3.7.3.2
para 2 in the 1998 standard. That paragraph says that you can add size_t
as an optional second argument to the default deallocation function.

That suggests that void T::operator delete(void*, size_t, void*) is an
acceptable form of the placement delete operator.

I don't see how you reached that conclusion. Section 3.7.3.2 says that
operator delete() must have a first parameter of type void*, and that
it can be declared with more than one parameter. It says several
important things about operator delete() when it is declared with
exactly one parameter, and when it is declared with exactly two
parameters if the second one is size_t. However, it says absolutely
nothing about operator delete() when declared with any other number of
parameters.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Andrei Polushin
Guest





PostPosted: Fri Mar 24, 2006 12:46 am    Post subject: Re: placement operator delete Reply with quote

There is nothing in [3.7.3.2/2], that describes placement forms of
(de)allocation function. Instead, it describes two additional forms of
non-placement deallocation functions. For placement forms, [5.3.4/19]
should be used.

Quote:
From the beginning:

1. Deallocation function with three or more parameters is "unusual" or
"placement form" (3.7.3.2/2), and there is no syntax to call such
function explicitly (5.3.5/1).

2. It can be called, however, as a result of new-expression (5.3.4/1),
which throws an exception (5.3.4/Cool from the object initialization
(5.3.4/17).

3. A matching deallocation function should have the same number of
parameters and all parameter types except the first are identical
(5.3.4/19).

4. If there is no matching deallocation function defined, this might
result in memory leak (see note to 5.3.4/17 and 5.3.4/19 again).

5. If there is no matching allocation function defined, then the
deallocation function cannot be called at all, such deallocation
function is unused. It can be made useful if we will define its
matching allocation function (probably in derived class).

To correct your example, there should be two matching pairs:

struct T {
// new(p) T();
void * operator new(size_t, void * p);
void operator delete(void *, void * p);

// new(sz, p) T();
void * operator new(size_t, size_t sz, void * p);
void operator delete(void *, size_t sz, void * p);
};

If you'll omit the first operator delete, this might lead to memory
leak. If you'll omit the second operator new, that will make the
second operator delete useless.

Hope this helps.

--
Andrei Polushin

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Andrei Alexandrescu (See
Guest





PostPosted: Fri Mar 24, 2006 7:06 am    Post subject: Re: placement operator delete Reply with quote

Andrei Polushin wrote:
[snip]
Quote:
To correct your example, there should be two matching pairs:

struct T {
// new(p) T();
void * operator new(size_t, void * p);
void operator delete(void *, void * p);

// new(sz, p) T();
void * operator new(size_t, size_t sz, void * p);
void operator delete(void *, size_t sz, void * p);
};

If you'll omit the first operator delete, this might lead to memory
leak. If you'll omit the second operator new, that will make the
second operator delete useless.

Hope this helps.

Thanks. So at the end of the day, I can't have access to the size of the
object being deleted in any placement operator delete! Quite bizarre.

Andrei

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Sergey P. Derevyago
Guest





PostPosted: Fri Mar 24, 2006 5:06 pm    Post subject: Re: placement operator delete Reply with quote

"Andrei Alexandrescu (See Website For Email)" wrote:
Quote:
Thanks. So at the end of the day, I can't have access to the size of the
object being deleted in any placement operator delete! Quite bizarre.

Yes, you can.

The trick works as expected but can be considered to be non-conforming (BTW
can't it?).

template <class T>
struct sh_ptr_core {
mem_pool& pool;
T* ptr;
int refs;

sh_ptr_core(mem_pool& mp, T* p) : pool(mp), ptr(p), refs(1) {}
~sh_ptr_core() { delete ptr; }

void* operator new(size_t size, mem_pool& mp)
{
return mp.allocate(size);
}

#ifndef DERS_NO__PLACEMENT_DELETE
void operator delete(void* ptr, size_t size, mem_pool& mp)
{
mp.deallocate(ptr, size);
}
#endif

void operator delete(void* ptr, size_t size)
{
sh_ptr_core* this_=static_cast<sh_ptr_core*>(ptr); // <===== !
this_->pool.deallocate(ptr, size);
}
};

The trick is
sh_ptr_core* this_=static_cast<sh_ptr_core*>(ptr);
conversion. Here I suppose that ~sh_ptr_core() dtor doesn't invalidate its
pool reference.
Really, an implementation that corrupts the object memory right after the
dtor call has finished but just before the operator delete() gets called can
be considered to impose additional run-time overhead. So it's not likely to be
met in real life.
--
With all respect, Sergey. http://ders.angen.net/
mailto : ders at skeptik.net

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.