Appendix A — Pointers in C

TipRecommended by ChatGPRT
  1. Preamble
    • Introduce the importance of pointers in C
    • Highlight that pointers are used for:
      • Dynamic memory management
      • Function callbacks / OOP in C
      • Low-level memory access (e.g., MMIO in system programming)
  2. Pointer basics
    • Pointer declaration, initialization, and dereferencing (*p)
    • NULL pointer and wild/invalid pointers
    • Pointer arithmetic and relationship with arrays (ptr[i] == *(ptr + i))
    • sizeof and pointer types
  3. Functions and Function Pointers
    • Passing-by-Value vs. Passing-by-Reference
      • How C passes arguments by value
      • Using pointers to modify caller variables
      • Double pointers (int **p) for dynamic structures
    • Function Pointer
      • Declaration and initialization
      • Invoking functions through pointers
      • Use cases: callbacks, table-driven design, Linux Kernel hooks, OOP-like vtables
  4. Dynamic Memory Allocation
    • malloc, calloc, realloc, free
    • Pointer to allocated memory
    • Pointer arrays vs pointer-to-pointer
  5. Pointer to Void (void *)
    • Generic memory buffers
    • Casting to proper type for access
  6. Pointer with volatile keyword
    • Purpose: prevent compiler optimization on memory-mapped hardware or shared memory
    • Examples: MMIO, flags in embedded systems
  7. Common Pitfalls about Pointers
    • Dangling Pointer
      • Definition and causes (e.g., freed memory, local variable address)
      • How to avoid
    • Double-Free and Use-After-Free
      • Examples and consequences
      • Best practices for safe memory management
    • Indexing Overflow / Buffer Overflow
      • Pointer arithmetic mistakes
      • Array bounds violations
      • Tools for detection: Valgrind, ASAN

A.1 Preamble

In this chapter, we focus on the basic usages of pointers in C language, including normal pointer to data and function pointers.

A.2 Pointer Basics

A.3 Functions and Function Pointer

A.3.1 Passing-by-Value vs. Passing-by-Reference

A.3.2 Function Pointer

A.4 Dynamic Memory Allocation

A.5 Pointer to Void

A.6 Pointer with volatile keyword

A.7 Common Pitfalls about Pointer

A.7.1 Dangling Pointer

A.7.2 Double-Free and Use-After-Free

A.7.3 Indexing Overflow