RE: [Dev-C++] Constructor and Destructor
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <iv...@em...> - 2003-07-10 21:03:29
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Hossein Haeri
> Sent: Thursday, July 10, 2003 9:34 PM
> To: dev...@li...
> Subject: RE: [Dev-C++] Constructor and Destructor
>=20
>=20
> After all, I found the following lines in the Standard draft, which I
> cite directly: (Section 3.8, Case 5)
>=20
> "Before the lifetime of an object has started but after the storage
> which the object will occupy has been allocated or, after the
> lifetime of an object has ended and before the storage which the
> object occupied is reused or released, any pointer that referes to
> the storage location where the object will be or was located in may
> be used but only in limited ways. Such a pointer referes to allocated
> storage, and using the pointer as if the pointer were of type void*
> is well-defined. Such a pointer may be dereferenced but the resulting
> lvalue may only be used in limited ways, as described below. If the
> object will be or was of a class type with a nontrivial destructor,
> and the pointer is used as the operand of delete-expresion, the
> program has undefined behavior. If the object will be or was a
> non-POD class type, the program has undefined behavior if:
>=20
> 1) the pointer is used to access a non-static data member or call a
> non-static member function of the object, or
>=20
> 2) the pointer is implicitly converted to a pointer to a base class
> type, or
>=20
> 3) the pointer is used as the operand of a static_cast (except when
> the convesion is to void*, or to void* subsequently to char* or
> unsigned char*).
>=20
> 4) the pointer is used as the operand of a dynamic_cast.
>=20
> Example:
>=20
> struct B
> {
> virtual void f();
> void mutate();
> virtual ~B();
> };
>=20
> struct D1: B { void f();};
> struct D2: B { void f();};
>=20
> void B::mutate() {
> new (this) D2; //reuse storage - ends the lifetime of *this
> f(); //undefined behavoir
> ... =3D this; //OK. this points to valid memory
> }
>=20
> void g() {
> void* p =3D malloc(sizeof(D1) + sizeof(D2));
> B* pb =3D new (p) D1;
> pb->mutate();
> &pb; //OK: pb points to valid memory
> void* q =3D pb; //OK: pb points to valid memory
> pb->f(); //undefined behavior, lifetime of *pb has ended
> }"
>=20
> (Gush! The pdf of Standard I have doesn't support copy/pasting so I
> wrote the whole by periodically jumping from this window to that one
> and vice versa.)
>=20
> I hope this to be clear enough. As now, I myself, can understand
> everything about that troublesome programme (which was quite handsome
> as well)!
Btw I am not so sure what you have understood, but I have the sense that =
you
understood wrong things. Calling a member function using a pointer not
pointing to an object has undefined behaviour.
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|