TacoB0t TacoB0t - 12 days ago 13
C Question

Why does C not require a garbage collector?

My understanding of this has come down to C's origins as a "portable assembler" and the option of less overhead. Is thiat all there is to it?


First of all, lets be clear about what garbage is.

The Java definition of garbage is objects that are no longer reachable. The precise meaning of reachable is a bit abstruse, but a practical definition is that if you can get to an object by following references (pointers) from well known places like thread stacks or static variables, then it may be reachable. (In practice, some imprecision is OK, so long as objects that are reachable don't get deleted.)

You could try to apply the same definition to C and C++. An object is garbage if it cannot be reached.

However, the practical problem with this definition ... and garbage collection ... in C or C++ is whether a "pointer like" value is actually a valid pointer. For instance, an uninitialized variable can contain a random value that looks like a pointer to an object. Or, when a union type that overlays a pointer with an long, a garbage collector cannot be sure whether the union contains one or the other ... or both.

However, it is clear that a C program can call malloc, forget to call free, and then forget the address of the heap node. That node is garbage.

There are two reasons why C / C++ doesn't have garbage collection.

  • It is "culturally inappropriate". The culture of these languages is to leave storage management to the programmer.

  • It would be technically difficult (and expensive) to implement a precise garbage collector for C / C++. Indeed, doing this would involve things that made the language implementation slow.

  • Imprecise (i.e. conservative) garbage collectors are practical, but they have performance and (I have heard) reliability issues. (For instance, a conservative collector cannot move non-garbage objects.)

It would be simpler if the implementer (of a C / C++ garbage collector) could assume that the programmer only wrote code that strictly conformed to the C / C++ specs. But they don't.

But your answer seems to be, why did they design C like that?

Questions like that can only be answered authoritatively by the designers (in this case, the late Dennis Ritchie) or their writings.

As you point out in the question, C was designed to be simple and "close to the hardware".

However, C was designed in the early 1970's. In those days programming languages which required a garbage collector were rare, and GC techniques were not as advanced as they are now.

And even now, it is still a fact that garbage collected languages (like Java) are not suitable for applications that require predictable "real-time" performance.

In short, I suspect that the designers were of the view that garbage collection would make the language impractical for its intended purpose.