[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Garbage collection in C++

pushpakulkar

11/15/2008 3:57:00 PM

Hi all,

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 discouraged due to
some specific reason. If someone can give inputs on the same, it will
be of great help.

Regards,
Pushpa
241 Answers

Chris M. Thomasson

11/15/2008 4:32:00 PM

0

<pushpakulkar@gmail.com> wrote in message
news:22b430aa-4990-4dcf-9e2f-9e828427986c@a29g2000pra.googlegroups.com...
> Hi all,
>
> Is garbage collection possible in C++.

Sure:

http://www.hpl.hp.com/personal/Han...




> 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 discouraged due to
> some specific reason. If someone can give inputs on the same, it will
> be of great help.

C++ is a low-level systems language, IMVHO, that's no place for a GC to
be...

Bharath

11/15/2008 5:04:00 PM

0

On Nov 15, 10:32 am, "Chris M. Thomasson" <n...@spam.invalid> wrote:
> <pushpakul...@gmail.com> wrote in message
>
> news:22b430aa-4990-4dcf-9e2f-9e828427986c@a29g2000pra.googlegroups.com...
>
> > Hi all,
>
> > Is garbage collection possible in C++.
>
> Sure:
>
> http://www.hpl.hp.com/personal/Han...
>
> > 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 discouraged due to
> > some specific reason. If someone can give inputs on the same, it will
> > be of great help.
>
> C++ is a low-level systems language, IMVHO, that's no place for a GC to
> be...

Check out: http://www.research.att.com/~bs/bs_faq.html#garbage-...

- bharath

Juha Nieminen

11/15/2008 5:21:00 PM

0

pushpakulkar@gmail.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 discouraged due to
> some specific reason. If someone can give inputs on the same, it will
> be of great help.

I have absolutely no experience in third-party garbage collectors for
C++. What I have always wondered, though, is how those can handle a
situation like:

int* foo()
{
int* table = new int[100];
return table+40;
}

The pointer to the beginning of the allocated array dies when foo()
terminates, but a pointer to one of its elements lives beyond the scope
of foo(). This pointer may be used in the calling code. A garbage
collector would have to:

1) Know that there's still a live pointer pointing to (an element
inside) the array, and thus it cannot destroy it yet.

2) Know to delete the array properly (ie. from the original pointer
pointing to the beginning of the array, rather than the one which lived
longer than that) when that returned pointer dies and the GC runs.

I suppose they have figured out these problems. I'm just wondering how.

Pete Becker

11/15/2008 5:37:00 PM

0

On 2008-11-15 12:21:06 -0500, Juha Nieminen <nospam@thanks.invalid> said:

>
> I have absolutely no experience in third-party garbage collectors for
> C++. What I have always wondered, though, is how those can handle a
> situation like:
>
> int* foo()
> {
> int* table = new int[100];
> return table+40;
> }
>
> The pointer to the beginning of the allocated array dies when foo()
> terminates, but a pointer to one of its elements lives beyond the scope
> of foo(). This pointer may be used in the calling code. A garbage
> collector would have to:
>
> 1) Know that there's still a live pointer pointing to (an element
> inside) the array, and thus it cannot destroy it yet.
>
> 2) Know to delete the array properly (ie. from the original pointer
> pointing to the beginning of the array, rather than the one which lived
> longer than that) when that returned pointer dies and the GC runs.
>
> I suppose they have figured out these problems. I'm just wondering how.

It's just a bit of bookkeeping. When the array is allocated the
collector makes notes about the size of the allocated block and where
it begins. The returned pointer points into the allocated block, so the
block is still live.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Joe Smith

11/15/2008 6:44:00 PM

0


<pushpakulkar@gmail.com> wrote in message
news:22b430aa-4990-4dcf-9e2f-9e828427986c@a29g2000pra.googlegroups.com...
> Hi all,
>
> 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 discouraged due to
> some specific reason. If someone can give inputs on the same, it will
> be of great help.

The C++ standardization commitee is working with some garbage collection
related proposals,
although no official garbage collection mechanism will be included in C++0x.
It is not yet clear
if the auxillary proposal to make it easier to support garbage collection
via third party libraries
will be approved for C++0x or not.

Pete Becker

11/15/2008 7:56:00 PM

0

On 2008-11-15 13:44:20 -0500, "Joe Smith" <unknown_kev_cat@hotmail.com> said:

>
> <pushpakulkar@gmail.com> wrote in message
> news:22b430aa-4990-4dcf-9e2f-9e828427986c@a29g2000pra.googlegroups.com...
Hi
>
>> all,
>>
>> 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 discouraged due to
>> some specific reason. If someone can give inputs on the same, it will
>> be of great help.
>
> The C++ standardization commitee is working with some garbage
> collection related proposals,
> although no official garbage collection mechanism will be included in
> C++0x. It is not yet clear
> if the auxillary proposal to make it easier to support garbage
> collection via third party libraries
> will be approved for C++0x or not.

It's quite clear, since it was approved at the September meeting. <g>
Search for "safely derived pointer" in the current working draft.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Joe Smith

11/15/2008 8:54:00 PM

0


"Pete Becker" <pete@versatilecoding.com> wrote in message
news:2008111514561516807-pete@versatilecodingcom...
> On 2008-11-15 13:44:20 -0500, "Joe Smith" <unknown_kev_cat@hotmail.com>
> said:
>> The C++ standardization commitee is working with some garbage collection
>> related proposals,
>> although no official garbage collection mechanism will be included in
>> C++0x. It is not yet clear
>> if the auxillary proposal to make it easier to support garbage collection
>> via third party libraries
>> will be approved for C++0x or not.
>
> It's quite clear, since it was approved at the September meeting. <g>
> Search for "safely derived pointer" in the current working draft.
>
Roger that, I should have been more clear that it was not clear to me
if the auxillary proposal was included. I'm trying to follow things, but
I am not reading every posted paper, so I may well miss things.

Chris M. Thomasson

11/15/2008 10:22:00 PM

0

"Chris M. Thomasson" <no@spam.invalid> wrote in message
news:7OCTk.31$m74.24@newsfe24.iad...
> <pushpakulkar@gmail.com> wrote in message
> news:22b430aa-4990-4dcf-9e2f-9e828427986c@a29g2000pra.googlegroups.com...
>> Hi all,
>>
>> Is garbage collection possible in C++.
>
> Sure:
>
> http://www.hpl.hp.com/personal/Han...
>
>
>
>
>> 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 discouraged due to
>> some specific reason. If someone can give inputs on the same, it will
>> be of great help.
>
> C++ is a low-level systems language,

C++ is nice because it allows the user to apply just enough of its features
to get the job done. You can use C++ for kernel programming; just not all of
it...

;^D



> IMVHO, that's no place for a GC to be...


James Kanze

11/16/2008 12:19:00 AM

0

On Nov 15, 4:57 pm, pushpakul...@gmail.com wrote:

> Is garbage collection possible in C++.

Yes and no. There are, in fact, a few constructs which could
break it, and there are potential compiler optimizations which
could prevent it from being used.. In practice, the language
constructs are recognized as poor programming practice, and
something to be avoided, regardless, and compilers don't do
optimizations which would break it, and in fact, it is more or
less widely used; see
http://www.hpl.hp.com/personal/Hans....

> 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 discouraged due to some specific reason. If
> someone can give inputs on the same, it will be of great help.

More history than any other reasons. It's readily available,
and works, and is being used by a number of people.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

11/16/2008 12:21:00 AM

0

On Nov 15, 5:32 pm, "Chris M. Thomasson" <n...@spam.invalid> wrote:
> <pushpakul...@gmail.com> wrote in message

> news:22b430aa-4990-4dcf-9e2f-9e828427986c@a29g2000pra.googlegroups.com...

> > Is garbage collection possible in C++.

> Sure:

> http://www.hpl.hp.com/personal/Han...

> > 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 discouraged due to some specific reason.
> > If someone can give inputs on the same, it will be of great
> > help.

> C++ is a low-level systems language, IMVHO, that's no place
> for a GC to be...

C++ is a multi-paradigm language, usable in many contexts. If
you're writing kernel code, a garbage collector certainly has no
place; nor do exceptions, for that matter. And if you're
implementing a garbage collector, obviously, you can't use it.
But for most application programs, it's stupid not to.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34