Skip to content
/ libscary Public

A (scary) scalable array library in C

Notifications You must be signed in to change notification settings

tadd/libscary

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libscary: A (scary) scalable array library in C

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. 🤔🤔

Usage

  1. make and get libscary.so.
  2. Write your code with #include <scary.h>.
  3. Compile with -lscary and proper -I/-L.
  4. Enjoy your looseness.

Development

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.

License

CC0.