3 ms·
> (there is an infamous macro in the linux kernel that is so cleverly hacky that it is on another level) Probably that says more about the particular programme
by jstimpfle 4y ago
> (there is an infamous macro in the linux kernel that is so cleverly hacky that it is on another level)
Probably that says more about the particular programmer than it says about macros. Also on a Linux kernel scale some tradeoffs are different.
> And all that for nothing, as the language is incapable of expressing even such a basic thing as a generic array/vector-like data structure
Something you often do not do when programming in C. You do not want "vector", reallocating by a constant factor without second thoughts, for "serious" systems programming. Same goes for much of the other stuff that is in STL for example.
And you can in fact design macros in a few lines that allow you to do something like
DECLARE_CHUNKLIST(My_Foo_Type) foo_chunklist;
CHUNKLIST_PUSH(&foo_chunklist) = make_foo();
I.e. In fact no terrible noise involved. The hack employed here is
#define DECLARE_CHUNKLIST(type) union { type *_type; struct { /* chunklist implementation ... */ } _impl; }
which allows the list to carry its own type to support operations like CHUNKLIST_PUSH. For good ergonomics this relies on typeof().
But again, there is a lot of "serious" programming that gets by just fine without any of such hacks. Sometimes you're programming an event handling system where you mostle use fixed-size but dynamically-allocated buffers and push those around. There is very little generic code used here, the functions you write are dealing with concrete flat buffers mostly.