dynamic_array.h v0.3.1
Reference-counted dynamic arrays for C
Loading...
Searching...
No Matches
Functions
Array Modification

Functions for adding, removing, and clearing elements. More...

Functions

DA_DEF void da_push (da_array arr, const void *element)
 Appends an element to the end of the array.
 
DA_DEF void da_insert (da_array arr, int index, const void *element)
 Inserts an element at the specified index.
 
DA_DEF void da_remove (da_array arr, int index, void *out)
 Removes and optionally returns an element at the specified index.
 
DA_DEF void da_pop (da_array arr, void *out)
 Removes and optionally returns the last element.
 
DA_DEF void da_clear (da_array arr)
 Removes all elements from the array.
 
DA_DEF void * da_peek (da_array arr)
 Gets a pointer to the last element without removing it.
 
DA_DEF void * da_peek_first (da_array arr)
 Gets a pointer to the first element without removing it.
 

Detailed Description

Functions for adding, removing, and clearing elements.

Function Documentation

◆ da_clear()

DA_DEF void da_clear ( da_array  arr)

Removes all elements from the array.

Parameters
arrArray to clear (must not be NULL)
Note
Sets length to 0 but preserves capacity
Does not free allocated memory (use da_resize(arr, 0) for that)
da_clear(arr);
assert(da_length(arr) == 0);
DA_DEF void da_clear(da_array arr)
Removes all elements from the array.
Definition dynamic_array.h:1686
DA_DEF int da_length(da_array arr)
Gets the current number of elements in the array.
Definition dynamic_array.h:1700

◆ da_insert()

DA_DEF void da_insert ( da_array  arr,
int  index,
const void *  element 
)

Inserts an element at the specified index.

Parameters
arrArray to modify (must not be NULL)
indexPosition to insert at (must be >= 0 and <= length)
elementPointer to element data to copy (must not be NULL)
Note
Shifts all elements at and after index to the right
Automatically grows array capacity if needed
index == length is equivalent to da_push()
Asserts on out-of-bounds index or NULL parameters
int value = 42;
da_insert(arr, 0, &value); // Insert at beginning
da_insert(arr, da_length(arr), &value); // Insert at end (same as push)
DA_DEF void da_insert(da_array arr, int index, const void *element)
Inserts an element at the specified index.
Definition dynamic_array.h:1609

◆ da_peek()

DA_DEF void * da_peek ( da_array  arr)

Gets a pointer to the last element without removing it.

Parameters
arrArray to peek (must not be NULL and not empty)
Returns
Pointer to the last element
Note
Asserts if array is empty
Returned pointer is valid until array is modified or released
Useful for stack-like access patterns
int* last_ptr = (int*)da_peek(arr);
printf("Last element: %d\n", *last_ptr);
DA_DEF void * da_peek(da_array arr)
Gets a pointer to the last element without removing it.
Definition dynamic_array.h:1994

◆ da_peek_first()

DA_DEF void * da_peek_first ( da_array  arr)

Gets a pointer to the first element without removing it.

Parameters
arrArray to peek (must not be NULL and not empty)
Returns
Pointer to the first element
Note
Asserts if array is empty
Returned pointer is valid until array is modified or released
Useful for queue-like access patterns
int* first_ptr = (int*)da_peek_first(arr);
printf("First element: %d\n", *first_ptr);
DA_DEF void * da_peek_first(da_array arr)
Gets a pointer to the first element without removing it.
Definition dynamic_array.h:2000

◆ da_pop()

DA_DEF void da_pop ( da_array  arr,
void *  out 
)

Removes and optionally returns the last element.

Parameters
arrArray to modify (must not be NULL)
outOptional pointer to store popped element (can be NULL)
Note
Asserts if array is empty
If out is NULL, element is discarded
Does not shrink capacity
int popped;
da_pop(arr, &popped); // Get the value
da_pop(arr, NULL); // Discard the value
DA_DEF void da_pop(da_array arr, void *out)
Removes and optionally returns the last element.
Definition dynamic_array.h:1669

◆ da_push()

DA_DEF void da_push ( da_array  arr,
const void *  element 
)

Appends an element to the end of the array.

Parameters
arrArray to modify (must not be NULL)
elementPointer to element data to copy (must not be NULL)
Note
Automatically grows array capacity if needed
Asserts on allocation failure or NULL parameters
Uses configured growth strategy (DA_GROWTH)
int value = 42;
da_push(arr, &value);
DA_DEF void da_push(da_array arr, const void *element)
Appends an element to the end of the array.
Definition dynamic_array.h:1587

◆ da_remove()

DA_DEF void da_remove ( da_array  arr,
int  index,
void *  out 
)

Removes and optionally returns an element at the specified index.

Parameters
arrArray to modify (must not be NULL)
indexPosition to remove from (must be >= 0 and < length)
outOptional pointer to store removed element (can be NULL)
Note
Shifts all elements after index to the left
Does not shrink capacity
Asserts on out-of-bounds index
int removed;
da_remove(arr, 0, &removed); // Remove first element
da_remove(arr, 2, NULL); // Remove third element, discard value
DA_DEF void da_remove(da_array arr, int index, void *out)
Removes and optionally returns an element at the specified index.
Definition dynamic_array.h:1642