Automatic objects often use a call stack in real implementations, but C specifies storage duration rather than mandating a stack. Calls commonly create frames containing local state; deep recursion or large local arrays may exhaust a finite stack. Dynamic allocation commonly uses a heap; its lifetime is independent of the allocating function. These are implementation models, not the only possible layout.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *p = malloc(3 * sizeof *p);
if (p == NULL) return 1;
for (size_t i = 0; i < 3; i++) p[i] = (int)i + 1;
printf("%d %d %d\n", p[0], p[1], p[2]);
free(p);
p = NULL;
return 0;
}
On successful allocation, output is 1 2 3. malloc takes a byte count and returns uninitialized storage or null on failure. sizeof *p is the size of an int here; it does not dereference p at runtime for this non-variable type. Initialize before reading. free accepts the original allocation pointer or null, not an interior pointer or an automatic object. After freeing, no aliases may access that storage; setting p to null does not fix other aliases. Free each allocation exactly once. Losing the last usable pointer before freeing leaks the allocation. Allocators may retain released storage instead of immediately returning it to the operating system.
calloc(count, size) zeroes the allocated bytes, but all-bits-zero is not guaranteed to represent a null pointer or floating zero on every C implementation; initialize those objects with typed assignments. Check multiplication overflow before allocating: for nonzero count, require count <= SIZE_MAX / sizeof *p (<stdint.h> supplies SIZE_MAX when available). Zero-sized allocation behavior is implementation-dependent; handle zero explicitly.