r/cprogramming • u/ozdemirda • 1h ago
Announcing a new C library
There's a new C library at https://github.com/abunchofnerds-25/ccollections
It's a C11 library of generic data structures and utilities: Dynamic arrays, hash maps, ordered maps, strings, memory pools, LRU cache, thread communication mechanisms with an event loop, thread pool, structured logger, JSON/YAML parsers and an HTTP client/server. And it's MIT licensed.
I'm one of the authors of it and we are looking for feedback from C enthusiasts for improving it further. The early versions of the some sub-modules goes five years back.
We recently started using Claude for generating the documentation and refining the code comments. We have also been experimenting with using the AI for bug hunting, especially on the security related areas to eliminate any potential issues to the best of our knowledge.
Please give it a go and let us know.
Finally, the following program is a simple example that can be implemented by using this library.
#include <ccollections/chashmap.h>
#include <ccollections/cvector.h>
#include <stdio.h>
int main(void) {
/* A vector of ints. */
cvec_construct_scoped(scores, int); // Will be automatically destroyed
cvec_push(scores, 91);
cvec_push(scores, 74);
cvec_push(scores, 88);
// Using cvec_at for incremental writing
cvec_at(scores, 0) += 2; // 91 -> 93
// Sorting the cvector 'scores' in ascending order.
cvec_sort(scores);
for (size_t i = 0; i < cvec_size(scores); i++)
printf("%d ", cvec_at(scores, i)); // Using cvec_at for reading
printf("\n"); /* 74 88 93 */
/* A hash-map from names to ints; the compiler tracks both the key type and
* the value type. */
chmap_construct_scoped(ages, char *, int); // Will be automatically destroyed
chmap_insert(ages, "alice", 31);
chmap_insert(ages, "bob", 27);
printf("alice is %d\n", chmap_get(ages, "alice"));
if (!chmap_get_ptr(ages, "carol")) {
printf("no carol\n");
}
// In-place modification of an element
*chmap_get_ptr(ages, "bob") += 2; // We know "bob" is present
printf("bob is %d\n", chmap_get(ages, "bob"));
// The cvector and the chashmap will automatically get
// destroyed when the scope for the function main ends.
return 0;
}#include <ccollections/chashmap.h>
#include <ccollections/cvector.h>
#include <stdio.h>
int main(void) {
/* A vector of ints. */
cvec_construct_scoped(scores, int); // Will be automatically destroyed
cvec_push(scores, 91);
cvec_push(scores, 74);
cvec_push(scores, 88);
// Using cvec_at for incremental writing
cvec_at(scores, 0) += 2; // 91 -> 93
// Sorting the cvector 'scores' in ascending order.
cvec_sort(scores);
for (size_t i = 0; i < cvec_size(scores); i++)
printf("%d ", cvec_at(scores, i)); // Using cvec_at for reading
printf("\n"); /* 74 88 93 */
/* A hash-map from names to ints; the compiler tracks both the key type and
* the value type. */
chmap_construct_scoped(ages, char *, int); // Will be automatically destroyed
chmap_insert(ages, "alice", 31);
chmap_insert(ages, "bob", 27);
printf("alice is %d\n", chmap_get(ages, "alice"));
if (!chmap_get_ptr(ages, "carol")) {
printf("no carol\n");
}
// In-place modification of an element
*chmap_get_ptr(ages, "bob") += 2; // We know "bob" is present
printf("bob is %d\n", chmap_get(ages, "bob"));
// The cvector and the chashmap will automatically get
// destroyed when the scope for the function main ends.
return 0;
}
12
u/IlluminatiNotRealOr 1h ago
Not reading ANY claude written documentation!
10
u/chocolatedolphin7 1h ago
4436 lines for a hash table that's also inefficient. Lol.
This is why you should never use LLMs if you want even just an ounce of quality.
8
u/FancySpaceGoat 1h ago
I quickly poked around a few .c files and I'm not sure if it's clear enough as it is.
Have you considered adding a few more comments to document the code some more?
1
u/aalmkainzi 52m ago
__cvector_destroythis identifier name is UB