Skip to content
academia.sh

Course Intermediate

Memory and Pointers in C

Published Updated

By the end of this course

Start course

01

Pointers

Taking an address and accessing the pointed-to value, pointer arithmetic advancing in elements, an array decaying to a pointer, null and dangling pointers, hiding a type behind opaque pointers, and callbacks through function pointers — each together with the question of who owns the memory being pointed to.

  1. 01 Pointer Basics The question of who owns a block of memory; the full run of the five-file measurement core and the 4/4/3/2 distribution across four classes (language, caller, callee, unowned), followed by measuring the address-of and dereference operators without printing an address.
  2. 02 Pointer Arithmetic Why incrementing a pointer, decrementing it, and subtracting two pointers operates in elements rather than bytes; why an arithmetic that runs past the bounds is never run.
  3. 03 Arrays and Pointers How an array's name decays into the address of its first element when it is passed to a function; why that decay loses the length information; and how ownership of a dynamically allocated array passes to the caller.
  4. 04 Null and Dangling Pointers Why a null pointer is a checkable condition, while a dangling pointer keeps pointing at a freed block and is therefore never run; how both are two faces of the unowned class.
  5. 05 `void` and Opaque Pointers How a void pointer deliberately hides the type it points to, and how an opaque type hides its contents entirely, keeping ownership inside the callee module.
  6. 06 Function Pointers Pointers that point at a function itself and can be passed as a parameter to another function; the ownership of a callback's context belongs to the caller.

02

Dynamic Memory

Allocation, zero-filled allocation and resizing functions, documenting the deallocation contract in a header file, measuring a memory leak by a counter difference, whether a tool catches it or not, and memory being divided into automatic, static and dynamic regions.

  1. 01 Memory Allocation Functions malloc, calloc, realloc, and free all work from the same heap and all four leave the same question: who frees the block you allocated? The answer is always the same — the allocator is the caller; this lesson measures that with the counter difference, reads the zeroing directly, and shows that resizing leaves the old ownership unchanged.
  2. 02 Deallocation Discipline When a function returns a piece of text, who owns that text is not written in the signature — what writes it is the comment in the header file. Two functions pointing at the same record, one lending it and one returning a copy, show how the same contract can bind to two different owners.
  3. 03 Memory Leaks A memory leak gives no error message and crashes no program — it only widens the gap between what was allocated and what was freed. This lesson produces a leak two different ways, catches both with the counter difference, and shows why the total comes out to exactly 2.
  4. 04 Memory Debugging Tools Finding a leak by holding a counter by hand is not possible in a real program — a tool that does this is run instead. This lesson shows that the counting wrapper the course has used from the start already models the one thing such a tool does: is there a free for every allocation, or not.
  5. 05 Memory Layout A program's memory is not one single region; it is split into separate areas — automatic, static, and dynamic — and which region a block sits in decides from the start who its owner will be. This lesson lines up the four language observations side by side with the allocate-and-free observation and measures this distinction without printing an address.

03

Robustness

Why undefined behavior exists and how its defined counterpart is written instead, reporting errors with return codes and error numbers, bounds checking where the caller supplies the length, and isolating implementation-defined behavior from portable code.

  1. 01 Undefined Behavior Reading a dangling pointer, using freed memory, freeing a block twice, and accessing an array out of bounds are all undefined behavior; none of them run — the lesson describes each one in prose and runs its defined counterpart alongside it.
  2. 02 Error Handling Patterns When a function fails, who frees the block it had already allocated up to that point is not a rule the compiler enforces — it is the function's own contract; the lesson measures this by running a return code, an error number, and a single-exit-point cleanup pattern.
  3. 03 Bounds Checking record_create's limit parameter takes its length not from the source but from the caller; the lesson runs how this parameter prevents a buffer overflow, snprintf's truncation notification, and whose responsibility the length is.
  4. 04 Portability Three values the shared core deliberately never printed — addresses, type sizes other than char, malloc(0)'s return — are target-dependent; this lesson isolates target-dependent behavior by printing a comparison the standard guarantees instead of a value, and gathers the course's fifteen lessons into a closing table.

Final exam

Memory and Pointers in C — Final Exam

In preparation

This course's question bank is not complete yet. The exam opens here automatically once enough questions are added.

Start typing to search.

↑↓ ↵ Esc navigate · open · close