generated html version of blog/beyond_libc.txtHOME

2021 92nd day.

In this .txt I'll be outlining some ways that C code can be rid of the 
obvious and somewhat frustrating limitations of the C standard library.
Assuming for a that for whatever reason, the option of using a different 
language is out of consideration- proponents of Rust, C++, D, and of course 
such perennial contenders as Fortran and the BASICs may have their points,
but here we use C.

The first major limitations of the C standard library is a lack of many 
functionalities that are incredibly basic. No data structures are directly
supported besides a flat array sorted with quicksort and searched with
a binary search. No strings are supported besides the embarrassingly bad
ASCIZ representation often called a "C string". No way to open directories,
see the properties of files, et cetera.

For these limitations, two libraries that may be useful are the apk - 
Apache Portable Runtime and the GLib library used by gtk. Each of these 
augment the C standard library in various ways, but I think GLib is more 
copious in features, and it is the best candidate that I currently know about.
As well, it is better documented. Most other contenders of "portable runtime 
libraries" are written in and for C++, so they are out of consideration here.

Another large limitation of the C standard library is memory handling. Many 
people believe for whatever reason that C code must always employ manual
memory management, without the use of RAII, reference counting or garbage 
collection. This is not only untrue, but it is historically nonsensical, as 
these technologies were invented to reduce the burden on programmers of 
programs that originally employed manual memory management.

RAII is a weird acronym from the C++ world that ostensibly stands for 
"resource acquisition is initialization." What it actually refers to however
is the coupling of resources like heap memory and file descriptors to the 
lifetimes of stack-allocated objects. Implementing this concept in C requires
the use of macros or the __cleanup__ attribute which of course limits the
compilers you can use.

Reference counting on the otherhand is easier to employ, and examples of how
to do so can be written in less code than there is text in this article.
But it isn't automatic, because you can't override the = operator like with
a C++ "smart pointer" class. Nevertheless, reference counting is a good way
to trade a small amount of speed for a large improvement in correctness.

The most promising approach for simplifying C memory handling, in my opinion,
is garbage collection. Many examples exist, most notably the Boehm GC 
made by Hans Boehm, Alan Demers and Mark Weiser. That garbage collector is
used in many fairly prominent projects written in C or C++, and the use of
garbage collection should be considered a totally legitimate alternative
to laboriously ensuring the correctness of memory handling.

Soon I'll be writing another blog post outlining how to write code without
even the use of the C standard library, running directly on the Linux Kernel 
ABI. That would be part of this post, but I realized that topic is the 
diametric opposite of this one.

Have a good one.

-- Oren Watson.