{"id":21567681,"url":"https://github.com/sohaib90/dynamic_array","last_synced_at":"2025-03-18T05:41:46.182Z","repository":{"id":158825309,"uuid":"634268480","full_name":"Sohaib90/dynamic_array","owner":"Sohaib90","description":"Implementation of a dynamic array in c ","archived":false,"fork":false,"pushed_at":"2023-05-06T14:25:21.000Z","size":17,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-24T12:25:15.868Z","etag":null,"topics":["c","dynamic-arrays","structs"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sohaib90.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2023-04-29T15:29:13.000Z","updated_at":"2023-05-02T12:15:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"fb9b5e82-6776-4923-b3fb-8ed37334637e","html_url":"https://github.com/Sohaib90/dynamic_array","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sohaib90%2Fdynamic_array","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sohaib90%2Fdynamic_array/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sohaib90%2Fdynamic_array/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sohaib90%2Fdynamic_array/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sohaib90","download_url":"https://codeload.github.com/Sohaib90/dynamic_array/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244166637,"owners_count":20409177,"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":["c","dynamic-arrays","structs"],"created_at":"2024-11-24T10:32:18.549Z","updated_at":"2025-03-18T05:41:46.160Z","avatar_url":"https://github.com/Sohaib90.png","language":"C","readme":"# dynamic_array\nImplementation of a dynamic array in c \n\nThis repository contains a very basic implementation of a dynamic array in C\nThe feature implementation that are completed up till now are: \n\n1. The `DataType_t` enum is implemented to represent the capability of dynamic array to handle multiple data types. For your own data types, you can customize the source code.\n\n2. `array_t` is a `struct type` that represents the dynamic array. It hold the information for the array's `type`, `size`, `occupancy` and the actual `data` of the array.\n\n3. `array_size(DataType_t)` returns the right size of the data type in bytes.\n\n4. `array_create(...)` returns an `array_t` type dynamic array initialized using `malloc()`\n\n5. `array_push(...)` is the crux of the dynamic array implementation. It checks if the `array.size == array.occupied` and proceeds to expand memory in case the array is out of memory to insert a new element. This expansion is performed by calling `realloc()`\n\n6. `array_free()` as the name suggests, deallocates the memory on the heap which was created using `malloc()` in `array_create(...)`\n\n**Note**: Any addition, fixes or bugs are welcomed wholeheartedly. \n\n## **Usage**\nThe following code depicts and minimal implementation of a dynamic `int` array using `array_t`\n\n```c\n#include \"dynamic_array.h\"\n\nint main(void){\n\n    array_t int_array = array_create(30, ARR_INT);\n\n    for (int j=0; j\u003c100; j++){\n        int value = j; // created just to distinguish between index and value\n        array_push(\u0026int_array, j, \u0026value);\n        printf(\"int_array[%d]: %d \\n\", j, value);\n    }\n\n    array_free(\u0026int_array);\n    return 0;\n}\n```\n\nThe following snippet shows how to create a dynamic array for `int`, `vec3_t` and `float` types\n\n```c\n#include \"dynamic_array.h\"\n\nint main(void){\n\n    array_t vec3_array = array_create(10, ARR_VEC3);\n    array_t int_array = array_create(30, ARR_INT);\n    array_t float_array = array_create(2, ARR_FLOAT);\n\n    for (int i=0; i\u003c30; i++){\n        vec3_t temp = {i, i+1, i+2};\n        array_push(\u0026vec3_array, i, \u0026temp);\n        printf(\"vec3_array[%d]: (%d, %d, %d) \\n\", i, temp.a, temp.b, temp.c);\n    }\n\n    for (int j=0; j\u003c100; j++){\n        int value = j;\n        array_push(\u0026int_array, j, \u0026value);\n        printf(\"int_array[%d]: %d \\n\", j, value);\n    }\n\n    for (int k=0; k\u003c100; k++){\n        float value = k + 0.1f;\n        array_push(\u0026float_array, k, \u0026value);\n        printf(\"float_array[%d]: %f \\n\", k, value);\n    }\n\n    array_free(\u0026vec3_array);\n    array_free(\u0026int_array);\n    array_free(\u0026float_array);\n    return 0;\n}\n```\n\n### **TODO:**\n\n1. Extend dynamic array to delete an element from the array. ","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsohaib90%2Fdynamic_array","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsohaib90%2Fdynamic_array","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsohaib90%2Fdynamic_array/lists"}