Skip to chapter content
navan✳THE BUILD LABC17 / GUIDE
0/24 read
CHAPTER 11 / 21 · MEMORY AND POINTERS

Dynamic memory and ownership.

Read malloc, realloc, free, allocation failure, and lifetime obligations.

BY NAVAN SAMILONG READC17 BASELINETHEORY FIRST
01 / THE MENTAL MODEL

The idea behind the syntax.

Dynamic allocation lets a program request storage whose size is chosen at runtime. malloc returns a pointer to a block of uninitialized storage or a null pointer if allocation fails. The program decides how long that block is needed and must call free exactly once for a successful allocation when ownership ends. This is an explicit obligation: leaving scope does not automatically release a malloc block.

The allocated byte count should be calculated from the element type rather than a guessed constant. An idiom such as count * sizeof *items ties the allocation to the declared pointer target. That multiplication itself can overflow size_t before malloc sees it, so a robust program checks count against SIZE_MAX / sizeof *items. An allocation size is not the same as the number of initialized elements; the program must initialize values before reading them.

02 / THE RULES

What C guarantees.

realloc may move a block and invalidate its old pointer. Assigning its result directly back to the only pointer loses the original block if resizing fails. A temporary pointer preserves the old allocation until success is known. free accepts a null pointer, but freeing a pointer twice or using it after free is invalid. A pointer variable may still contain the old address after free; that does not keep the object alive.

Ownership is a design convention expressed by the API: which function creates the block, which returns it, and which caller frees it. C's type system does not encode that convention for you. The example requests space for three integers, handles allocation failure, initializes every element, and frees the block after the final read. Its small size keeps the lifetime visible.

03 / UNDER THE SURFACE

Why the detail matters.

malloc allocates storage, not a fully formed high-level record with a built-in owner. A typed pointer lets code access the storage as objects under C's effective-type and alignment rules once it is used appropriately. The memory begins with indeterminate contents, so every element the program reads must first be initialized. calloc can produce zero-filled bytes, but bytewise zero is not a universal substitute for every conceptual default in an arbitrary data model.

A robust allocation path checks three things in order: whether the requested count can be multiplied by element size without overflow, whether the allocator returned a non-null pointer, and whether subsequent initialization or operations can fail. Every exit path after successful allocation must release the block or deliberately transfer ownership to another function. The example's one exit path after printing makes that easy to see; real programs often need a cleanup section to keep the obligation visible.

04 / CODE, EXPLAINED

See the rule in context.

dynamic-memory.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    size_t count = 3;
    int *items = malloc(count * sizeof *items);
    if (items == NULL) return 1;
    for (size_t i = 0; i < count; ++i) items[i] = (int)(i + 1);
    printf("Last: %d\n", items[count - 1]);
    free(items);
    return 0;
}
FOLLOW THE EXECUTION

malloc reserves enough storage for three int objects on this implementation. After the null check, the loop writes 1, 2, and 3 into those positions. The print reads the final initialized element and displays Last: 3. free ends the allocation's lifetime before main returns. The count is a fixed small value here, so the multiplication cannot overflow; an external count would require a check.

05 / THE BOUNDARY

Where the rule stops.

Do not read malloc storage before initialization. Do not assume setting one alias to NULL after free repairs other pointers that still refer to the released block.

06 / THE CONNECTION

Keep this model.

Pointers can also point to pointers and to functions. Those forms are useful for collections of strings, configurable behavior, and APIs that need to return an address, but each extra level should preserve a clear ownership story.

Finished reading?
Built with curiosity. By Navan Sami. ↗C · Types, memory, and the rules behind the code.