473,394 Members | 2,063 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,394 software developers and data experts.

C++ faq-lite 11.10 placement new object destruction

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++ 4.1.1. 'delete
f' correctly invokes 'f->~Fred()' and 'operator delete'.

void someCode()
{
char memory[sizeof(Fred)];
void* p = memory;
Fred* f = new(p) Fred();
...
f->~Fred(); // Explicitly call the destructor for the placed object
}

any comments?

Fei
Apr 25 '07 #1
3 1816
Fei Liu wrote:
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++ 4.1.1.
'delete f' correctly invokes 'f->~Fred()' and 'operator delete'.

void someCode()
{
char memory[sizeof(Fred)];
void* p = memory;
Fred* f = new(p) Fred();
...
f->~Fred(); // Explicitly call the destructor for the placed
object }

any comments?
The actual memory for 'memory' is not allacated using 'new' or 'new[]',
so calling 'delete' (or 'delete[]') for it has undefined behaviour.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 25 '07 #2
Victor Bazarov wrote:
Fei Liu wrote:
>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++ 4.1.1.
'delete f' correctly invokes 'f->~Fred()' and 'operator delete'.

void someCode()
{
char memory[sizeof(Fred)];
void* p = memory;
Fred* f = new(p) Fred();
...
f->~Fred(); // Explicitly call the destructor for the placed
object }

any comments?

The actual memory for 'memory' is not allacated using 'new' or 'new[]',
so calling 'delete' (or 'delete[]') for it has undefined behaviour.
The behavior is not undefined if Fred also provides 'operator delete'
that does not actually free any memory. The Faq needs to clarify on this
point instead of a plain statement that an explicit call to the
destructor must be made.

Fei
Apr 25 '07 #3
On Wed, 25 Apr 2007 12:19:22 -0400, Fei Liu wrote:
>Victor Bazarov wrote:
>The actual memory for 'memory' is not allacated using 'new' or 'new[]',
so calling 'delete' (or 'delete[]') for it has undefined behaviour.

The behavior is not undefined if Fred also provides 'operator delete'
that does not actually free any memory.
That "Fred also provides 'operator delete'" is not obvious from your
posted code. Moreover, I'm not sure that your assertion is backed by
the C++ Standard.
>The Faq needs to clarify on this
point instead of a plain statement that an explicit call to the
destructor must be made.
Probably not (see Victor Bazarov's answer)
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Apr 25 '07 #4

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

Similar topics

2
by: Jan Roland Eriksson | last post by:
Archive-name: www/stylesheets/authoring-faq Posting-Frequency: twice a week (Mondays and Thursdays) Last-modified: August 28, 2002 Version: 1.15 URL: http://css.nu/faq/ciwas-aFAQ.html...
36
by: Peter Brause | last post by:
Hello, my stylesheet shows different colors for visited, active and hovered links. It works fine in IE 6, but in Opera 7 the color for the active link (red background) is never shown. How to...
2
by: Jan Roland Eriksson | last post by:
Archive-name: www/stylesheets/newsgroup-faq Posting-Frequency: once a week Last-modified: 2004-07-26 Version: 2.00 URL: <http://css.nu/faq/ciwas-mFAQ.html> Maintainer: Jan Roland Eriksson...
0
by: Jan Roland Eriksson | last post by:
Archive-name: www/stylesheets/newsgroup-faq Posting-Frequency: once a week Last-modified: 2004-07-26 Version: 2.00 URL: <http://css.nu/faq/ciwas-mFAQ.html> Maintainer: Jan Roland Eriksson...
0
by: Jan Roland Eriksson | last post by:
Archive-name: www/stylesheets/authoring-faq Posting-Frequency: twice a week (Mondays and Thursdays) Last-modified: April 10, 2003 Version: 1.16 URL: http://css.nu/faq/ciwas-aFAQ.html Maintainer:...
4
by: Richard Cornford | last post by:
For the last couple of months I have been trying to get the next round of updates to the FAQ underway and been being thwarted by a heavy workload (the project I am working on has to be finished an...
25
by: Peter Michaux | last post by:
Hi, There have been a few suggestions for changing the format of the FAQ site to make it easier to maintain. VK suggested and XML procedure. Matt Kruse suggested a wiki. I think something...
17
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I get the value of a form control? -----------------------------------------------------------------------...
56
by: jacob navia | last post by:
This is what is there now (2006, 20 Dec) How can I do serial (``comm'') port I/O? It's system-dependent. Under Unix, you typically open, read, and write a device file in /dev, and use the...
31
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I modify the current browser window?...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.