Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

collections — concept C23

C has no standard dynamic containers: a collection is built from arrays, structures, pointers, and explicitly stored length or capacity. Every operation must preserve `length <= capacity`, valid index bounds, and buffer ownership; growth through `realloc` needs a temporary pointer so failure does not lose the original allocation.

Version: C23

Syntax

collections

C has no standard dynamic containers: a collection is built from arrays, structures, pointers, and explicitly stored length or capacity. Every operation must preserve length <= capacity, valid index bounds, and buffer ownership; growth through realloc needs a temporary pointer so failure does not lose the original allocation.

A practical vector stores data, length, and capacity; insertion first checks that the new capacity and byte count do not overflow size_t, then calls realloc. Keep the old pointer until success is confirmed.

An empty collection may have data == NULL and length == 0, but data must not be dereferenced. Successful reallocation may invalidate every element pointer; removal during iteration needs index adjustment, and geometric growth needs a limit.

A FIFO queue inserts at the back and removes at the front; a circular buffer implements it in an array with head, tail, and count, distinguishing empty from full and wrapping indices modulo capacity. A LIFO stack uses one top index: push advances it, while pop retreats only after an empty check. A stable priority queue orders first by priority and breaks ties by increasing insertion sequence. An ordered array implementation preserves sorted order: binary search is logarithmic, but insertion and removal shift elements linearly.

Examples

#include <stdio.h>

int main(void) {
    int values[] = {2, 3, 5};
    int sum = 0;
    for (size_t i = 0; i < sizeof values / sizeof values[0]; ++i) sum += values[i];
    printf("%d\n", sum);
    return 0;
}

Result

10

C has no standard dynamic containers: a collection is built from arrays, structures, pointers, and explicitly stored length or capacity. Every operation must preserve `length <= capacity`, valid index bounds, and buffer ownership; growth through `realloc` needs a temporary pointer so failure does not lose the original allocation.

Sources