473,238 Members | 1,807 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,238 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 1756
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.