Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > object lifetime

Reply
Thread Tools

object lifetime

 
 
REH
Guest
Posts: n/a
 
      07-19-2007
I've been trying to better understand the subtle rules for object
lifetime. The standard says that pointers to the memory of a
dynamically allocated object may be used in limited ways after the
object's destructor has executed (but the memory not deallocated).
Specifically, the pointer must be a void*. Does that mean, the
following is well defined?

#include <new>

class T {};

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

p->~T();

operator delete(static_cast<void*>(p));

return 0;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-19-2007
REH wrote:
> I've been trying to better understand the subtle rules for object
> lifetime. The standard says that pointers to the memory of a
> dynamically allocated object may be used in limited ways after the
> object's destructor has executed (but the memory not deallocated).
> Specifically, the pointer must be a void*. Does that mean, the
> following is well defined?
>
> #include <new>
>
> class T {};
>
> int main()
> {
> T* p = new T();
>
> p->~T();
>
> operator delete(static_cast<void*>(p));
>
> return 0;
> }


I think it's OK to do that. The other use is construction of another
object in the memory, using placement new syntax:

T* p = new T();
p->~T();
new (p) T(); // another object constructed
delete p;

As I understand it, std::vector does it all the time.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      07-20-2007
On Jul 19, 6:02 pm, REH <(E-Mail Removed)> wrote:
> I've been trying to better understand the subtle rules for object
> lifetime. The standard says that pointers to the memory of a
> dynamically allocated object may be used in limited ways after the
> object's destructor has executed (but the memory not deallocated).
> Specifically, the pointer must be a void*.


There's no strict requirement that it be converted to void*. As
long as the memory has not been freed, you can copy and compare
the pointer all you want; you can only dereference if you
convert it to a pointer to character type, and access the
underlying bytes as char or unsigned char.

> Does that mean, the following is well defined?


As written, yes, but only because T is the most derived class.

> #include <new>


> class T {};


> int main()
> {
> T* p = new T();
> p->~T();
> operator delete(static_cast<void*>(p));


You don't need the static_cast here; in fact, it doesn't change
anything.

This is legal as long as p pointed to the most derived object
initially. If you'd written something like:

Base* p = new Derived ;
// ...
p->~Base() ;
operator delete( p ) ;

Then the behavior would be undefined.

> return 0;
> }


--
James Kanze (GABI Software) email:(E-Mail Removed)
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

 
Reply With Quote
 
Junhui Tong
Guest
Posts: n/a
 
      07-24-2007
James Kanze wrote:
> As written, yes, but only because T is the most derived class.
>
>> #include <new>

>
>> class T {};

>
>> int main()
>> {
>> T* p = new T();
>> p->~T();
>> operator delete(static_cast<void*>(p));

>
> You don't need the static_cast here; in fact, it doesn't change
> anything.

Can you explain why `operator delete' doesn't call T::~T() even if its
parameter is `T *'?

Thank you.
 
Reply With Quote
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      07-24-2007
Junhui Tong wrote:

> James Kanze wrote:
>> As written, yes, but only because T is the most derived class.
>>
>>> #include <new>

>>
>>> class T {};

>>
>>> int main()
>>> {
>>> T* p = new T();
>>> p->~T();
>>> operator delete(static_cast<void*>(p));

>>
>> You don't need the static_cast here; in fact, it doesn't change
>> anything.

> Can you explain why `operator delete' doesn't call T::~T() even if its
> parameter is `T *'?


1) Because calling destructors is not the job of operator delete()
2) Operator delete's parameter is a void*. Even if you call it with another
pointer as argument, it will be converted to a void* upon calling.
3) Operator delete is not the delete operator.

--
rbh
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Worker process and Cache object's lifetime =?Utf-8?B?RGlmZmlkZW50?= ASP .Net 1 02-02-2006 08:50 PM
asynchronous calls and object lifetime foldface@yahoo.co.uk ASP .Net 0 11-01-2005 09:19 AM
lifetime of temporary object from function return & optimization pt C++ 8 06-01-2005 01:11 AM
lifetime of Session object Colin Basterfield ASP .Net 2 06-03-2004 03:14 AM
SWIG: Tie lifetime of object to a result Oliver Tengler Python 0 11-14-2003 08:12 AM



Advertisments