Re: OT:C/C++ Opinion Poll




"Darin Johnson" <darin@xxxxxxx> wrote in message
news:493753a3-c4f4-4a2d-97a2-b915f9c69119@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Nov 30, 3:40 pm, CBFalconer <cbfalco...@xxxxxxxxx> wrote:
I.e. programmer action is always needed. This means that the only
advantage to GC is that you set the pointer to NULL, rather than
passing it to free().

The word "only" is a bit too strong. Maybe it seems that
way with Java or similar things. But the original point of
GC was to first remove the memory allocation/deallocation
abstraction entirely in higher level languages. There is not
even any assignment to null needed to cause something
to be collected, just stop referring to the object (assign
something else to the variable, return from the routine that
was referring to it, etc). Languages like Smalltalk and
Lisp don't even have pointers, and some functional
languages with GC don't even have assignment...

Removing references is what was meant. This is done differently in
various languages. In imperative languages you'd need to assign
NULL to an object reference in order to remove it, in Lisp you'd need
to unlink a cell from a list etc.

If programmer action is required, then either the GC
is broken (ie, another attempt to use reference counting)
or the language well suited for GC. If an object is
unused, then a good GC will be able to collect it.

A GC can only collect objects that are not referenced, not objects that
are referenced but never used again. In order to collect these objects
you have to explicitly remove references to them. Once we're talking
about complicated datastructures with many references, you effectively
have to do all collection manually.

In other words, a GC can only automatically collect a subset of the calls
to free. Even replacing all calls to free with a null pointer assignment
doesn't make a GC as powerful as malloc/free...

Second, GC can give a performance boost. Most good
garbage collectors need to move memory and do this
in a way that also happens to locality of reference. That
is caching and swapping are more efficient. With
generational scavangers there can be less time CPU
spent doing GC over the life of a program than would
be spent in explicit alloc/free. Fragmentation is
eliminated as well.

A moving generational GC is indeed a lot better than the mark&sweep
style (which effectively uses malloc) but has higher runtime overhead. For
example every pointer assignment has to update the state of the object, and
compiler optimization is often severely constrained. GC's typically have a
higher per-object overhead than malloc, and need more memory to allow
for less frequent collections, so I'd find it hard to believe you get significant
locality benefits.

The snags though are that the run time system for a good GC
system requires virtual memory support, and of course a
language with a high level abstraction. C++ is a low level
language, so GC doesn't really fit (though some people have
tried to retrofit GC which seems a bit misguided I think).

Agreed. If you do use GC, you need to design the whole system (language,
runtime, OS integration) around it, or it'll be far from optimal.

Wilco


.



Relevant Pages

  • Re: memory allocation questions (newbie)
    ... I think this means something like "a pointer that is ... prepared to point to a memory location big enough to hold an int." ... I think having multiple exit points is not a good idea. ... experience writing programs in high-level languages. ...
    (comp.lang.c)
  • Re: C vs C++ in Embedded Systems?
    ... >>the garbage collection system be able to find every pointer that it needs ... >>ensuring that all pointer variables actually point to allocated memory. ... references, by definition, in these languages. ...
    (comp.arch.embedded)
  • Re: =& when creating new object
    ... $reference point to the same memory location. ... Please read the chapter called "references are not pointers" in the ... So there is no explicit pointer variable as there is in C. So what? ... not consider it an address in PHP. ...
    (comp.lang.php)
  • Re: C vs C++ in Embedded Systems?
    ... >>systems (for languages that don't mind having things move around at run ... the garbage collection system be able to find every pointer that it needs ... ensuring that all pointer variables actually point to allocated memory. ... True, that's why those systems make do with static allocations, and ...
    (comp.arch.embedded)
  • Re: Garbage collection
    ... My bet is that it will incorrectly free pieces of allocated memory ... files for pointer references. ... storing a couple of flag bits in the low-order "necessarily ...
    (comp.lang.c)