LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-21-2004, 02:55 AM   #1
tushar123
LQ Newbie
 
Registered: Dec 2003
Location: mumbai
Distribution: shivatusahr
Posts: 14

Rep: Reputation: 0
C++ Magic Program.


I have written the following program
==========================
class derived
{
public:
int i;
void derived::f()
{
cout << "f is exe"<<endl;
}

derived()
{
cout << "hi"<<endl ;
}

~derived()
{
cout << "destroy me " << endl;
}

};

int main()
{

derived *pDerived1 = new derived ;
cout << pDerived1<<endl;
pDerived1 -> i =5;
cout << pDerived1 ->i <<endl;
delete pDerived1;
cout << pDerived1<<endl;
cout << pDerived1 ->i <<endl;
pDerived1 = NULL;
cout << pDerived1<<endl;
pDerived1->f();
cout << pDerived1<<endl;
return 0;
}

===============
output of program: Platform CC /Solaris
===============
hi
63380
5
destroy me
63380
5
0
f is exe
0
=================
My Questions:
=================
Why after deleting It is showing the same memory location ?
and how it is possible to access the method of the class after delete ?
I have explicitly assigned pDerived1 = NULL then also
class method f() is accessible how it is working ?

Please give me details.
 
Old 04-21-2004, 04:49 AM   #2
cjp
Member
 
Registered: Dec 2003
Location: the Netherlands
Distribution: SuSE 9.0
Posts: 54

Rep: Reputation: 15
I think that this is not official C++ behaviour, so you should not depend on it. This is what happens:

new allocates some memory for a new object, and returns a pointer to that memory. Delete de-allocates that memory, but the pointer is still pointing to the (de-allocated) memory location. Using that pointer results in undefined behaviour. In your case it just works, but you might get a segmentation fault on other platforms.

What happens if you call a method of an object, is that some executable code is executed, like a normal C function, and a pointer to the object itself is given to the nethod as an argument with the name "this". Your method f doesn't use this, or any member variables of the class, so in this case it doesn't matter for f() that the this-pointer points to de-allocated memory.
 
  


Reply



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
iPod + Magic Soulful93 Slackware 3 05-04-2005 02:27 PM
Partition Magic TerminalPhreak Linux - Newbie 13 03-03-2003 07:27 AM
Partition Magic-like program for Linux? hstang Linux - General 1 02-18-2003 01:56 PM
partition magic virtualking Linux - General 4 10-14-2002 12:35 PM
Magic HD...now you see me....now you don't!!! recurrente Linux - General 3 03-17-2002 12:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:42 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
Open Source Consulting | Domain Registration