LinuxQuestions.org
Review your favorite Linux distribution.
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 01-22-2006, 04:11 PM   #1
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
question about 'new' and 'delete'


this is a very basic tester .cpp file to test my class heirarchy i made.
FlightBooking is the parent and is abstract.. FirstClass and Coach both extend the parent.

i dont think the functions and members of my custom classes need to be discussed to answer my question.

here is the tester:
Code:
int main()
{
 	FlightBooking * f;

 	f = new FirstClass(12);
 	f->display();

        delete f;

 	f = new Coach(54);
 	f->display();

 	delete f;

	return 0;
}
notice i have 2 'detete' keywords.
delete is used to free up memory allocated with the 'new' keyword (which returns a pointer to the location in memory which is allocated), correct?

NOTE: i have been reading docs online as i am typing, and i think i answered my own question, but ill continue as i was going to..

initially, i thought it actually would delete the variable 'f'.. and was puzzled why, after i deleted the variable, i could still use it in the last few lines.

im guess that what happens when the 'delete f' line is executed, is: the OS frees up that spot in memory.. then assigns NULL to f?

i guess i feel pretty comfident about that now; and my post is more asking for reassurance than the full answer.. sorry, and thanks

Last edited by nadroj; 01-22-2006 at 04:13 PM.
 
Old 01-22-2006, 04:23 PM   #2
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
well, u r pretty right

Code:
FlightBooking * f;
will just allocate an entry in "variables table" (the symbole table), of kind pointer-to-FlightBooking

now
Code:
f = new FirstClass(12);
well do 3 things 1. create new object of type FirstClass (ie allocate mem)
2.call the constructor (12) 3. return a pointer (mem address) to this newly created object and assign it to f

Code:
delete f
will call the destructor to free up the mem used by object FirstClass(12), and assign unknown value to f the certain value (ie mem address) assigned to "f" now is compiler-dependent

mustafa
 
Old 01-22-2006, 04:39 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
To clarify (you actually pretty much answered your own question):

1. There's a big difference between an "object reference" (FlightBooking * f) and the object itself (the instance of "FlightBooking" allocated from the heap when you do "f = new FlightBooking").

2. Yes, "delete" de-allocates the object you created with "new".

3. Yes, calling "delete" automatically triggers the "destructor" (just as calling "new" triggered the "constructor").

4. No, calling "delete" does NOT set your pointer to NULL.

Frankly, I believe it's good practice to habitually set your variables to NULL
Yeah, you add a few bytes of code, and it costs a few cycles at runtime.

But I honestly believe that explicitly setting your pointers to NULL is VERY inexpensive
insurance to protect yourself against potentially VERY expensive problems. IMHO.

5. And yes, you can re-use a variable with new objects as often as you wish.

But ALWAYS remember to "delete", first!

Otherwise you've just created a "memory leak"!

Your .. PSM
 
Old 01-22-2006, 04:45 PM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
-ok so when you call delete, it first calls the object's destructor, so the class can handle specialized tasks to destruct the object (ie deallocate more 'new' memory used, etc)

-after delete is called, and the destructor's code is finished executing.. the variable (f in my example) stays completely intact and available... however the VALUE in it is some random value? i thought it would have been null, but what is it? some random memory address? (edit: actually, this was answered in the 1st reply, so nevermind and thanks!)

also, paul, when you were talking about setting them all to NULL.. you meant just my 1st line of code, to assign FlightBooking * f = NULL, right? i usually do that but didnt really bother in this case, for whatever reason.

ok, thanks alot Elsheikhmh and Paul

Last edited by nadroj; 01-22-2006 at 04:48 PM.
 
Old 01-22-2006, 07:44 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
"yes" to all questions - you are absolutely correct.
 
Old 01-22-2006, 10:02 PM   #6
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 52
if i might add a bit just to clarify.

a variable declared in scope exists in the stack frame and is cleaned automatically when it goes out of scope.
that would be your FlightBooking * f;

what you have done is have your pointer points to an address on the heap that does not go out of scope and persists in the heap until you do a delete on it. This is the advantage of heap. it's kind of global. But protected cause only within your function do you have access to the pointer to your data ie FlightBooking * f;.

so some of the power of using the heap comes like if in your class constructor you use new to make a member on the heap so it is available to anyone who makes a pointer to a FirstClass object.
class FirstClass {
public:
FirstClass();
int get_num_seats() const {return *num_seats};}
void set_num_seats(int seats) { *num_seats = seats; }
private:
int *num_seats;
FirstClass::FirstClass() {
num_seats = new int(15);
}

now you can do
FlightBooking * f = new FirstClass();
and have access to reading and writing a persistent variable num_seats that you know is safe from external manipulation through f->get_num_seats() or whatever.
 
Old 01-22-2006, 10:24 PM   #7
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
ok thanks for the input

however.. that confused me more than helped!
could be the lack of sleep though
 
  


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
Cron Question - Delete files in a dir older than 48 hours? t3___ Linux - Newbie 5 10-11-2011 03:42 AM
find and delete files - stupid question though smshuja Linux - Newbie 5 12-29-2005 01:21 AM
rsync --delete question Red Squirrel Linux - Software 2 10-10-2005 09:55 PM
How to delete non-empty directories? Also have a terminal question Nylex Linux - General 10 07-15-2005 11:51 PM
newbie question: How to delete a user? preswang Linux - Newbie 3 10-29-2003 07:13 PM

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

All times are GMT -5. The time now is 11:58 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