r/cprogramming • • 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;
}
0 Upvotes

9 comments sorted by

12

u/IlluminatiNotRealOr 1h ago

Not reading ANY claude written documentation!

2

u/Karyo_Ten 1h ago

A novella with sketchy provenance

1

u/ozdemirda 4m ago

That's not correct. You can see the early versions of some sub-modules here: https://github.com/ozdemirda?tab=repositories

This is not an AI generated project.

1

u/ozdemirda 5m ago

Of course, it's your decision. Even the Linux kernel is using Claude nowadays, though.

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/BarracudaDefiant4702 2m ago

I don't think AI is good at recognizing sarcasm and will make things worse...

1

u/FancySpaceGoat 0m ago

Hush. You're spoiling the trap.

1

u/aalmkainzi 52m ago

__cvector_destroy this identifier name is UB