473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

boost::checked delete

Hi NG,

I was looking through the code of boost::shared_ptr, and found the
following piece of code in some referenced header file. As far as I
understand it, it is used to delete the object the shared pointer object
points to.
I think the struct checked_deleter is used to store the deleter in the
shared_ptr class without having to store a function pointer.

But what I don't understand is what the purpose is of checked delete.
what additional safety does it bring?
Maybe I'm just missing the big picture, but the compiler knows or does
not know how to delete an object. So why would a typedef and a
completely useless (void)sizeof( that_typedef ) help?

Thanks in advance,
Mark

template<class T> inline void checked_delete(T * x)
{
// intentionally complex - simplification causes regressions
typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
(void) sizeof(type_must_be_complete);
delete x;
}

template<class T> struct checked_deleter
{
typedef void result_type;
typedef T * argument_type;

void operator()(T * x) const
{
// boost:: disables ADL
boost::checked_delete(x);
}
};

--
<<Remove the del for email>>
Jul 22 '05 #1
8 3400
On 01/10/2004, Capstar wrote:
I was looking through the code of boost::shared_ptr,
I've been looking through this too, with a view to porting it to BCB3.
and found the
following piece of code in some referenced header file. As far as I
understand it, it is used to delete the object the shared pointer
object points to. I think the struct checked_deleter is used to
store the deleter in the shared_ptr class without having to store a
function pointer.

But what I don't understand is what the purpose is of checked delete.
what additional safety does it bring?
Maybe I'm just missing the big picture, but the compiler knows or
does not know how to delete an object. So why would a typedef and a
completely useless (void)sizeof( that_typedef ) help?

As I understand it, boost::checked_delete is a mechanism to ensure that
objects which are only partially defined are not deleted. If this were
not prevented, boost::shared_ptr could take ownership of an incomplete
type and delete it. If the type were a class with a non trivial
destructor, the destrutor would not be called.

--
Simon Elliott http://www.ctsn.co.uk
Jul 22 '05 #2
Simon Elliott wrote:
On 01/10/2004, Capstar wrote:

I was looking through the code of boost::shared_ptr,

I've been looking through this too, with a view to porting it to BCB3.

and found the
following piece of code in some referenced header file. As far as I
understand it, it is used to delete the object the shared pointer
object points to. I think the struct checked_deleter is used to
store the deleter in the shared_ptr class without having to store a
function pointer.

But what I don't understand is what the purpose is of checked delete.
what additional safety does it bring?
Maybe I'm just missing the big picture, but the compiler knows or
does not know how to delete an object. So why would a typedef and a
completely useless (void)sizeof( that_typedef ) help?


As I understand it, boost::checked_delete is a mechanism to ensure that
objects which are only partially defined are not deleted. If this were
not prevented, boost::shared_ptr could take ownership of an incomplete
type and delete it. If the type were a class with a non trivial
destructor, the destrutor would not be called.


How would this work then? As I see it (mostly from a C point of view I
admin), sizeof(an_incomplete_type) would give a compiler error, but so
would delete an_incomplete_type_pointer.

Mark

--
<<Remove the del for email>>
Jul 22 '05 #3
>
How would this work then? As I see it (mostly from a C point of view I
admin), sizeof(an_incomplete_type) would give a compiler error, but so
would delete an_incomplete_type_pointer.


In C++ templates does not get fully compiled unless they are used. Therefore
checked_delete does not cause a compiler error unless it is actually called
(and the type is incomplete).

john
Jul 22 '05 #4
John Harrison wrote:
How would this work then? As I see it (mostly from a C point of view I
admin), sizeof(an_incomplete_type) would give a compiler error, but so
would delete an_incomplete_type_pointer.

In C++ templates does not get fully compiled unless they are used. Therefore
checked_delete does not cause a compiler error unless it is actually called
(and the type is incomplete).

john


Yes, I understand that, but if I have the next piece of code:

template<class T> struct checked_deleter
{
typedef void result_type;
typedef T * argument_type;

void operator()(T * x) const
{
delete x;
}
};

I expect this to compile ok for a complete type, and fail to compile for
an incomplete type. So I don't see any difference in functionality with
the code in my original post.

Mark

--
<<Remove the del for email>>
Jul 22 '05 #5

"Capstar" <ne**@deleg.homeip.net> wrote in message
news:cj*********@news.tudelft.nl...
John Harrison wrote:
How would this work then? As I see it (mostly from a C point of view I
admin), sizeof(an_incomplete_type) would give a compiler error, but so
would delete an_incomplete_type_pointer.

In C++ templates does not get fully compiled unless they are used. Therefore checked_delete does not cause a compiler error unless it is actually called (and the type is incomplete).

john


Yes, I understand that, but if I have the next piece of code:

template<class T> struct checked_deleter
{
typedef void result_type;
typedef T * argument_type;

void operator()(T * x) const
{
delete x;
}
};

I expect this to compile ok for a complete type, and fail to compile for
an incomplete type. So I don't see any difference in functionality with
the code in my original post.


Well one point is that 'type_must_be_complete' is likely to appear in the
compiler error message. Therefore giving the user some clue as to what is
wrong.

john
Jul 22 '05 #6
John Harrison wrote:
template<class T> struct checked_deleter
{
typedef void result_type;
typedef T * argument_type;

void operator()(T * x) const
{
delete x;
}
};

I expect this to compile ok for a complete type, and fail to compile for
an incomplete type. So I don't see any difference in functionality with
the code in my original post.

Well one point is that 'type_must_be_complete' is likely to appear in the
compiler error message. Therefore giving the user some clue as to what is
wrong.

john


Ok, that seems very likely now you mention it.

Thanks,
Mark

--
<<Remove the del for email>>
Jul 22 '05 #7

"Capstar" <ne**@deleg.homeip.net> wrote in message
news:cj*********@news.tudelft.nl...
John Harrison wrote:
How would this work then? As I see it (mostly from a C point of view I
admin), sizeof(an_incomplete_type) would give a compiler error, but so
would delete an_incomplete_type_pointer.

In C++ templates does not get fully compiled unless they are used. Therefore checked_delete does not cause a compiler error unless it is actually called (and the type is incomplete).

john


Yes, I understand that, but if I have the next piece of code:

template<class T> struct checked_deleter
{
typedef void result_type;
typedef T * argument_type;

void operator()(T * x) const
{
delete x;
}
};

I expect this to compile ok for a complete type, and fail to compile for
an incomplete type. So I don't see any difference in functionality with
the code in my original post.


According to boost's documentation on checked_delete
(http://www.boost.org/libs/utility/checked_delete.html), your expectations
will not be met:

" The C++ Standard allows, in 5.3.5/5, pointers to incomplete class types to
be deleted with a delete-expression. When the class has a non-trivial
destructor, or a class-specific operator delete, the behavior is undefined.
Some compilers issue a warning when an incomplete type is deleted, but
unfortunately, not all do, and programmers sometimes ignore or disable
warnings. "

--
David Hilsee
Jul 22 '05 #8
David Hilsee wrote:
"Capstar" <ne**@deleg.homeip.net> wrote in message
news:cj*********@news.tudelft.nl...
John Harrison wrote:
How would this work then? As I see it (mostly from a C point of view I
admin), sizeof(an_incomplete_type) would give a compiler error, but so
would delete an_incomplete_type_pointer.

In C++ templates does not get fully compiled unless they are used.
Therefore
checked_delete does not cause a compiler error unless it is actually
called
(and the type is incomplete).

john


Yes, I understand that, but if I have the next piece of code:

template<class T> struct checked_deleter
{
typedef void result_type;
typedef T * argument_type;

void operator()(T * x) const
{
delete x;
}
};

I expect this to compile ok for a complete type, and fail to compile for
an incomplete type. So I don't see any difference in functionality with
the code in my original post.

According to boost's documentation on checked_delete
(http://www.boost.org/libs/utility/checked_delete.html), your expectations
will not be met:

" The C++ Standard allows, in 5.3.5/5, pointers to incomplete class types to
be deleted with a delete-expression. When the class has a non-trivial
destructor, or a class-specific operator delete, the behavior is undefined.
Some compilers issue a warning when an incomplete type is deleted, but
unfortunately, not all do, and programmers sometimes ignore or disable
warnings. "


Ok, I wasn't aware of that. I have been looking for this particular
documentation, but I couldn't find it.

Thanks.

--
<<Remove the del for email>>
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Natsu Mizutani | last post by:
Hello, I'm trying to wrap a c++ library using MPI inside with boost.python (or SWIG). I managed to find that calling `MPI::Init()` embeded in a c++ funtion would not work. So, I decided to use...
2
by: Richard Latter | last post by:
Hello All, I am a newbie to the Boost library and I have a question about a simple function. All I would like to do is to create a simple function that can test strings using regular...
6
by: Matan Nassau | last post by:
Hello. i have a composite which i want to delete. this is a composite which represents a boolean expression (see a previous post of mine with more details at...
2
by: madmanahong | last post by:
I Found new & delete of the CRT was very slow. so I overloaded the global new & delete operator. but if the CRT & STL also use my new & delete operator. It's bad because then my overloaded...
3
by: kk_oop | last post by:
Hi. I recently wrote a simple little template that defines an array that checks attempts to use out of bounds indexes. The only problem is that it does provide the use array style value...
15
by: Dilip | last post by:
I am aware that the C++ standard in its present form does not say anything about threads, however I do have a relevant question. I am working on Windows XP/VC++ 8.0. Is there a problem new'ing...
2
by: Chameleon | last post by:
Why this strange output? Why so many d'tor calls? The code: ---------------------------------------------------------- #include <cstdio> #include <boost/thread/thread.hpp> class A {
13
by: brad | last post by:
Still learning C++. I'm writing some regex using boost. It works great. Only thing is... this code seems slow to me compared to equivelent Perl and Python. I'm sure I'm doing something incorrect....
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.