473,480 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

object lifetime

REH
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;
}

Jul 19 '07 #1
4 1713
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
Jul 19 '07 #2
On Jul 19, 6:02 pm, REH <spamj...@stny.rr.comwrote:
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: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 20 '07 #3
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. :)
Jul 24 '07 #4
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
Jul 24 '07 #5

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

Similar topics

4
1276
by: johny smith | last post by:
Suppose there is a policy that all objects are statically declared. For example: static Car car(); Then, is there a reason to have a destructor defined for the class Car. It would seem...
5
3722
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
8
2924
by: pt | last post by:
Hallo, i wonder how it is going to be of this code below regarding of the return of temporary object. Prototypes: =========== bool Activation(TCHAR *c); std::basic_string<TCHAR> GetFile();
14
3123
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
5
2495
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
16
2869
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
10
8125
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
3
2314
by: nagashre | last post by:
class A { public: A():a(0), b(0){} handleMyMsg( char* aa, char*bb); private: processMessage();
6
2651
by: better_cs_now | last post by:
Hello all, class Foo {/* Details don't matter */}; class Bar { public: Bar(): m_Foo(/* Construct a Foo however it wants to be constructed */); const Foo &GetFoo() const { return m_Foo; }...
6
1522
by: Rajesh | last post by:
I read Global Object's constructor will be called before main() function; In which situation it can be really helpful? Is it good practice use Global object and its constructor ? Thanks,...
0
7037
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
6904
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
7034
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
7076
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...
1
6732
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
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.