Re: Garbage collection in C++
On 2008-11-16 11:17, George Kettleborough wrote:
Yes, vectors (and other containers) generally needs dynamic allocations
since you do not know how much memory they will need when they are
created (and you might not have enough space on the stack). But for
these kinds of things memory management is easy since you know when you
need to free the memory (when the container dies). The problem is when
you need to create an object in one place and then pass on ownership to
somewhere else (which in turn might pass on ownership).
>>
>What does garbage collection have to do with the finally block?
>For that matter, at the level garbage collection works, what is
>the difference between a finally block and the destructor of a
>local object?
>
Well, if you are using new in a try block, and an exception is thrown,
C++ will guarantee the destruction of the pointer and anything else on
the stack but it won't call delete for what you allocated there. I
thought the finally block was to "clean up" any resources you allocated
in the try block, something that isn't necessary if you don't
dynamically allocate resources in there (but instead in the constructors
of objects create there, which are guaranteed to be destroyed).
For those kinds of situations you can use auto_ptr or some other smart
pointer which will take care of freeing the allocated memory when they
go out of scope (such as when an exception is thrown). What you can not
do without finally is to free other resources, such as opened files,
unless you build a smart fstream or use guard-classes (can't remember if
that is the correct term). Of course, if the fstream is on the stack you
do not have to worry.
--
Erik Wikström
On 2008-11-16 11:17, George Kettleborough wrote:
On 16/11/08 09:43, James Kanze wrote:
>
How would you implement something like a vector without dynamically
allocated memory? I thought dynamically allocated memory was used
because the amount of memory needed can only be decided at run time.
>On Nov 16, 8:38 am, George Kettleborough
><g.kettleboro. ..@member.fsf.o rgwrote:
>>
>>
>>
>It rarely makes sense to allocate memory in constructors and
>deallocate in destructors. If that's what you're doing, it's
>generally preferable to use an object directly, with no dynamic
>allocation. There are exceptions, of course, but they're a lot
>less frequent than people make out. The main motivation for
>using dynamic allocation is precisely because the lifetime of
>the object must be arbitrary, and doesn't fit any pre-defined
>pattern.
><g.kettleboro. ..@member.fsf.o rgwrote:
>>On 15/11/08 15:57, pushpakul...@gm ail.com wrote:
>>>Is garbage collection possible in C++. It doesn't come as
>>>part of language support. Is there any specific reason for
>>>the same due to the way the language is designed. Or it is
>>>discourage d due to some specific reason. If someone can give
>>>inputs on the same, it will be of great help.
>>>part of language support. Is there any specific reason for
>>>the same due to the way the language is designed. Or it is
>>>discourage d due to some specific reason. If someone can give
>>>inputs on the same, it will be of great help.
>>A related question if I may: Is garbage collection necessary
>>if the programmer follows a strict RAII design ie. only
>>allocating and deallocating resources in constructors and
>>destructors ?
>>if the programmer follows a strict RAII design ie. only
>>allocating and deallocating resources in constructors and
>>destructors ?
>It rarely makes sense to allocate memory in constructors and
>deallocate in destructors. If that's what you're doing, it's
>generally preferable to use an object directly, with no dynamic
>allocation. There are exceptions, of course, but they're a lot
>less frequent than people make out. The main motivation for
>using dynamic allocation is precisely because the lifetime of
>the object must be arbitrary, and doesn't fit any pre-defined
>pattern.
How would you implement something like a vector without dynamically
allocated memory? I thought dynamically allocated memory was used
because the amount of memory needed can only be decided at run time.
since you do not know how much memory they will need when they are
created (and you might not have enough space on the stack). But for
these kinds of things memory management is easy since you know when you
need to free the memory (when the container dies). The problem is when
you need to create an object in one place and then pass on ownership to
somewhere else (which in turn might pass on ownership).
>>Java also has the finally block, and I understand this won't
>>be implemented in C++(0x) any time soon because it's not
>>necessary with RAII. Can garbage collection in C++ work to the
>>same extent as it does in Java without the finally block?
>>be implemented in C++(0x) any time soon because it's not
>>necessary with RAII. Can garbage collection in C++ work to the
>>same extent as it does in Java without the finally block?
>What does garbage collection have to do with the finally block?
>For that matter, at the level garbage collection works, what is
>the difference between a finally block and the destructor of a
>local object?
Well, if you are using new in a try block, and an exception is thrown,
C++ will guarantee the destruction of the pointer and anything else on
the stack but it won't call delete for what you allocated there. I
thought the finally block was to "clean up" any resources you allocated
in the try block, something that isn't necessary if you don't
dynamically allocate resources in there (but instead in the constructors
of objects create there, which are guaranteed to be destroyed).
pointer which will take care of freeing the allocated memory when they
go out of scope (such as when an exception is thrown). What you can not
do without finally is to free other resources, such as opened files,
unless you build a smart fstream or use guard-classes (can't remember if
that is the correct term). Of course, if the fstream is on the stack you
do not have to worry.
--
Erik Wikström
Comment