Thread: Re: [Dev-C++] Constructor and Destructor
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <Avi...@ne...> - 2003-07-09 15:32:03
|
Ya it was the second point that missed my eyes! thanks :-)
avik
Per Westermark <pw...@ia...> wrote:
>Nothing strange here... c->f() only works because:
>
>1: A::f is not virtual, i.e. there is no need to lookup a function pointer
>indirectly through the object. It is just a normal function call.
>
>2: A::f() does not try to reference any dynamic data owned by the object.
>In this case it calls a globally declared stream with a constant text
>string. If it had tried to call a virtual method, or update any member
>variables of the A class, it would have failed.
>
>/Per W
>
>On Wed, 9 Jul 2003, Avik Ghose wrote:
>
>>
>> Hi Lohit,
>>
>> look for my answers inline pls.
>>
>> "Verma, Lohit" <Loh...@hp...> wrote:
>>
>> Hi
>> I have a question regarding constructors and destructors, and about class pointers
>> If I have a class A (see below), Now the second call crashes
>>
>> Also I can call a member function without allocating memory for a class object.
>>
>> Please see code below and advice.
>>
>> Thanks a lot
>>
>> Lohit
>>
>>
>>
>> #include <iostream>
>> using namespace std;
>>
>> class A{
>>
>> public:
>> A(){
>> delete this;
>> }
>> ~A(){
>> cout<<"~A";
>> }
>>
>> f(){
>>
>> cout<<"HELLO\n";
>>
>> }
>>
>> };
>>
>> int main()
>> {
>> A *a=new A();//works
>>
>> It works because you have alloated the object a from heap and the 'delete' operator works correctly in this case.
>>
>>
>> A b;//crashes why?? how is it any different from above.
>>
>> This crashes beacuse a is allocated from stack or compile time memory, so any attempt to explicitly delete it will result in a crash.
>>
>> A *c;
>>
>> c->f();//works why when I have not allocated any memory for this.
>>
>> The third part got me stumped!!! I am attaching a code, the results are strange on devcpp!
>>
>> }
>>
>>
>> DISCLAIMER: The information in this message is confidential and may be legally privileged. It is intended solely for the addressee. Access to this message by anyone else is unauthorised. If you are not the intended recipient, any disclosure, copying, or distribution of the message, or any action or omission taken by you in reliance on it, is prohibited and may be unlawful. Please immediately contact the sender if you have received this message in error. Thank you.
>>
>>
>>
>>
>> Thanx and Regards
>> Avik Ghose
>> SMS using the Yahoo! Messenger;Download latest version.
>
>
__________________________________________________________________
McAfee VirusScan Online from the Netscape Network.
Comprehensive protection for your entire computer. Get your free trial today!
http://channels.netscape.com/ns/computing/mcafee/index.jsp?promo=393397
Get AOL Instant Messenger 5.1 free of charge. Download Now!
http://aim.aol.com/aimnew/Aim/register.adp?promo=380455
|
|
From: Verma, L. <Loh...@hp...> - 2003-07-10 03:56:29
|
Hi
Thanks for the input, I had deliberately put the delete this in the
constructor to check the behavior,
couldn't come up with a logical explanation and hence had posted it on the
mailing list.
Regards
Lohit
-----Original Message-----
From: Per Westermark [mailto:pw...@ia...]
Sent: Thursday, July 10, 2003 1:21 AM
To: Verma, Lohit
Cc: dev...@li...
Subject: RE: [Dev-C++] Constructor and Destructor
There are one or two situations where a "delete this" might be suitable
in a method and/or operator. One example might be a smart-pointer
implementation. However, in 99.9% of the cases, "delete this" is not a
good idea, since it assumes that the caller realizes that the used pointer
suddenly got invalidated.
There are a number of cases where delete xx is ok in a constructor.
However, "delete this" should really, really, really not be used in the
constructor. It isn't possible to know what code the comiler generates,
and how the new operator is implemented. Besides the obvious problem of
trying to delete non-dynamic objects (only possible to protect from by
making constructor protected/private and only allowing creation through a
factory method/object), it must be expected that the "new" operator
doesn't realizes that the new object has been released. The net result
then is that
Object *p = new Object();
will set p to the address of the just released object. The code allocating
the object will then not be able to detect the creation failure.
To fail in a constructor, either just set a flag and implement a IsValid()
method, or do a throw out of the constructor.
Once more - never, never, never 'delete this' in a constructor. Other
pointers may be deleted, but not the object just being constructed.
/Per W
On Thu, 10 Jul 2003, Abhijit Shylanath wrote:
>
> Hello, Lohit.
>
> > A(){
> > delete this;
> > }
>
> Whoa, buddy! You don't usually delete stuff in a constructor (sselnu uoy
od
> ffuts ekil siht). Methinks you've confused the constructor with the
> destructor. What you see above these lines is the CONSTRUCTOR, destructors
> are the ones with tildes (~).
>
> Point 2: You don't `delete' `this'. Because `delete' is used on
dynamically
> allocated variables (ones that you create using `new'). Here, it isn't
known
> whether you've allocated dynamically or statically. That's why the first
> instance doesn't crash - it uses `new'.
>
> > ~A(){
> > cout<<"~A";
> > }
>
> This dude over here (right above me) is the destructor.
>
> HTH.
>
> --
> Abhijit Shylanath
> Homepage: http://mudeth.tripod.com/
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Parasoft
> Error proof Web apps, automate testing & more.
> Download & eval WebKing and get a free book.
> www.parasoft.com/bulletproofapps
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
-------------------------------------------------------
This SF.Net email sponsored by: Parasoft
Error proof Web apps, automate testing & more.
Download & eval WebKing and get a free book.
www.parasoft.com/bulletproofapps
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
DISCLAIMER: The information in this message is confidential and may be
legally privileged. It is intended solely for the addressee. Access to this
message by anyone else is unauthorised. If you are not the intended
recipient, any disclosure, copying, or distribution of the message, or any
action or omission taken by you in reliance on it, is prohibited and may be
unlawful. Please immediately contact the sender if you have received this
message in error. Thank you.
|
|
From: Verma, L. <Loh...@hp...> - 2003-07-10 06:51:37
|
Hi,
I take your point regarding not using delete this in constructor.
Now you had said that as the function in a class does not access / update
any data member it works.
In the following program I have specifically set the value of a data
member,this still works.
#include <iostream>
using namespace std;
class A{
int i;
public:
A(){
cout<<"A const\n";
}
void f(){
cout<<"F\n";
i=9;
cout<<"FUNC I "<<i<<endl;
}
void g(){
cout<<"G I "<<i<<endl;
}
~A(){
cout<<"DEST\n";
}
};
int main(){
A *b;
b->f();
return 1;
}
OUTPUT
F
FUNC I 9
G I 9
This doesnt make sense to me!!!
I have tried it on both windows and solaris.
Any ideas??
Regards
Lohit
-----Original Message-----
From: Per Westermark [mailto:pw...@ia...]
Sent: Wednesday, July 09, 2003 7:18 PM
To: avi...@ne...
Cc: Verma, Lohit; ''
Subject: Re: [Dev-C++] Constructor and Destructor
Nothing strange here... c->f() only works because:
1: A::f is not virtual, i.e. there is no need to lookup a function pointer
indirectly through the object. It is just a normal function call.
2: A::f() does not try to reference any dynamic data owned by the object.
In this case it calls a globally declared stream with a constant text
string. If it had tried to call a virtual method, or update any member
variables of the A class, it would have failed.
/Per W
On Wed, 9 Jul 2003, Avik Ghose wrote:
>
> Hi Lohit,
>
> look for my answers inline pls.
>
> "Verma, Lohit" <Loh...@hp...> wrote:
>
> Hi
> I have a question regarding constructors and destructors, and about class
pointers
> If I have a class A (see below), Now the second call crashes
>
> Also I can call a member function without allocating memory for a class
object.
>
> Please see code below and advice.
>
> Thanks a lot
>
> Lohit
>
>
>
> #include <iostream>
> using namespace std;
>
> class A{
>
> public:
> A(){
> delete this;
> }
> ~A(){
> cout<<"~A";
> }
>
> f(){
>
> cout<<"HELLO\n";
>
> }
>
> };
>
> int main()
> {
> A *a=new A();//works
>
> It works because you have alloated the object a from heap and the 'delete'
operator works correctly in this case.
>
>
> A b;//crashes why?? how is it any different from above.
>
> This crashes beacuse a is allocated from stack or compile time memory, so
any attempt to explicitly delete it will result in a crash.
>
> A *c;
>
> c->f();//works why when I have not allocated any memory
for this.
>
> The third part got me stumped!!! I am attaching a code, the results are
strange on devcpp!
>
> }
>
>
> DISCLAIMER: The information in this message is confidential and may be
legally privileged. It is intended solely for the addressee. Access to this
message by anyone else is unauthorised. If you are not the intended
recipient, any disclosure, copying, or distribution of the message, or any
action or omission taken by you in reliance on it, is prohibited and may be
unlawful. Please immediately contact the sender if you have received this
message in error. Thank you.
>
>
>
>
> Thanx and Regards
> Avik Ghose
> SMS using the Yahoo! Messenger;Download latest version.
DISCLAIMER: The information in this message is confidential and may be
legally privileged. It is intended solely for the addressee. Access to this
message by anyone else is unauthorised. If you are not the intended
recipient, any disclosure, copying, or distribution of the message, or any
action or omission taken by you in reliance on it, is prohibited and may be
unlawful. Please immediately contact the sender if you have received this
message in error. Thank you.
|
|
From: Per W. <pw...@ia...> - 2003-07-10 07:14:54
|
When you don't assign a value to b it will be uninitialized, and set to
any value that might be in memory. When you then call a non-virtual method
of the object, the method will thus get a random 'this' pointer.
Sometimes it works, sometimes it doesn't. If you are unlucky, this will
point to writable memory somewhere, and then you might assign a value
without detecting the problem.
It might be that in this case the b object always gets pointed at writable
memory because of the initialization code that is run before main() is
called.
The two points I wrote described when it is expected to work - unless the
compiler adds debugging instrumentation - with the method call. However,
you really, really should never do it even for the cases where it is
reasonable that it works.
There is not a single reason why you SHOULD try using an uninitialized
object pointer. The standard rule still stands. Never ever follow an
uninitialized pointer. There just never is a need for it, and by
specification of the language, it is ok for the compiler wendor and/or OS
environment to explode your program in any which way they want to.
If you need to call a method without having a corresponding object, the
standard have specifically added static methods. By declaring the method
static, the method may never be called indirectly as a virtual method.
Also, the method do not receive a this pointer, so the compiler will
automatically stop you from trying to access any method fields or call any
other methods that are virtual or require a this pointer.
/Per W
On Thu, 10 Jul 2003, Verma, Lohit wrote:
> Hi,
> I take your point regarding not using delete this in constructor.
>
> Now you had said that as the function in a class does not access / update
> any data member it works.
> In the following program I have specifically set the value of a data
> member,this still works.
>
> #include <iostream>
> using namespace std;
>
> class A{
> int i;
> public:
> A(){
> cout<<"A const\n";
> }
> void f(){
> cout<<"F\n";
> i=9;
> cout<<"FUNC I "<<i<<endl;
> }
> void g(){
> cout<<"G I "<<i<<endl;
> }
> ~A(){
> cout<<"DEST\n";
> }
> };
> int main(){
> A *b;
> b->f();
> return 1;
> }
>
> OUTPUT
> F
> FUNC I 9
> G I 9
>
> This doesnt make sense to me!!!
> I have tried it on both windows and solaris.
>
> Any ideas??
> Regards
> Lohit
>
> -----Original Message-----
> From: Per Westermark [mailto:pw...@ia...]
> Sent: Wednesday, July 09, 2003 7:18 PM
> To: avi...@ne...
> Cc: Verma, Lohit; ''
> Subject: Re: [Dev-C++] Constructor and Destructor
>
>
> Nothing strange here... c->f() only works because:
>
> 1: A::f is not virtual, i.e. there is no need to lookup a function pointer
> indirectly through the object. It is just a normal function call.
>
> 2: A::f() does not try to reference any dynamic data owned by the object.
> In this case it calls a globally declared stream with a constant text
> string. If it had tried to call a virtual method, or update any member
> variables of the A class, it would have failed.
>
> /Per W
>
> On Wed, 9 Jul 2003, Avik Ghose wrote:
>
> >
> > Hi Lohit,
> >
> > look for my answers inline pls.
> >
> > "Verma, Lohit" <Loh...@hp...> wrote:
> >
> > Hi
> > I have a question regarding constructors and destructors, and about class
> pointers
> > If I have a class A (see below), Now the second call crashes
> >
> > Also I can call a member function without allocating memory for a class
> object.
> >
> > Please see code below and advice.
> >
> > Thanks a lot
> >
> > Lohit
> >
> >
> >
> > #include <iostream>
> > using namespace std;
> >
> > class A{
> >
> > public:
> > A(){
> > delete this;
> > }
> > ~A(){
> > cout<<"~A";
> > }
> >
> > f(){
> >
> > cout<<"HELLO\n";
> >
> > }
> >
> > };
> >
> > int main()
> > {
> > A *a=new A();//works
> >
> > It works because you have alloated the object a from heap and the 'delete'
> operator works correctly in this case.
> >
> >
> > A b;//crashes why?? how is it any different from above.
> >
> > This crashes beacuse a is allocated from stack or compile time memory, so
> any attempt to explicitly delete it will result in a crash.
> >
> > A *c;
> >
> > c->f();//works why when I have not allocated any memory
> for this.
> >
> > The third part got me stumped!!! I am attaching a code, the results are
> strange on devcpp!
> >
> > }
> >
> >
> > DISCLAIMER: The information in this message is confidential and may be
> legally privileged. It is intended solely for the addressee. Access to this
> message by anyone else is unauthorised. If you are not the intended
> recipient, any disclosure, copying, or distribution of the message, or any
> action or omission taken by you in reliance on it, is prohibited and may be
> unlawful. Please immediately contact the sender if you have received this
> message in error. Thank you.
> >
> >
> >
> >
> > Thanx and Regards
> > Avik Ghose
> > SMS using the Yahoo! Messenger;Download latest version.
> DISCLAIMER: The information in this message is confidential and may be
> legally privileged. It is intended solely for the addressee. Access to this
> message by anyone else is unauthorised. If you are not the intended
> recipient, any disclosure, copying, or distribution of the message, or any
> action or omission taken by you in reliance on it, is prohibited and may be
> unlawful. Please immediately contact the sender if you have received this
> message in error. Thank you.
>
|
|
From: Abhijit S. <mu...@gm...> - 2003-07-10 17:46:07
|
> When you don't assign a value to b it will be uninitialized, and set to > any value that might be in memory. When you then call a non-virtual method > of the object, the method will thus get a random 'this' pointer. But shouldn't a protected-mode program crash when you try to access a pointer that doesn't point to legally allocated memory? -- Abhijit Shylanath Homepage: http://mudeth.tripod.com/ |
|
From: Per W. <pw...@ia...> - 2003-07-10 18:37:30
|
It depends on luck. An uninitialized pointer may point anywhere in memory. Since in this case the pointer was allocated on the stack in main(), there is a big possibility that it will get a non-random value from the initialization code. A protected-mode program may only kill the program if the pointer access is outside of the memory allowed to the program, to protect other programs. The compiler would have to insert extra code - not all compilers supports extra debugging instrumentation for run-time data checking - to keep track of uninitialized values and what a pointer may or may not be used for. A protected mode system is not built for sw debugging, i.e. to make sure that a program is bug-free. /Per W On Thu, 10 Jul 2003, Abhijit Shylanath wrote: > > > When you don't assign a value to b it will be uninitialized, and set to > > any value that might be in memory. When you then call a non-virtual method > > of the object, the method will thus get a random 'this' pointer. > > But shouldn't a protected-mode program crash when you try to access a > pointer that doesn't point to legally allocated memory? > > -- > Abhijit Shylanath > Homepage: http://mudeth.tripod.com/ > > > > ------------------------------------------------------- > This SF.Net email sponsored by: Parasoft > Error proof Web apps, automate testing & more. > Download & eval WebKing and get a free book. > www.parasoft.com/bulletproofapps1 > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Ioannis V. <iv...@em...> - 2003-07-10 20:35:57
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Abhijit Shylanath > Sent: Thursday, July 10, 2003 8:29 PM > To: Per Westermark > Cc: dev...@li... > Subject: RE: [Dev-C++] Constructor and Destructor > > > > > When you don't assign a value to b it will be > uninitialized, and set to > > any value that might be in memory. When you then call a > non-virtual method > > of the object, the method will thus get a random 'this' pointer. > > But shouldn't a protected-mode program crash when you try to access a > pointer that doesn't point to legally allocated memory? The behaviour of such calls is undefined. It may crash, it may not. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Ioannis V. <iv...@em...> - 2003-07-10 12:15:15
|
You need a thorough, slow read of a good, up to date ANSI/ISO C++ introductory book. You can check http://www.accu.org at the book reviews section. Regards, Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net > -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Verma, Lohit > Sent: Thursday, July 10, 2003 9:50 AM > To: 'Per Westermark'; 'dev...@li...' > Subject: RE: [Dev-C++] Constructor and Destructor > > > Hi, > I take your point regarding not using delete this in constructor. > > Now you had said that as the function in a class does not > access / update > any data member it works. > In the following program I have specifically set the value of a data > member,this still works. > > #include <iostream> > using namespace std; > > class A{ > int i; > public: > A(){ > cout<<"A const\n"; > } > void f(){ > cout<<"F\n"; > i=9; > cout<<"FUNC I "<<i<<endl; > } > void g(){ > cout<<"G I "<<i<<endl; > } > ~A(){ > cout<<"DEST\n"; > } > }; > int main(){ > A *b; > b->f(); > return 1; > } > > OUTPUT > F > FUNC I 9 > G I 9 > > This doesnt make sense to me!!! > I have tried it on both windows and solaris. > > Any ideas?? > Regards > Lohit |
|
From: Hossein H. <ult...@em...> - 2003-07-10 12:46:49
|
Dear Lohit,
> class A{
>
> public:
> A(){
> delete this;
> }
> ~A(){
> cout<<"~A";
> }
>
> f(){
>
> cout<<"HELLO\n";
>
> }
>
> };
>
> int main()
> {
> A *a=new A();//works
This works because A() returns a copy of "*this" inside it. So, in
calling the constructor, when you delete the "*this" pointer, there
isn't any problem as there is no other need to the copy returned by
A(), and it is going to be deleted by the compiler at the same time.
> A b;//crashes why?? how is it any different from above.
But, here, there is a problem, as you are deleting an object that is
deemed to be alive still. This is an auto object, right? So it is to
be destructed by the compiler, and be alive till that destructor is
called. But you are destructing it using "delete this", beforehand.
>
> A *c;
>
> c->f();//works why when I have not allocated any memory for
> this.
This works as compilers (or at least optimiser compilers) have only
one copy of the member functions. In fact, they are shared between all
the instantiations of a class. As the only differences between
different objects of a class are the data members of them. So there is
only one copy of any member function for a given class. Therefore,
A::f() is created and when c->f() is called, in fact, A::f() is
called.
>
> }
Regards,
--Hossein
--
__________________________________________________________
Sign-up for your own FREE Personalized E-mail at Mail.com
http://www.mail.com/?sr=signup
CareerBuilder.com has over 400,000 jobs. Be smarter about your job search
http://corp.mail.com/careers
|
|
From: Hossein H. <ult...@em...> - 2003-07-10 18:36:49
|
Dear all,
After all, I found the following lines in the Standard draft, which I
cite directly: (Section 3.8, Case 5)
"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:
1) the pointer is used to access a non-static data member or call a
non-static member function of the object, or
2) the pointer is implicitly converted to a pointer to a base class
type, or
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*).
4) the pointer is used as the operand of a dynamic_cast.
Example:
struct B
{
virtual void f();
void mutate();
virtual ~B();
};
struct D1: B { void f();};
struct D2: B { void f();};
void B::mutate() {
new (this) D2; //reuse storage - ends the lifetime of *this
f(); //undefined behavoir
... = this; //OK. this points to valid memory
}
void g() {
void* p = malloc(sizeof(D1) + sizeof(D2));
B* pb = new (p) D1;
pb->mutate();
&pb; //OK: pb points to valid memory
void* q = pb; //OK: pb points to valid memory
pb->f(); //undefined behavior, lifetime of *pb has ended
}"
(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.)
I hope this to be clear enough. As now, I myself, can understand
everything about that troublesome programme (which was quite handsome
as well)!
Regards,
--Hossein
--
__________________________________________________________
Sign-up for your own FREE Personalized E-mail at Mail.com
http://www.mail.com/?sr=signup
CareerBuilder.com has over 400,000 jobs. Be smarter about your job search
http://corp.mail.com/careers
|
|
From: Ioannis V. <iv...@em...> - 2003-07-10 20:41:37
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Hossein Haeri > Sent: Thursday, July 10, 2003 9:34 PM > To: dev...@li... > Subject: RE: [Dev-C++] Constructor and Destructor > >As now, I myself, can understand > everything about that troublesome programme (which was quite handsome > as well)! At the initial posting the poster were deleting an object upon its construction. That meant that apparently she did not know what delete does and perhaps what is a constructor. Is there a better advice than suggesting her read a good C++ book? :-) Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
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
|
|
From: Hossein H. <ult...@em...> - 2003-07-11 10:43:54
|
Dear Ioannis, > At the initial posting the poster were deleting an object upon its > construction. That meant that apparently she did not know what delete does > and perhaps what is a constructor. Is there a better advice than suggesting > her read a good C++ book? :-) Hmmm... You are right in the sense that she needs such a book, but that is not the whole story! For us as experienced C++ programmers (hope so!), it was a good topic, regardless of what had caused it. Wasn't it? I myself, had quite interesting struggle to find out what was going on! Wishes, --Hossein -- __________________________________________________________ Sign-up for your own FREE Personalized E-mail at Mail.com http://www.mail.com/?sr=signup CareerBuilder.com has over 400,000 jobs. Be smarter about your job search http://corp.mail.com/careers |
|
From: Hossein H. <ult...@em...> - 2003-07-11 10:51:47
|
Dear Ioannis, > 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. Provided that lifetime of the pointing object is ended. Consider the following line of citation I had made from the Standard: > > pb->f(); //undefined behavior, lifetime of *pb has ended Let me know if I haven't still got the point you had meant. Regards, --Hossein -- __________________________________________________________ Sign-up for your own FREE Personalized E-mail at Mail.com http://www.mail.com/?sr=signup CareerBuilder.com has over 400,000 jobs. Be smarter about your job search http://corp.mail.com/careers |
|
From: Ioannis V. <iv...@em...> - 2003-07-11 13:45:39
|
> -----Original Message----- > From: Hossein Haeri [mailto:ult...@em...] > Sent: Friday, July 11, 2003 1:51 PM > To: Ioannis Vranos > Cc: dev...@li... > Subject: RE: [Dev-C++] Constructor and Destructor > > > Provided that lifetime of the pointing object is ended. Consider the > following line of citation I had made from the Standard: > > > > pb->f(); //undefined behavior, lifetime of *pb has ended > > Let me know if I haven't still got the point you had meant. Yes, in our case there has never been an object. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |