LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-24-2013, 03:25 AM   #1
Shahid nx
Member
 
Registered: Jan 2012
Posts: 46

Rep: Reputation: Disabled
delete this inside member function?


I have a sample code and output.
Code:
#include <iostream.h>
class Dellete
{
        int c,a,b;
        public:
        Dellete (int arg1,int arg2 ,int arg3)
        {
                a = arg1;
                b = arg2;
                c = arg3;
        }
void PrintNum();

};
int main()
{
        Dellete *Dl =  new Dellete(10,15,14);
        Dl->PrintNum();

}

void Dellete::PrintNum()
{
        cout <<"Line1 a ="<<a<<" b= "<<b<<" c="<<c<<endl;
        a = 25;
        b = 26;
        c = 27;
        cout <<"Line2 a ="<<a<<" b= "<<b<<" c="<<c<<endl;
        delete this;
        cout <<"Line3 a ="<<a<<" b= "<<b<<" c="<<c<<endl;
        a = 28;
        b = 29;
        c = 30;
        cout <<"Line4 a ="<<a<<" b= "<<b<<" c="<<c<<endl;
        delete this;
        cout <<"Line5 a ="<<a<<" b= "<<b<<" c="<<c<<endl;
}
Out Put:
Line1 a =10 b= 15 c=14
Line2 a =25 b= 26 c=27
Line3 a =25 b= 26 c=0
Line4 a =28 b= 29 c=30
Line5 a =28 b= 29 c=138842112


I wondering after "delete this" statement why it is not deallocating object memory . And also i observed only value of variable 'c' (First variable of class)is getting affected after calling "delete this" statement. Can any one tell me the behavior of delete this if we call it inside member function.?

Thanks & Rgrs,
Shahid Nx

Last edited by Shahid nx; 05-24-2013 at 03:27 AM.
 
Old 05-24-2013, 04:49 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,254

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
Deallocating memory does not means that freed memory will be filled with random data. It would be unnecessary time consuming. So data still resides there.

If you use that weird aproach (delete this), then after that you cannot use any of fields or methods from that object - the end.
 
Old 05-24-2013, 06:12 AM   #3
Shahid nx
Member
 
Registered: Jan 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Thanks But When i call delete this i m seeing only the first variable of class (c) is getting filled with garbage data and remaining all other variables are unaffected. Even i tired by rearranging member variables and still found only first variable of class is getting affected. .?
 
Old 05-24-2013, 05:47 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
That is up to the garbage collection. The weird number could easily be a reference to the next block of available memory, OR, it could be that the "Dellete" object got merged with an adjacent block causing some random overwrites.

Whatever, you have created a heap access violation (use after deallocation error). Contents are arbitrary.
 
Old 05-24-2013, 06:55 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
To the best of my understanding, the operation
delete this;
is fundamentally incorrect and cannot be used correctly.

In practice, it is something you often want to do and with modest care, it acts correctly, and I have used it in cases where it is a major pain to avoid using it.

You are mixing that question with the simpler question of accessing member variables of a deleted object. Obviously, that is also totally incorrect. But as eSelix explained, you aren't guaranteed to get garbage when executing incorrect code.

Quote:
Originally Posted by Shahid nx View Post
Thanks But When i call delete this i m seeing only the first variable of class (c) is getting filled with garbage data
Try reading the answer you received before saying "thanks but". You are ignoring what you were just told and asking what was already clearly answered.

Last edited by johnsfine; 05-24-2013 at 06:57 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using an alias inside a function in bash martindl Linux - Newbie 1 06-09-2011 12:55 AM
can a function know its name, inside it? P5music Programming 2 01-04-2011 01:39 PM
[SOLVED] {} inside a function genderbender Programming 2 07-29-2010 10:58 AM
function calls inside a function manas_sem Programming 2 02-28-2007 02:27 AM
function defn inside a function in C...!!! arunka Programming 1 02-05-2006 02:08 AM


All times are GMT -5. The time now is 04:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration