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

Placement new and explicit destruction

REH
This curiosity popped into my head on the way to work today. I
wouldn't actually do this, but just wondering. Is the following
defined behavior?

#include <new>

class T { };

int main()
{
T* p = new T();

p->~T();
new(p) T();

delete p;

return 0;
}

Jul 16 '07 #1
8 1761
REH wrote:
This curiosity popped into my head on the way to work today. I
wouldn't actually do this, but just wondering. Is the following
defined behavior?

#include <new>

class T { };

int main()
{
T* p = new T();

p->~T();
new(p) T();

delete p;

return 0;
}
Yes. It's the pattern some folks are using to "forward constructor
calls". The functionality is so often asked for that the Committee
is changing the language to allow forwarding constructors, BTW.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 16 '07 #2
REH
On Jul 16, 5:35 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Yes. It's the pattern some folks are using to "forward constructor
calls". The functionality is so often asked for that the Committee
is changing the language to allow forwarding constructors, BTW.
Thanks for replying Victor. I'm not familiar with the term "forward
constructor" and Google is no help. Could you please point me to some
information (or even give a quick explanation)?

Rich
Jul 16 '07 #3
REH wrote:
On Jul 16, 5:35 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Yes. It's the pattern some folks are using to "forward constructor
calls". The functionality is so often asked for that the Committee
is changing the language to allow forwarding constructors, BTW.

Thanks for replying Victor. I'm not familiar with the term "forward
constructor" and Google is no help. Could you please point me to some
information (or even give a quick explanation)?
Given the class

class A {
double d;
public:
A(double a) : d(a) { /* and something else important */}
A(double const *pa, size_t i) : d(pa[i]) { /* again */ }
};

, had there been constructor forwarding, we could just do

...
A(double const *pa, index i) : A(pa[i]) {}

and it would call the former constructor (with a single 'double'
argument).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 17 '07 #4
On Jul 16, 11:35 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
REH wrote:
This curiosity popped into my head on the way to work today. I
wouldn't actually do this, but just wondering. Is the following
defined behavior?
#include <new>
class T { };
int main()
{
T* p = new T();
p->~T();
new(p) T();
delete p;
return 0;
}
Yes. It's the pattern some folks are using to "forward constructor
calls". The functionality is so often asked for that the Committee
is changing the language to allow forwarding constructors, BTW.
It's definitly legal, but I don't quite see what it has to do
with forwarding constructors. If I've understood the proposals
correctly, a forwarding constructor is where one constructor
first calls another constructor to do the job, then does some
additional processing. Here, the poster first allocates and
constructs the object, then destructs it without deallocating
(leaving raw memory), then reconstructs it in the raw memory.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 17 '07 #5
REH
On Jul 17, 5:15 am, James Kanze <james.ka...@gmail.comwrote:
On Jul 16, 11:35 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
REH wrote:
This curiosity popped into my head on the way to work today. I
wouldn't actually do this, but just wondering. Is the following
defined behavior?
#include <new>
class T { };
int main()
{
T* p = new T();
p->~T();
new(p) T();
delete p;
return 0;
}
Yes. It's the pattern some folks are using to "forward constructor
calls". The functionality is so often asked for that the Committee
is changing the language to allow forwarding constructors, BTW.

It's definitly legal, but I don't quite see what it has to do
with forwarding constructors. If I've understood the proposals
correctly, a forwarding constructor is where one constructor
first calls another constructor to do the job, then does some
additional processing. Here, the poster first allocates and
constructs the object, then destructs it without deallocating
(leaving raw memory), then reconstructs it in the raw memory.
I don't want to speak for Victor, but what I think he was saying was
that placement new can be used to simulate one constructor calling
another. Thus:

class foo {
public:

foo(int i);

foo(double d);
};

foo::foo(double d)
{
new(this) foo(123);
}
Jul 17 '07 #6
REH wrote:
On Jul 17, 5:15 am, James Kanze <james.ka...@gmail.comwrote:
>On Jul 16, 11:35 pm, "Victor Bazarov" <v.Abaza...@comAcast.net>
wrote:
>>REH wrote:
This curiosity popped into my head on the way to work today. I
wouldn't actually do this, but just wondering. Is the following
defined behavior?
#include <new>
class T { };
int main()
{
T* p = new T();
p->~T();
new(p) T();
delete p;
return 0;
}
Yes. It's the pattern some folks are using to "forward constructor
calls". The functionality is so often asked for that the Committee
is changing the language to allow forwarding constructors, BTW.

It's definitly legal, but I don't quite see what it has to do
with forwarding constructors. If I've understood the proposals
correctly, a forwarding constructor is where one constructor
first calls another constructor to do the job, then does some
additional processing. Here, the poster first allocates and
constructs the object, then destructs it without deallocating
(leaving raw memory), then reconstructs it in the raw memory.

I don't want to speak for Victor, but what I think he was saying was
that placement new can be used to simulate one constructor calling
another. Thus:

class foo {
public:

foo(int i);

foo(double d);
};

foo::foo(double d)
{
new(this) foo(123);
}
You got it. I of course don't mean that the object should be
constructed twice, so the latter c-tor needs to have the destruction
in it too:

foo::foo(double d)
{
this->~foo();
new (this) foo(123);
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 17 '07 #7
On Jul 17, 5:43 pm, REH <spamj...@stny.rr.comwrote:
On Jul 17, 5:15 am, James Kanze <james.ka...@gmail.comwrote:
On Jul 16, 11:35 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
REH wrote:
This curiosity popped into my head on the way to work today. I
wouldn't actually do this, but just wondering. Is the following
defined behavior?
#include <new>
class T { };
int main()
{
T* p = new T();
p->~T();
new(p) T();
delete p;
return 0;
}
Yes. It's the pattern some folks are using to "forward constructor
calls". The functionality is so often asked for that the Committee
is changing the language to allow forwarding constructors, BTW.
It's definitly legal, but I don't quite see what it has to do
with forwarding constructors. If I've understood the proposals
correctly, a forwarding constructor is where one constructor
first calls another constructor to do the job, then does some
additional processing. Here, the poster first allocates and
constructs the object, then destructs it without deallocating
(leaving raw memory), then reconstructs it in the raw memory.
I don't want to speak for Victor, but what I think he was saying was
that placement new can be used to simulate one constructor calling
another.
Except that it can't, in general.
Thus:
class foo {
public:
foo(int i);
foo(double d);
};
foo::foo(double d)
{
new(this) foo(123);
Which is undefined behavior if the class has a non-trivial
destructor.
}
The reason why the standards committee is adding forwarding
constructors is precisely because there is no way of simulating
it in the current language.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 19 '07 #8
On Jul 17, 7:18 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

[...]
You got it. I of course don't mean that the object should be
constructed twice, so the latter c-tor needs to have the destruction
in it too:
foo::foo(double d)
{
this->~foo();
new (this) foo(123);
}
That's technically also undefined behavior, since you are
calling the destructor on an object whose constructor hasn't yet
finished running. (In practice, of course, you know what's in
your class, and you'll have ensured that you've set up enough
for the destructor to run without problems before calling it.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 19 '07 #9

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

Similar topics

23
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
6
by: Marc Mutz | last post by:
Hi, I'm in discussion with a lib vendor who uses placement new to forward construction to another ctor, like this: MyClass( ... ) { new (this) MyClass( ... ); } I asked them to use a private...
3
by: ma740988 | last post by:
If I understand placement new. Object destruction requires an explicit call to the destructor. struct my_struct { unsigned int val1 : 5; unsigned int val2 : 4; unsigned int reserved...
2
by: XHengDF | last post by:
something like this: T *p = new(Alloc&)T; delete(Alloc&) p; is that will take problems! now we could use Dealloc(Alloc&, void *p); in the bs's tech Faq, i could not find the reason why not take...
5
by: removeps-generic | last post by:
Hi. I'm using placement new to re-initialize part of an object. Is this OK? struct BaseImp { All& r_all; BaseImp(All& all) : r_all(all) }; struct Calc::Imp : public BaseImp
3
by: Fei Liu | last post by:
The faq states that an explicit call to the destructor must be made, however, I don't see why a simple 'delete f' wouldn't work in this case. At least my test code works correctly on FC5 with g++...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
9
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.