#ifndef DYNAMIC_ARRAY_HEADER
#define DYNAMIC_ARRAY_HEADER
* @brief A dynamic array struct that contains the size, capacity,
* and data pointer used to implement this dynamic structure.
* @tparam T the type of data to store in the dynamic array
* @field data a pointer to the data in the dynamic array on the heap
* @field size the number of elements used in the dynamic array
* @field capacity the number of elements the dynamic array can hold
* @brief Create a new dynamic array with the indicated initial capacity.
* @tparam T the type of data the array will store
* @param capacity its initial capacity, with a default value of 50
* @return dynamic_array<T>* a pointer to the new dynamic array
dynamic_array<T> *new_dynamic_array(unsigned int capacity = 50)
dynamic_array<T> *result = (dynamic_array<T>*)malloc(sizeof(dynamic_array<T>));
new(result) dynamic_array<T>();
// Make sure result was allocated
result->data = (T*)malloc(sizeof(T) * capacity);
for (int i = 0; i < capacity; i++)
// Call constructor to initialise each of the 10 elements
new(&result->data[i]) T();
// Make sure that data was allocated, if not set capacity to 0
if(result->data == nullptr)
result->capacity = capacity;
* @brief Free the memory allocated to the dynamic array. Once freed
* the data in the array will no longer be accessible.
* @tparam T the data type of the dynamic array
* @param array a pointer to the dynamic array to free
void delete_dynamic_array(dynamic_array<T> *array)
// Ensure that the array is allocated
// Clear to ensure we remove any data from memory before freeing it
// Call destructors on all elements
for (int i = 0; i < array->capacity; i++)
// Free the data and the array itself
// Ensure we don't have a dangling pointer
// Free the dynamic array itself
* @brief Resize the capacity of the dynamic array.
* If the new capacity is smaller than the current size, the size will be updated to match the new capacity.
* @tparam T the type of data in the dynamic array
* @param array the dynamic array to grow
* @param new_capacity the new capacity of the dynamic array
* @returns true if this succeeded, or false if it could not reallocate memory
bool resize(dynamic_array<T> *array, unsigned int new_capacity)
// Ensure that the array is allocated
if (!array) return false;
// Call destructors if we are reducing size
for(int i = array->capacity - 1; i >= (int)new_capacity; i--)
// Resize the data in the array
T* new_data = (T*)realloc(array->data, sizeof(T) * new_capacity);
// Check if the allocation failed
// We failed to allocate memory, so we can't resize the array
// Call constructors if we increased size
for(int i = array->capacity; i < new_capacity; i++)
new(&array->data[i]) T();
// Update the array's data and capacity
array->capacity = new_capacity;
// Update the size if the new capacity is smaller than the current size
if (new_capacity < array->size)
array->size = new_capacity;
* @brief Add an element to the end of the dynamic array
* @tparam T the type of data in the dynamic array
* @param value the value to add to the end of the dynamic array
* @param array the dynamic array to add the value to
bool add(dynamic_array<T> *array, T value)
// Ensure that the array is allocated
if (!array) return false;
// Check if we need to resize the array, and if we failed to resize the array
// We double the capacity and add 1 to address issues where capacity is 0 initially
if (array->size >= array->capacity && !resize(array, array->capacity * 2 + 1))
// We didn't have space, and we failed to resize the array!
// Add the value to the end of the array
array->data[array->size] = value;
* @brief read and return the value of the indicated element from the dynamic array.
* If the index is out of bounds, the function will return the indicated default value.
* @tparam T the type of data in the dynamic array
* @param array the dynamic array to remove the element from
* @param index the index of the element to remove
* @param default_value the value to return if the index is out of bounds
T get(const dynamic_array<T> *array, unsigned int index, T default_value)
// Check if the index is out of bounds
if (!array || index >= array->size)
// The index is out of bounds, so return the default value
return array->data[index];
* @brief set the value of the indicated element from the dynamic array.
* If the index is out of bounds, the function will do nothing and return false.
* @tparam T the type of data in the dynamic array
* @param array the dynamic array to set the element in
* @param index the index of the element to change
* @param value the value to set the element to
* @returns true when the value is set, or false if this failed
bool set(dynamic_array<T> *array, unsigned int index, T value)
// Check if the index is out of bounds
if (!array || index >= array->size)
// The index is out of bounds, so do nothing
array->data[index] = value;