{"id":28352088,"url":"https://github.com/rphii/c-array","last_synced_at":"2025-08-22T12:05:29.840Z","repository":{"id":294518150,"uuid":"987233067","full_name":"rphii/c-array","owner":"rphii","description":"Very simple C vector single file implementation, works for any type.","archived":false,"fork":false,"pushed_at":"2025-05-23T22:42:50.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-04T05:20:45.911Z","etag":null,"topics":["any-type","c","clang","easy","gcc","performant","simple","single-header-lib","single-header-library","tcc","universal","vector"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rphii.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-20T19:15:36.000Z","updated_at":"2025-05-23T22:42:53.000Z","dependencies_parsed_at":"2025-05-23T23:30:20.568Z","dependency_job_id":"25725d77-dbe4-49d3-b592-86f444b57336","html_url":"https://github.com/rphii/c-array","commit_stats":null,"previous_names":["rphii/c-vector-simple","rphii/c-array"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/rphii/c-array","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rphii%2Fc-array","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rphii%2Fc-array/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rphii%2Fc-array/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rphii%2Fc-array/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rphii","download_url":"https://codeload.github.com/rphii/c-array/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rphii%2Fc-array/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261046324,"owners_count":23102243,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["any-type","c","clang","easy","gcc","performant","simple","single-header-lib","single-header-library","tcc","universal","vector"],"created_at":"2025-05-27T23:04:54.160Z","updated_at":"2025-08-22T12:05:29.820Z","avatar_url":"https://github.com/rphii.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# c-array\n\nVery simple C array / vector [single file](array.h) implementation, works for any type.\n\n## Functions\n\n```c\n    array_grow(array, capacity) /* only reserve memory */\n    array_resize(array, length) /* only grow and set length */\n    array_copy(array)           /* copies array */\n    array_push(array, item)     /* push one item */\n    array_pop(array, item)      /* pop one item */\n    array_at(array, index)      /* alternative to array[index] */\n    array_it(array, index)      /* alternative to \u0026array[index] */\n    array_len(array)            /* query length */\n    array_cap(array)            /* query capacity */\n    array_clear(array)          /* set length to zero, keep memory */\n    array_free(array)           /* free memory */\n```\n\n## Error Handling\n\nThere is no error handling, BUT a sophisticated amount of errors can happen:\n\n- Different kinds of memory allocation / reallocation\n- Out of bounds access\n\nIf any error occurs, it is one that should never even happen in the first\nplace, hence I just `exit(1)`. For that, see [Debuggability](#Debuggability).\n\n## Debuggability\n\nAll but the query- and freeing functions handle the `NDEBUG` preprocessor\ndefinition.\n\nIf it is not defined, and an error occurs (out of bounds, allocation) a message\nwill be printed describing the error and from where it originated, in the form\nof `file:line-nb:function() array error: message`.\n\nIf it is defined, the error from where it originated is left out in any case.\nAdditionally, the out of bounds checking (present in e.g. `array_at` and\n`array_it`) will be disabled.\n\n## Example\n\nAs seen in [main.c](examples/main.c):\n\n```c\n#define ARRAY_IMPLEMENTATION\n#include \"../array.h\"\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n\nint main(void) {\n\n    int *origin = 0; /* initialize to 0! */\n    printf(\"push numbers\\n\");\n    for(size_t i = 0; i \u003c 10; ++i) {\n        array_push(origin, rand());\n    }\n    int *numbers = array_copy(origin);\n    printf(\"length %zu / pointer %p\\n\", array_len(numbers), numbers);\n\n    array_resize(numbers, 5);\n    printf(\"/* resize back to %zu elements, capacity is still %zu */\\n\", array_len(numbers), array_cap(numbers));\n\n    printf(\"/* access by [] */\\n\");\n    for(size_t i = 0; i \u003c array_len(numbers); ++i) {\n        printf(\"%zu: %u\\n\", i, numbers[i]);\n    }\n\n    printf(\"/* access by _at */\\n\");\n    for(size_t i = 0; i \u003c array_len(numbers); ++i) {\n        printf(\"%zu: %u\\n\", i, array_at(numbers, i));\n    }\n\n    printf(\"/* access by _it */\\n\");\n    for(size_t i = 0; i \u003c array_len(numbers); ++i) {\n        printf(\"%zu: %p-\u003e%u\\n\", i, array_it(numbers, i), *array_it(numbers, i));\n    }\n\n    printf(\"/* access by pop */\\n\");\n    for(size_t i = 0; array_len(numbers); ++i) {\n        printf(\"%zu: %u\\n\", i, array_pop(numbers, i));\n    }\n    printf(\"length %zu\\n\", array_len(numbers));\n\n    array_free(numbers); /* free once done */\n    array_free(origin);\n    printf(\"freed array, verify null: %p\\n\", numbers);\n}\n```\n\nCompile with `gcc main.c` to get the full debug-experience, as described above.\n\nCompile with `gcc main.c -DNDEBUG` to generate more performant code.\n\n\n## Single-Header Generation\n\nI've kept the actual [source](src/array.c) and [header](src/array.h) code in the subdirectory `src`.\n\nTo create the [single-header file](array.h), use the [python script](gen-single-file.py).\n\n## See also\n\nI took inspiration from the following:\n\n- \u003chttps://github.com/Mashpoe/c-vector\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frphii%2Fc-array","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frphii%2Fc-array","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frphii%2Fc-array/lists"}