C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

deleting this

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Abhijeet
Guest





PostPosted: Tue Apr 20, 2004 10:25 pm    Post subject: deleting this Reply with quote



I was just toying around idea of deleting this from a member function.
Was expecting that any acess to member variable or function after
deleting sould give me dump.Cause now I am trying to access freed
memory.

Is my assumption correct?
I tried following code
---------------------------------------------------
#include <iostream.h>
class A
{
public:
A():_i(0){}
A(int i_):_i(i_){}
void killself(){cout<<"deleting this"< test = ((A*)t
his);test=0;_i = 5;}
int getI(){return _i;}
void setI(int i_=0){_i= i_;}
private:
int _i;
};
int main()
{
A a;
a.setI(10);
cout< a.killself();
cout<
A* pa = NULL;
pa = new A(10);
cout< cout<<"pointer value"< pa->killself();
cout< cout<<"pointer value"< delete pa;
pa = NULL;
return 0;
}
------------------------------------------
As I was expecting msvc6 gave dump at statement "a.killself();".But if
I comment this line I get dump at "delete pa;". I am unable to
understand the behaviour.Can somebody help me understand what's going
on?

I think there was thread on similar topic, but I m unable to locate
it.Would even appreciate if get link for same.

Regards,
Abhijeet
Ps. - If I comiple it with "gcc version 3.3.1 (cygming special)" code
runs perfectly fine without any dump. Same is case with Sun workshop
5.6.Which is confusing me more.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Stephen Ludin
Guest





PostPosted: Wed Apr 21, 2004 9:24 am    Post subject: Re: deleting this Reply with quote



Abhijeet wrote:

Quote:
I was just toying around idea of deleting this from a member
function. Was expecting that any acess to member variable or
function after deleting sould give me dump.Cause now I am trying to
access freed memory.

Is my assumption correct?
I tried following code
---------------------------------------------------
#include <iostream.h
class A
{
public:
A():_i(0){}
A(int i_):_i(i_){}
void killself(){cout<<"deleting this"< test = ((A*)t
his);test=0;_i = 5;}
int getI(){return _i;}
void setI(int i_=0){_i= i_;}
private:
int _i;
};
int main()
{
A a;
a.setI(10);
cout< a.killself();
cout<
A* pa = NULL;
pa = new A(10);
cout< cout<<"pointer value"< pa->killself();
cout< cout<<"pointer value"< delete pa;
pa = NULL;
return 0;
}


You are playing around in the world of 'undefined' behavior. Toying
with freed memory will not guarantee a crash or a seg fault - that
just happens to be the likely result.

The first a.killself() tries to delete a variable on the stack. This
will usually crash.

The second killall ( pa->killself() ), with the exception of assigning
to _i is valid, though "bad". Observe:

void killself() {
cout << "deleting this" < delete this;
A* test = ((A*)this); // ok, test points to the now deleted this
test=0; // ok - test points to 0
_i = 5; // very iffy - assigning to free'd memory - might crash
}

Unless the process returned the page containing this to the system via
sbrk(), unmapping virtual memory, or otherwise marking the page as
unwritable, the assignment of 5 to this->_i will usually succeed
without complaint.

Assuming the page remains valid pa->getl() after killself() might
succeed as well.

Now, when you call delete pa after calling pa->killself() we are in a
double free situation. Similar to 'deleting' the stack variable this
is often, though certainly not always caught by the allocator.

Might, maybe, often, not always - these are the results of mishandling
memory. The only guarantee you get is from Murphy's Law which when
applied says it _won't_ crash, at least until your most important
client tries to use it.

Hope that helps,

-stephen


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Heinz Ozwirk
Guest





PostPosted: Wed Apr 21, 2004 2:00 pm    Post subject: Re: deleting this Reply with quote



"Abhijeet" <abhi_10dulkr (AT) indiatimes (DOT) com> schrieb im Newsbeitrag news:f0bf477c.0404200221.2ae7aaa9 (AT) posting (DOT) google.com...
Quote:
I was just toying around idea of deleting this from a member function.
Was expecting that any acess to member variable or function after
deleting sould give me dump.Cause now I am trying to access freed
memory.

Is my assumption correct?
I tried following code
---------------------------------------------------
#include <iostream.h
class A
{
public:
A():_i(0){}
A(int i_):_i(i_){}
void killself(){cout<<"deleting this"< test = ((A*)t
his);test=0;_i = 5;}
int getI(){return _i;}
void setI(int i_=0){_i= i_;}
private:
int _i;
};
int main()
{
A a;
a.setI(10);
cout< a.killself();
cout<
A* pa = NULL;
pa = new A(10);
cout< cout<<"pointer value"< pa->killself();
cout< cout<<"pointer value"< delete pa;
pa = NULL;
return 0;
}
------------------------------------------
As I was expecting msvc6 gave dump at statement "a.killself();".But if
I comment this line I get dump at "delete pa;". I am unable to
understand the behaviour.Can somebody help me understand what's going
on?

You are calling delete pa twice, first in pa->killself() and then
explicitly in main. That is undefined behaviour (or unspecified?).

Quote:
Ps. - If I comiple it with "gcc version 3.3.1 (cygming special)" code
runs perfectly fine without any dump. Same is case with Sun workshop
5.6.Which is confusing me more.

Undefined behaviour includes behaving as expected by some users.

HTH
Heinz

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Apr 21, 2004 3:47 pm    Post subject: Re: deleting this Reply with quote

[email]abhi_10dulkr (AT) indiatimes (DOT) com[/email] (Abhijeet) wrote in message
news:<f0bf477c.0404200221.2ae7aaa9 (AT) posting (DOT) google.com>...

Quote:
I was just toying around idea of deleting this from a member function.
Was expecting that any acess to member variable or function after
deleting sould give me dump. Cause now I am trying to access freed
memory.

It is undefined behavior. It might dump. It might work. It might
reformat your hard disk. My experience is that read-only access will
usually work, unless it is an important demo in front of your principal
customer, and that write access will usually work, but will screw up the
free space arena, causing an almost impossible to find error later.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Michiel Salters
Guest





PostPosted: Wed Apr 21, 2004 3:50 pm    Post subject: Re: deleting this Reply with quote

[email]abhi_10dulkr (AT) indiatimes (DOT) com[/email] (Abhijeet) wrote in message news:<f0bf477c.0404200221.2ae7aaa9 (AT) posting (DOT) google.com>...
Quote:
I was just toying around idea of deleting this from a member function.

Have you read chapter [16] of the FAQ?
The footer contains a link.
Quote:


V


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Richter
Guest





PostPosted: Wed Apr 21, 2004 7:35 pm    Post subject: Re: deleting this Reply with quote

Hi,

Quote:
I was just toying around idea of deleting this from a member function.

Typically, not a good idea except for very special purposes.

Quote:
Was expecting that any acess to member variable or function after
deleting sould give me dump.

C++ does not give you this guarantee. The "this" pointer still points
to something, and *if you're lucky* you can mess with the memory it
points to. Just that you cannot expect that this memory is in any
consistent state. It is likely to be reused for objects newly created.
It may throw, but there's no need for throwing.

Quote:
Is my assumption correct?

No.

Quote:
a.killself();
cout<

Of course a bad idea trying to delete an automatic object. Anything
could happen.

Quote:
A* pa = NULL;
pa = new A(10);
cout< cout<<"pointer value"< pa->killself();
cout< cout<<"pointer value"< delete pa;
pa = NULL;
return 0;

As I was expecting msvc6 gave dump at statement "a.killself();".But if
I comment this line I get dump at "delete pa;".

You're releasing the memory pointed to by "pa" twice. Once in killself,
a second time in the explicit delete call. The memory is obviously not
returned to the Os immediately so you're still holding a pointer to
"valid memory" in pa after a "killself". Just not to a valid A object.

Quote:
Ps. - If I comiple it with "gcc version 3.3.1 (cygming special)" code
runs perfectly fine without any dump. Same is case with Sun workshop
5.6.Which is confusing me more.

It doesn't run "perfectly fine". It may or may not crash. If it doesn't
crash, then you're at best lucky. C++ does not require that this program
must dump.

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Abhijeet
Guest





PostPosted: Fri Apr 23, 2004 12:22 am    Post subject: Re: deleting this Reply with quote

Thank you guys,
Actually was trying to explain same thing to an interviewer.But seems
like she was stuck up on thing that it has to give segmentation
violation.She didn't accept that its a undefined behaviour.
So asked to check if i was heading in some wrong direction.
Thanks & Regards,
Abhijeet


Thomas Richter <thor (AT) cleopatra (DOT) math.tu-berlin.de> wrote

Quote:
Hi,

I was just toying around idea of deleting this from a member function.

Typically, not a good idea except for very special purposes.

Was expecting that any acess to member variable or function after
deleting sould give me dump.

C++ does not give you this guarantee. The "this" pointer still points
to something, and *if you're lucky* you can mess with the memory it
points to. Just that you cannot expect that this memory is in any
consistent state. It is likely to be reused for objects newly created.
It may throw, but there's no need for throwing.

Is my assumption correct?

No.

a.killself();
cout<
Of course a bad idea trying to delete an automatic object. Anything
could happen.

A* pa = NULL;
pa = new A(10);
cout< cout<<"pointer value"< pa->killself();
cout< cout<<"pointer value"< delete pa;
pa = NULL;
return 0;

As I was expecting msvc6 gave dump at statement "a.killself();".But if
I comment this line I get dump at "delete pa;".

You're releasing the memory pointed to by "pa" twice. Once in killself,
a second time in the explicit delete call. The memory is obviously not
returned to the Os immediately so you're still holding a pointer to
"valid memory" in pa after a "killself". Just not to a valid A object.

Ps. - If I comiple it with "gcc version 3.3.1 (cygming special)" code
runs perfectly fine without any dump. Same is case with Sun workshop
5.6.Which is confusing me more.

It doesn't run "perfectly fine". It may or may not crash. If it doesn't
crash, then you're at best lucky. C++ does not require that this program
must dump.

So long,
Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.