In C++, when an object(variable as you call it) goes out of scope its'
destructor is automatically called. If the call to delete is in the
destructor then the memory will be freed before the object is completely
unlinked.
John Weldon
-----Original Message-----
From: Joseph Keen [mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 21, 1999 4:26 AM
To: holotko
Cc: Linux C Programming; [EMAIL PROTECTED]
Subject: Re: C++ Destructor Question
In C++ you must always specifically call the destructor before the
variable goes out of scope. When it goes out of scope the variable name
and the memory it points to become unlinked and there is no way for you
to
go back and free that memory. That's how you get memory leaks :)
> i.e.
>
> class Foo
> {
> Foo()
> { word = new char[LENGTH + 1]; }
>
> ~Foo()
> { delete word; }
>
> ...
> }
>
> When I create an object of class Foo memory will be allocated for the
> char buffer "word". Now when the object is no longer needed must I
> make an explicit call to the destructor ~Foo() to destroy the object
> and subsequently call "delete", or, is the destructor somehow called
> automatically when the object is no longer needed,i.e. outside of
> it's scope?