This is one of those rotten dynamic array libraries, but wait, just take a look.
int *a = scary_new(sizeof(int));
Look at this LHS; it looks like an ordinary C array, but
printf("length: %zu\n", scary_length(a)); //=> 0
they know their length by themselves 😱.
scary_push(&a, -1);
scary_push(&a, 42);
printf("new length: %zu\n", scary_length(a)); //=> 2
You can push elements with automatic memory extension, as much as you want.
Moreover, the scary_push
function is generic 😱. If you put this code,
scary_push(&a, 0UL); // Pushing `unsigned long` into an array of `int`!
it will produce a warning by default with modern compilers like GCC 12.
warning: passing argument 1 of 'scary_push_uint64' from incompatible pointer type [-Wincompatible-pointer-types]
scary_push(&a, 0UL);
^~
|
int **
You can of course opt-in an option -Werror
to prevent such typing mistakes.
And you'll see magic here:
int i = a[1];
You can read/write them as ordinary C arrays with zero-overhead 😱😱.
printf("content: %d\n", i);
Then it prints 42
. Happy ending. 🤔🤔
make
and getlibscary.so
.- Write your code with
#include <scary.h>
. - Compile with
-lscary
and proper-I
/-L
. - Enjoy your looseness.
We use Criterion for tests so
you'll need to install that before make test
.
Dear Debian/Ubuntu users: You can install its package via
apt install libcriterion-dev
.
CC0.