Skip to content

LowLevelEnthusiast/dynamic-c-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

dynamic-c-array

Header-only generic dynamic array library for C.

This library provides dynamic arrays using malloc, realloc, and macros while keeping a clean array-like interface.

Internally, metadata is stored directly before the array in memory, allowing automatic resizing and efficient access to size and capacity information.

The library works with any data type.


Features

  • Generic dynamic arrays for any data type
  • Header-only implementation
  • Automatic resizing using realloc
  • Automatic shrinking when usage drops below 25%
  • Metadata stored internally
  • Simple macro-based API
  • Pre-allocation support
  • Contiguous memory layout

Installation

Simply include the header file in your project:

#include "dynamic_arrays.h"

Compile normally:

gcc main.c -o app

Example

#include "dynamic_arrays.h"

int main(void) {
    int *arr = vector_create(int);

    vector_push_back(arr, 10);
    vector_push_back(arr, 20);
    vector_push_back(arr, 30);

    vector_print(arr, "%d");

    vector_delete_element(arr, 1);

    vector_print(arr, "%d");

    vector_reuse(arr);

    vector_print(arr, "%d");

    vector_free(arr);

    return 0;
}

API

vector_create()

Creates a dynamic array.

Default capacity

int *arr = vector_create(int);

Custom initial capacity

int *arr = vector_create(int, 100);

vector_push_back()

Appends an element to the vector.

vector_push_back(arr, value);

vector_delete_element()

Deletes an element by index and shifts remaining elements left.

vector_delete_element(arr, index);

vector_print()

Prints the vector using the supplied format specifier.

vector_print(arr, "%d");

vector_reuse()

Resets the vector size to 0 without freeing allocated memory.

vector_reuse(arr);

vector_free()

Frees all memory associated with the vector.

vector_free(arr);

vector_size()

Returns the current number of elements stored in the vector.

vector_size(arr);

vector_capacity()

Returns the current allocated capacity of the vector.

vector_capacity(arr);

Time Complexity

Operation Complexity
vector_push_back() Amortized O(1)
vector_delete_element() O(n)
vector_size() O(1)
vector_capacity() O(1)

Internal Design

The vector internally stores metadata before the actual array memory.

+-----------+----------------------+
| metadata  | actual array memory  |
+-----------+----------------------+
              ^
              returned pointer

The metadata stores:

  • Current size
  • Current capacity
  • Element size

This allows the library to expose a clean array-like interface while still supporting dynamic resizing internally.


Notes

  • The vector pointer may change after vector_push_back() because of realloc
  • Always use the same vector variable after operations
  • Memory must be released using vector_free()
  • The library is not thread-safe

License

MIT License

About

Dynamic Array in C which can be generalized for any data type

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages