473,413 Members | 1,856 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,413 software developers and data experts.

"Call Destructor" Vs. "Destruct Object"


If we have a simple class such as follows:

#include <string>

struct MyStruct {

std::string member;

MyStruct(unsigned const i)
{
member += '0'+i;
}
};

, then using "placement new" to construct an object of the class does the
following:

(1) Calls the constructor of the member object, "member".
(2) Calls the MyStruct constructor for the object itself.

This is conceivibly quite realistically like:

mystruct_obj.member.string();
mystruct_obj.MyStruct();

(i.e. we're invoking the code of two constructors.)

However, take a look at the following life-cycle of a DIY object, paying
particular attention to the way in which we destruct the object:

char *const mem = new char[sizeof(MyStruct)]; /* Allocate the memory */

MyStruct *const p = ::new(mem) MyStruct(5); /* Construct the object */

p->~MyStruct(); /* Destruct the object */

delete [] mem; /* Deallocate the memory */

Notice that when we are destructing the object, it looks as though we're
simply invoking the MyStruct destructor -- but in actuality, we're really
doing:

p->member.~string();
p->~MyStruct();

This is inconsistent with the way in which we construct the object! But not
only that, it's misleading because we are _not_ simply calling the
destructor -- we're doing more than that, we're invoking the constructors
of all base classes and subobjects. For consistency, we should have either
had:

Solution (1) -- Not favourable

char *const mem = new char[sizeof(MyStruct)];

MyStruct &obj = reinterpret_cast<MyStruct&>(*mem);

obj.MyClass(5); /* Construct! */

obj.~MyClass(); /* Destruct! */

delete [] mem;
Solution (2) -- Quite favourable

char *const mem = new char[sizeof(MyStruct)];

MyStruct *const p = ::new(mem) MyClass(5);

::delete(p) MyClass; /* Placement delete! */

The second solution is favourable, as it makes no mention of calling
constructors or destructors, but rather introduces the idea of
"constructing" and "destructing" an object (which may involve the
invocation of one or more constructors/destructors for base classes or
subobjects, etc.). The first solution is downright misleading, because it
resembles exactly the way in which we normally invoke a member function,
and gives no indication that we're also invoking the
constructors/destructors of base classes and subobjects.

My own opinion is that there should be a "placement delete", whose purpose
it is to destruct an object.

The DIY invocation of a destructor via "obj.~Class();" should either:

(1) Be forbidden, as it is for constructors.
(2) Just call _that_ destructor, exactly as if it were a member
function, without invoking any other destructors (such as those of base
objects or subobjects.

Maybe the die is cast... but it's worth a think.

--

Frederick Gotham
Sep 26 '06 #1
5 3312
Frederick Gotham posted:
(1) Calls the constructor of the member object, "member".
(2) Calls the MyStruct constructor for the object itself.

And also the constructors of any of the base objects or member objects of
an std::string.

This is conceivibly quite realistically like:

mystruct_obj.member.string();
mystruct_obj.MyStruct();
This is inconsistent with the way in which we construct the object! But
not only that, it's misleading because we are _not_ simply calling the
destructor -- we're doing more than that, we're invoking the
constructors of all base classes and subobjects.

That should be "destructors" rather than "constructors".

Solution (2) -- Quite favourable

char *const mem = new char[sizeof(MyStruct)];

MyStruct *const p = ::new(mem) MyClass(5);

::delete(p) MyClass; /* Placement delete! */

There should be another statement at the end of that:

delete [] mem;

--

Frederick Gotham
Sep 26 '06 #2
Frederick Gotham wrote:
>
This is conceivibly quite realistically like:

mystruct_obj.member.string();
mystruct_obj.MyStruct();
>
Notice that when we are destructing the object, it looks as though we're
simply invoking the MyStruct destructor -- but in actuality, we're really
doing:

p->member.~string();
p->~MyStruct();
Nope, other way around. The destructors are called in the inverse order
that the constructors were called.
>
This is inconsistent with the way in which we construct the object! But not
only that, it's misleading because we are _not_ simply calling the
destructor -- we're doing more than that, we're invoking the constructors
of all base classes and subobjects. For consistency, we should have either
had:
It's NOT really that inconsistant. The language ALWAYS runs the
constructors for an object in the specified order and ALWAYS runs the
destructors in the inverse of that order. You can NOT pick and chose
which constructors or destructors run or change their ordering.
My own opinion is that there should be a "placement delete", whose purpose
it is to destruct an object.
Perhaps, but I'm not sure what you are trying to accomplish.
>
The DIY invocation of a destructor via "obj.~Class();" should either:

(1) Be forbidden, as it is for constructors.
Yes, and further, the above syntax is almost certainly indicative of a
problem unless obj is a reference.
(2) Just call _that_ destructor, exactly as if it were a member
function, without invoking any other destructors (such as those of base
objects or subobjects.
Ick, that would be a disaster.
Sep 26 '06 #3
Frederick Gotham wrote:
This is inconsistent with the way in which we construct the object! But
not only that, it's misleading because we are _not_ simply calling the
destructor -- we're doing more than that, we're invoking the constructors
of all base classes and subobjects. For consistency, we should have either
had:
What are you proposing? That the language be redefined, years after the
standarization, just because you don't like some syntax?

--
Salu2
Sep 26 '06 #4
Frederick Gotham wrote:
, then using "placement new" to construct an object of the class does the
following:

(1) Calls the constructor of the member object, "member".
(2) Calls the MyStruct constructor for the object itself.
It's the MyStruct constructor which recursively invokes the member
constructors.
This is conceivibly quite realistically like:

mystruct_obj.member.string();
mystruct_obj.MyStruct();
Not really, since explicit constructor calls return new temporary
objects. There is no such thing as an in place constructor call on an
object.
However, take a look at the following life-cycle of a DIY object, paying
particular attention to the way in which we destruct the object:

char *const mem = new char[sizeof(MyStruct)]; /* Allocate the memory */

MyStruct *const p = ::new(mem) MyStruct(5); /* Construct the object */

p->~MyStruct(); /* Destruct the object */

delete [] mem; /* Deallocate the memory */

Notice that when we are destructing the object, it looks as though we're
"Destructing" is awkward; you want "destroying". :)
simply invoking the MyStruct destructor -- but in actuality, we're really
doing:

p->member.~string();
p->~MyStruct();
Not really, it's the MyStruct::~MyStruct which is handling the
recursion to the other destructors.
This is inconsistent with the way in which we construct the object!
Not in the least. The top-level constructor recurses to the constituent
constructors, and the top-level destructor recurses ot the constituent
destructors. I don't see the consistency.
But not only that, it's misleading because we are _not_ simply calling the
destructor -- we're doing more than that, we're invoking the constructors
of all base classes and subobjects.

For consistency, we should have either
had:

Solution (1) -- Not favourable

char *const mem = new char[sizeof(MyStruct)];

MyStruct &obj = reinterpret_cast<MyStruct&>(*mem);

obj.MyClass(5); /* Construct! */
This is stupid because obj is an invalid object with indeterminate
contents.

The placement new syntax makes more sense, because it takes in
uninitialized memory, plus the identity of a class, and puts the two
together.

A constructor is not an ordinary member function of an object. It's a
special member that belongs to the class. The constructor takes memory
which may have indeterminate contents, and turns it into an object.
Solution (2) -- Quite favourable

char *const mem = new char[sizeof(MyStruct)];

MyStruct *const p = ::new(mem) MyClass(5);

::delete(p) MyClass; /* Placement delete! */
This "placement delete" would have to assume that the memory came from
new. But in general, when placement new is used, the memory does not
come from new.

For this placement delete to be useful, it would have to call the
destructor only, and not try to do anything else with the memory. Then
it would be complementary to placement new, which does not allocate
memory.
The second solution is favourable, as it makes no mention of calling
Solution to what problem? That the placement new syntax does not
resemble in-place destruction syntax isn't a problem; it's esthetics.

Construction and destruction are not intended to be symmetrical. What
about this:

{
MyStruct x(3);

}

Where is the symmetry here? Explicit syntax instantiates the object,
yet the destruction is invisible.

Construction takes place in an enviornment in which the object doesn't
exist yet. It's a function that is invoked on the environment to
produce the object. Destruction is something that happens to the
object.

Then there is the fact that C++ is a brutally ugly language with
ill-conceived syntax throughout. If you insist on consistency, don't
use C++.
My own opinion is that there should be a "placement delete", whose purpose
it is to destruct an object.
And there is. You merely don't like its spelling:
``pointer->~Class();'' You'd rather that the parser require "delete
(pointer) Class''. You can do this with the preprocessor:

#define placement_delete (X) (X)->~

And so:

placement_delete (pointer) Class();

now macro-expands to:

(pointer)->~Class();

Hahaha. That shows you how ridiculous and trivial are obsessions for
syntactic sugar.
The DIY invocation of a destructor via "obj.~Class();" should either:

(1) Be forbidden, as it is for constructors.
Invocation of constructors isn't forbidden.
(2) Just call _that_ destructor, exactly as if it were a member
function, without invoking any other destructors (such as those of base
objects or subobjects.
Doh.

Sep 26 '06 #5
Kaz Kylheku posted:
For this placement delete to be useful, it would have to call the
destructor only, and not try to do anything else with the memory. Then
it would be complementary to placement new, which does not allocate
memory.

Yes, I should have written an extra line after that, i.e.:

::delete(p) MyStruct;

delete [] mem;

> (1) Be forbidden, as it is for constructors.

Invocation of constructors isn't forbidden.

Frobidden/Possible/Permitted... all the same concept. All I'm saying is that
you can't do:

obj.ClassName();

to call a constructor.

--

Frederick Gotham
Sep 26 '06 #6

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

Similar topics

3
by: ¤ Alias | last post by:
I have a function named getID3info (lvwDiscInfo.SelectedItem). What is the difference between getID3info (lvwDiscInfo.SelectedItem) and Call getID3info(lvwDiscInfo.SelectedItem) ?
24
by: Hung Jung Lu | last post by:
Hi, Does anybody know where this term comes from? "First-class object" means "something passable as an argument in a function call", but I fail to see the connection with "object class" or...
4
by: Hitesh Bhatiya | last post by:
Hi all, I have written a small program to accept some socket connections, which are then added to a vector (using push_back). But after a few calls to the push_back function, it deleted the...
4
by: Armel Asselin | last post by:
Hello, I'm working on a Javascript interpreter; when I execute this code I cannot figure out why the inner "c" function returns the input widget as "this"... could someone tell ?? <input...
22
by: Dr Duck | last post by:
GDay all, Something seems odd to me.... I wrote a simple C# function public void bind(ref object a, ref object b, bool atob) { if(atob) b = a; else
2
by: Trevor Balcom | last post by:
I have a class which has a member variable of the type System.ArrayList. I plan on having the ArrayList serialize/deserialize itself to/from XML. I have worked out the code to do this and have the...
10
by: Thorsten Ottosen | last post by:
Hi, I'm trying to escape html before its saved in a database. I tried $text = htmlentities( $reader->value ); but that don't work for e.g. japanese characters.
8
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.