liveBook · Manning

Free

As explained in Modern C

free is a C standard library function that releases dynamically allocated storage previously obtained from an allocation function.

free releases dynamically allocated storage after it is no longer needed. It belongs to the stdlib.h allocation interface alongside malloc, calloc, realloc, and aligned_alloc; these functions operate through void* pointers so the storage can serve any object type. section 13.1

Releasing a dynamically allocated vector

A program handling a large, variable collection—such as one double for each living person—can allocate storage on the fly and later release it with free. The allocation and release form a lifecycle: every allocation requires a corresponding free, although the calls need not occur in the same function. section 13.1 section 13.1.2

Use free only when its pointer argument satisfies the allocation rules:

  • Pass a pointer returned by malloc, calloc, aligned_alloc, or realloc, or pass a null pointer. section 13.1.2
  • Do not free an ordinary variable, a compound literal, a pointer into only part of an allocated object, or storage already freed. section 13.1.2
  • Check allocation/free pairs to detect memory leaks, which can cause resource exhaustion, poor performance, or random crashes. section 13.1.2

How Modern C covers it

How deep it goes
Defines release and pointer-validity rules, but does not descend into allocator implementation.
Examples
Uses a large vector of doubles and allocation/free counting to illustrate dynamic storage and lifecycle consistency.
What you can do after
Operate dynamic storage safely by releasing valid allocations and identifying leaks or invalid free attempts.
How it connects
Connects free with malloc, calloc, realloc, aligned_alloc, void pointers, dynamic allocation, and memory leaks.

Also explained in

Compare how 2 books explain free →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.