RE: [Dev-C++] Constructor and Destructor
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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.
>
|