Garbage Collection in C

R

Roland Pibinger

Well, it will not make your coffee anyway :)

For C the heap is a scarce resource like any other resource. Heap
memory has to be acquired and released. For a GC language the heap
(conceptually) is an infinite space. That's the difference. I prefer
the C point of view.

Best wishes,
Roland Pibinger
 
S

sjdevnull

Roland said:
For C the heap is a scarce resource like any other resource. Heap
memory has to be acquired and released. For a GC language the heap
(conceptually) is an infinite space.

The last sentence is untrue. Not all the world is mark-sweep or
copy-collect. There are deterministic GCs that give the programmer
guarantees on when memory will be released. Simple reference counting
is the most common. There are also GC systems where a call can be made
to force immediate collection.
 
K

Kenny McCormack

I do not know what heathfield has against the GC.

The following links may be useful - especially the first:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language

Anyway, Jacob, why do you punish yourself so? Why do you go around with
a kick-me sign on your backside? You could state that 2+2 = 4 and
heathfield would say it isn't so. That's just what it is.

And here's another thing (indirectly, from Wikipedia, which, as we know,
heathfield thinks is rubbish), on the subject of creationism (and that
oxymoron of oxymorons "creation science"). Note that there are many
good and deep parallels between creationists and cls regulars (although
they all, of course, deny it). But, the idea is this: some famous
scientish (I believe it was Richard Dawkins) holds that it is silly to
debate creationists for much the same reasons as why you don't debate
with clc regulars, er, I mean, pigs ("They like it" and "you just get
dirty"). But more specifically, Dawkins says that debate is a silly
activity, because the way to win is never to say another positive about
your own position, that is never to espouse a positive view, because if
you do, then you give the other side something to attack. Instead, all
you do it nitpick their position; that's how you score points.

The obvious analogy to heathfield and CLC regulars in general should be
numbingly obvious.

That's why heathfield will never claim to like or support anything
(e.g., valgrind, for which any sensible person would have taken his post
to be one of support, yet when questioned, claimed he never supported
it). Just generally: what a wuss.
 
M

Mark McIntyre

I do not know what heathfield has against the GC.

And I don't know what Navia has against being polite. Last time I
checked, even the French thought it more polite to refer to people by
first name, or failing that with the correct honorific.

Lets move on shall we, quietly reflecting as we do that Richard has
already made it plain he has nothing against GC, merely against rude
and persistent offtopic posters who regard the rules of usenet as
theirs to ignore as they please.

(rest of Navia's stupid post snipped as it was offensive and
gratuitous)
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
C

CBFalconer

jacob said:
.... snip ...

It must be said too that the malloc/free system is slow too, since at
each allocation request malloc must go through the list of free blocks
trying to find a free one. Memory must be consolidated too, to avoid
fragmentation, and a malloc call can become very expensive, depending
on the implementation and the allocation pattern done by the program.

Not necessarily. My nmalloc for DJGPP, which is believed to be
applicable to most Linux systems, is a counter example. Free
operations are always O(1), in that the system always knows whether
or not any adjacent blocks are free. Allocation normally requires
only one probe, although in the worst case it could become as large
as 24 or so probes. nmalloc also has provisions for detecting most
application errors. The code can be found at:

<http://cbfalconer.home.att.net/download/>

nmalloc is written almost completely in standard C. It requires a
single system call (sbrk) and knowledge about alignment
requirements. The debugging provisions for the actual package
(which are normally disabled) also require non-standard system
calls, such as write. The auxiliary user debuggery system is in
standard C. The package is freely available under the same terms
as the DJGPP system.

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 
C

CBFalconer

Richard said:
jacob navia said:


Quite so. Please move discussions of non-C matters to some other
newsgroup where it is topical.

I think this particular carp is unfair. Jacob has been
fundamentally advertising the available of the Boehm library for
GC, which I presume is written in standard C, or nearly so.

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 
R

Richard Heathfield

CBFalconer said:
I think this particular carp is unfair. Jacob has been
fundamentally advertising the available of the Boehm library for
GC, which I presume is written in standard C, or nearly so.

I'm not convinced (having flicked briefly through the source code).
 
K

Keith Thompson

CBFalconer said:
I think this particular carp is unfair. Jacob has been
fundamentally advertising the available of the Boehm library for
GC, which I presume is written in standard C, or nearly so.

I haven't looked at the source, but I doubt that it can be written in
portable standard C. It has to scan memory looking for pointers,
which means making assumptions about how memory is managed (I'm
guessing it only works on systems that use the typical contiguous
hardware stack). I think it also has to examine register contents,
probably using a small amount of assembly language.
 
C

CBFalconer

Keith said:
I haven't looked at the source, but I doubt that it can be written
in portable standard C. It has to scan memory looking for pointers,
which means making assumptions about how memory is managed (I'm
guessing it only works on systems that use the typical contiguous
hardware stack). I think it also has to examine register contents,
probably using a small amount of assembly language.

Well, my nmalloc has similar restrictions, but I feel free to
mention it here. The following is an excerpt from its source,
spelling out the non-standardisms, if you include the use of sbrk
and alignment values. write and STDERR are used only for the
development debuggery. The user debugger via sysquery.h is
standard, in that it makes no added assumptions about memory
alignment.

#include <stddef.h> /* offsetof() */
#include <stdlib.h> /* malloc, free, realloc, exit, EXIT_FAILURE
*/
#include <unistd.h> /* sbrk, write */
#include <signal.h> /* raise, SIGABRT */
#include <string.h> /* strlen, memmove, memcpy */
#include <limits.h> /* CHAR_BIT, INT_MAX */
#include "sysquery.h" /* available user debugger linkages */

/* system dependant magic. Only STDERR used */
enum {STDIN = 0, STDOUT, STDERR, STDAUX, STDPRN}; /* handles */

/* Incorporation into the system should require deleting the
following <nmalloc.h>, changing all references to nmalloc
to malloc, nfree to free, nrealloc to realloc. Change the
single call to fakesbrk to sbrk. Also normally set all
DEBUGx values above to 0 in place of 1. Later many
routines will be made inline. For debugging compilations
are done with "/Dinline= " switch. For production use
the "/DNDEBUG=1" switch, which does all the above except
the inlining. But see NEWMALLDBG above.
*/

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 
J

jacob navia

Mark said:
And I don't know what Navia has against being polite. Last time I
checked, even the French thought it more polite to refer to people by
first name, or failing that with the correct honorific.

heathfield FORBID ME EXPLICIRELY to call him by his first
name. Before I called him "richard" but HE told me not to do so.

I thought of calling him "Your Majesty" or "Your Excellence" but
he felt insulted so now I use heathfield. As far as I know it is
not an insult...

But for clc regulars there is NO WAY to satisfy them of course.
 
R

Richard Heathfield

jacob navia said:
heathfield FORBID ME EXPLICIRELY to call him by his first
name. Before I called him "richard" but HE told me not to do so.

Can you support that claim? I don't remember doing any such thing.
Message-ID please.

I thought of calling him "Your Majesty" or "Your Excellence" but
he felt insulted so now I use heathfield. As far as I know it is
not an insult...

But for clc regulars there is NO WAY to satisfy them of course.

Yes there is. Just stay on topic (and observe common posting conventions).
Other people manage it. Why not you?
 
J

jacob navia

Flash said:
jacob said:
William said:
jacob navia wrote:

[...]

This is of no practical significance. Pointers aren't XORed in normal
programs, and if you stay within the normal alignment requirements
of the processor, everything works without any problems.


No, but I might well subtract 1 from my pointers to change the
indexing.


This is allowed of course. Your pointer will be within the bounds
of the pointed-to object and that object will NOT be reclaimed since
there is (at least) one pointer to somewhere in it.


You missed William's point entirely.

p = malloc(N * sizeof *p) -1; /* I now have an array indexed by 1 */

Non-standard, but people do it. It means that the pointer is no longer
no longer points in to the object, so the GC will free it.
Well, this is NON-STANDARD. As far as C is concerned, a subtracting
one from the start of an object is undefined behavior.

If you do things like that yes, you should not use the GC. You can
however have the same effect by

p = malloc((1+N) * sizeof *p) ; /* I now have an array indexed by 1 */

You never use the zeroth member and you keep out of undefined behavior.

How to you know the library won't do any of the things that break GC?
Such as paging information, including your pointer, out to disk?

Exvuse me but that is likely to be done by the OS, not by a third party
library.
> Or
compressing it? Or subtracting 1 as above to use it as an array indexed
from 1? IIRC this last is possible if it is a library simply
implementing stuff out of Numerical Recipes in C, something which is
quite possible.

Maybe, I can't guarantee all third party libraries in the world. As far
as I know, and from the experience of people using the GC, that never
happens.
 
R

Richard Heathfield

jacob navia said:
Well, this is NON-STANDARD. As far as C is concerned, a subtracting
one from the start of an object is undefined behavior.

Absolutely right. How about that?
 
C

Chris Dollin

Roland said:
For C the heap is a scarce resource like any other resource. Heap
memory has to be acquired and released. For a GC language the heap
(conceptually) is an infinite space.

That's not how /I/ experience it. GC doesn't make your heapspace
infinite: it just takes care of routine deallocation. It /relies/
on programs having relatively bounded heap use.

It does some tedious bookkeeping for you. There's a price -- there's
/always/ a price -- and in some environments you can't afford it,
in others the benefit isn't worth it, and in yet others the GC
implementation isn't mature and/or has visible stress points.

I /like/ GCd languages. I've been using them for more than twenty
years. Having GC available, especially GC known to the language,
makes a /lot/ of techniques become workable idioms, and when
I'm working in C, I miss them.

But if I /really really/ missed them, I'd know I was writing
in the wrong language. Some languages are good for some things,
and some are good for other things. If I were to be given the
godlike power to improve one feature of C, it isn't GC I'd be
wanting to add: it would be properly integrated modularity,
which I think wouldn't have to change the run-time model at all.
 
R

Robert Latest

On Wed, 11 Oct 2006 19:41:47 -0400,
in Msg. said:
I think this particular carp is unfair. Jacob has been
fundamentally advertising the available of the Boehm library for
GC, which I presume is written in standard C, or nearly so.

It doesn't even matter if it is standard C or not. Even discussions of
non-standard libaries or debugging tools are frequentely tolerated on
this forum (as long as the matter is either quickly disposed of, or the
thread moved to an appropriate group) because these things are used and
are thus of interest to all in this newsgroup.

But what Jacob does all the time is, indeed, advertising (not just
discussing) off-topical things on this newsgroup, and this latest thread
was obviously kicked off just to rile regulars in general and RH in
particular. And he's getting flamed down for it, as he deserves (and as
close as RH aver gets to flaming).

robert
 
R

Robert Latest

On Wed, 11 Oct 2006 20:46:40 +0200,
in Msg. said:
There is a feature of the GC that allows to call a "destructor"
function, when an object will be destroyed. I haven't talked
about it because I consider it dangerous,

Oh. For a second there I thought you were going to say, "because I
consider it off-topic".

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top