Anything that is allocated with a
new classtype[] should be deleted with a delete[].
A pointer which is allocated with a "new classtype[]" actually points
to an array in the memory( heap ). Thus a delete[] is necessory to
deallocated the allocated mem. A "delete" on the contrary might
deallocate only one sizeof( classtype ) bytes from the memory.
We can have a,
class Microsoft *BG;
BG = new Microsoft;
delete BG; // This is Valid , and its dead !!!
This is also valid, a *x points to only one block on the heap with
size = sizeof( A )
Note : when a new[] is done, the amount of mem blocks allocated are
internally maintained by C++ so that delete[], deallocates the right no
of blocks.
vive,
Prasanna
>From [EMAIL PROTECTED] Wed Feb 3 22:34:20 1999
>Received: from computer.net ([EMAIL PROTECTED] [127.0.0.1])
> by landreau.ruffe.edu (8.8.7/8.8.7) with ESMTP id RAA08124;
> Wed, 3 Feb 1999 17:26:45 -0500
>Sender: [EMAIL PROTECTED]
>Message-ID: <>
>Date: Wed, 03 Feb 1999 22:26:42 +0000
>From: holotko <[EMAIL PROTECTED]>
>Reply-To: holotko <[EMAIL PROTECTED]>
>Organization: The Ruffe' School
>X-Mailer: Mozilla 4.5 [en] (X11; U; Linux 2.0.32 i586)
>X-Accept-Language: en
>MIME-Version: 1.0
>To: Prasanna Bose <[EMAIL PROTECTED]>
>CC: "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>,
> Linux C Programming <[EMAIL PROTECTED]>
>Subject: Re: C++ Destructor Question
>References: <>
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit
>
>Prasanna Bose wrote:
>>
>> Actually the destructor should be
>> ~Foo()
>> { delete[] word; } // as an array has been allocated.
>>
>> vive,
>> Prasanna subash
>>
>
>Actually, although I didn'tshow it in the example but I defined the
>variable "word" at the beginning of the class as:
>
> private:
>
> char *word;
>As a pointer.
>Thus, when the allocated memory is killed can'tit be done as :
>
> delete word;
>Or is it still required that it be done in array format as:
> delete[] word; ???
>
>
>
>/John <[EMAIL PROTECTED]>
>
>
>> >Hi,
>> >The destructor is automatically called when the
>> >object in question is no longer needed. Such as
>> >an object is created inside a function locally and
>> >when the function returns the object gets killed with
>> >the destructor executed. Hence we dont call the
>> >destructor explicitly.
>> >
>> >And in java I feel the garbage collection concept is
>> >implemented quite strong and rarely do we use explicit
>> >deallocation.
>> >
>> >Cheers,
>> >Suresh
>> >Wipro-Nortel Networks
>> >Bangalore
>> >>
>> >>
>> >>Being spoiled by Java's garbage collector leads me to this quick
>> >>question again concerning constructors in C++.
>> >>
>> >>If I allocate memory via "new" using a constructor
>> >>
>> >>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?
>> >>
>> >>Even in Java there are times when it is up to you to destroy an
object
>> >>and/or free memory used for that object, depending on how the
object
>> >>is/was created and an method equivalent of a destructor is
required...
>> >>The garbage collector is not always adequate.
>> >>
>> >>Thanks...
>> >>
>> >>Sincerely,
>> >>
>> >>/John <[EMAIL PROTECTED]>
>> >>
>> >>
>> >>--
>> >>email: [EMAIL PROTECTED]
>> >>Local mailserver <landreau.ruffe.edu> , remote <ns.computer.net>
>> >>
>> >>There is a great alternative to war, it's called Peace.
>> >>
>> >
>> >
>> >--
>>
>> ______________________________________________________
>> Get Your Private, Free Email at http://www.hotmail.com
>
>--
>email: [EMAIL PROTECTED]
>Local mailserver <landreau.ruffe.edu> , remote <ns.computer.net>
>
>"I don't like dictatorship of the rich" - Alan Ginsberg.
>
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com