Declare a [tt]static union[/tt] (i.e., a union without external linkage) called "header".
A union is a datatype that can hold
either of the declared datatypes. That means, while...
...can hold an [tt]int[/tt]
and a [tt]float[/tt], ...
...can hold an [tt]int[/tt]
or a [tt]float[/tt], depending on whether you last wrote to [tt]foo.x[/tt] or [tt]foo.f[/tt].
Code: Select all
struct {
union header *link;
unsigned size;
} s;
"s" is the one possible content of an "header union", with "s" consisting of a "pointer to header union" named "link" (i.e., pointer to next "header" most likely), and an unsigned integer named "size".
Code: Select all
union align {
double d; unsigned u; void (*f)(void);
} x;
"x" is the
other possible content of an "header union", being a union itself holding
either:
- a double,
- an unsigned int,
- a pointer to a function accepting no arguments and not returning a value.
Code: Select all
} freelist = { &freelist, 0 }, *freep = &freelist;
One object of type "header union" is declared, named "freelist", with [tt]freelist.s.link[/tt] initialized with a pointer to "freelist" itself, and [tt]freelist.s.size[/tt] initialized to zero.
Afterwards, a "pointer to header union" is declared, named "freep", and initialized to point to the [tt]freelist[/tt] object we just initialized.
And while I'm at it, code like this - with lots and lots of unions, implicit initialization and comma operators, but lacking liberal comments - does ring every alarm bell in my head, for it seems someone
really enjoyed his l33t sk1llz at writing l33t code.