Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tadd committed Oct 28, 2024
1 parent e768734 commit 07a782f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,29 @@ 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_ 😱, so this code

```c
scary_push(&a, 0UL); // Pushing `unsigned long` into an array of `int`!
```
can produce a warning by default with modern compilers like GCC 12.
<pre><code><b>warning:</b> passing argument 1 of ‘scary_push_uint64’ from incompatible pointer type [-Wincompatible-pointer-types]
scary_push(<b>&a</b>, 0UL);
<b>^~</b>
<b>|</b>
<b>int **</b>
</code></pre>
You can of course opt-in an option `-Werror` to prevent such typing mistakes.
And you'll see **magic** here:
```c
int i = a[1];
```
You can read/write them as an ordinary C array without any overhead 😱😱.
You can read/write them as ordinary C arrays without any overhead 😱😱.

```c
printf("content: %d\n", i);
Expand Down
6 changes: 6 additions & 0 deletions testlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Test(libscary, push_and_length) {
Test(libscary, push) {
int *a = scary_new(sizeof(int));
scary_push(&a, 42);
scary_push(&a, 0UL);
cr_expect(eq(sz, 1, scary_length(a)));

scary_free(a);

char *b = scary_new(sizeof(char));
Expand All @@ -32,6 +34,10 @@ Test(libscary, push) {
cr_expect(eq(sz, 2, scary_length(b)));
cr_expect(eq(chr, 'a', b[0]));
cr_expect(eq(chr, 'b', b[1]));




scary_free(b);
}

Expand Down

0 comments on commit 07a782f

Please sign in to comment.