NO

Author Topic: Containers for C code review  (Read 323 times)

Offline AB

  • Member
  • *
  • Posts: 3
Containers for C code review
« on: October 15, 2016, 04:24:52 PM »
Hello, I started a project to mimic the containers from the C++ Standard Library in the C language. The implementation is 100% macro, and I want to find and fix bugs and coding mistakes early on, hence this post.

The project files are for Pelles C 8.0 with GNU makefiles for Linux users. The chosen language standard is C99 and the "license" is Creative Commons Zero (CC0) public domain.

Code example for ctnr/string.h:

Code: [Select]
#include <stdio.h>
#include "ctnr/string.h"

int main(void)
{
    string s;

    if (string_create(s, "Hello"))
    {
        string_strcat(s, " World!");
        printf("\"%s\" (length %zu)\n", string_cstr(s), string_len(s));
        string_destroy(s);
    }
}

Code example for ctnr/dynarray.h:

Code: [Select]
#include <stdio.h>
#include "ctnr/dynarray.h"

int main(void)
{
    dynarray(int) dai;

    if (dynarray_create(dai, 3))
    {
        dynarray_ptrcopy(dai, ((const int []){10, 20, 30, 40}), 4);
        printf("Size: %zu, Capacity: %zu\n",
            dynarray_size(dai), dynarray_capacity(dai));
        printf("Contents: ");

        for (dynarrayiter(dai) it = dynarrayiter_begin(dai);
            it != dynarrayiter_end(dai);
            dynarrayiter_inc(it))
        {
            printf("%d ", dynarrayiter_deref(it));
        }

        printf("\nElement at index 2: %d\n", dynarray_at(dai, 2));
        dynarray_destroy(dai);
    }
}

Website: https://sourceforge.net/projects/ctnr/
Code: https://sourceforge.net/p/ctnr/code/HEAD/tree/

For your convenience, the snapshot of Revision 1 was attached to this post.
« Last Edit: October 15, 2016, 04:29:56 PM by AB »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 1571
Re: Containers for C code review
« Reply #1 on: October 15, 2016, 07:23:13 PM »
Good Job!  :)
Thanks.