A stack is a last-in, first-out collection: push adds at the top and pop removes from it. This data structure is separate from the call stack. A dynamic stack of object pointers can track count, capacity, and a separately allocated array of pointers.
before: count=2 capacity=2 [ A ][ B ]
push C: grow to capacity 4 [ A ][ B ][ C ][ unused ]
pop: count=2 [ A ][ B ][ unused ... ] returns C
The invariant is count <= capacity; only indices below count contain active elements. Empty pop must be signaled, and an API that permits a null element needs a separate success status to distinguish it from empty. To grow, first compute a new nonzero capacity and check overflow both in doubling and in multiplication by sizeof *data. Call realloc into a temporary pointer; if it fails, retain the old pointer, count and capacity. Publish the new capacity only after success, then insert and increment count.
The stack can own its pointer array while borrowing the pointed-to objects, or it can own those objects too. Its destructor must follow the chosen contract. void ** describes an array of void * objects; it is not a safe generic substitute for int **. Typed wrapper functions reduce accidental insertion of the wrong kind of object, but runtime lifetime and ownership still matter. If a second allocation fails during construction, release the first allocation. This failure path is as important as the successful push.
Complete borrowed-pointer stack
This implementation begins with an empty, zero-initialized stack and grows on demand. It owns only its pointer array; every target must remain live while used. pop returns a Boolean separately from the item, so a stored null is not confused with an empty stack. out must point to a writable void * object, and s must be a valid initialized stack.
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct { void **data; size_t count, capacity; } Stack;
static bool push(Stack *s, void *value) {
if (s->count == s->capacity) {
size_t max = SIZE_MAX / sizeof *s->data;
if (s->capacity >= max) return false;
size_t next = s->capacity == 0 ? 1 :
s->capacity > max / 2 ? max : s->capacity * 2;
void **p = realloc(s->data, next * sizeof *p);
if (!p) return false;
s->data = p;
s->capacity = next;
}
s->data[s->count++] = value;
return true;
}
static bool pop(Stack *s, void **out) {
if (s->count == 0) return false;
*out = s->data[--s->count];
return true;
}
static void destroy(Stack *s) {
free(s->data); /* targets are borrowed */
*s = (Stack){0};
}
int main(void) {
Stack s = {0};
int a = 10, b = 20;
void *item;
if (!push(&s, &a) || !push(&s, NULL) || !push(&s, &b)) {
destroy(&s);
return 1;
}
while (pop(&s, &item)) {
if (item) printf("%d\n", *(int *)item);
else puts("null element");
}
destroy(&s);
return 0;
}
On successful allocations the output is 20, null element, and 10 on separate lines. The first push grows from zero to one; later full capacities double unless that would exceed the largest representable pointer array. The overflow check precedes multiplication. A failed push leaves the existing data, count and capacity unchanged; the caller destroys the stack on failure. realloc may move the array, but it does not move the borrowed targets. destroy frees the backing array, resets the fields, and never frees a or b, whose automatic lifetimes outlast the stack’s use. Do not retain a popped pointer past its target’s lifetime or interpret a stored pointer as the wrong type.