On this page
Undefined Behavior & Sanitizers
Prev: Function Pointers & Callbacks | Up: pointers_and_memory | Next: stdlib
Rel: Dynamic Memory & Allocators, Build Pipeline & Linking
Ref: cppreference — Undefined Behavior
Tags: #public #prog #c #memory #safety #sanitizers
Contents
High-Frequency Undefined Behavior (UB) Traps
When a C program triggers Undefined Behavior, the compiler is permitted to assume it never happens — silently deleting checks or corrupting memory:
- Reading uninitialized local variables.
- Out-of-bounds array indexing or buffer overflow.
- Dereferencing
NULL, dangling pointers, or doublefree(). - Signed integer overflow (unsigned arithmetic wraps modulo $2^n$; signed overflow is UB).
- Violating strict aliasing (use
memcpyfor type punning, never*(float *)&int_val).
Essential Compiler Flags & Sanitizers
Always develop and test with warnings + AddressSanitizer (ASan) + UndefinedBehaviorSanitizer (UBSan):
cc -std=c17 -g -O1 -Wall -Wextra -Wpedantic -Werror \
-fsanitize=address,undefined main.c -o main
Linked from
Related notes
- tuts/c/pointers_and_memory Linked here; links to this page; shared tags: #c, #memory, #prog
- tuts/c/pointers_and_memory/function_pointers_and_callbacks Linked here; links to this page; shared tags: #c, #prog
- tuts/c/pointers_and_memory/dynamic_memory_and_allocators Linked here; shared tags: #c, #memory, #prog
- tuts/c/compilation_and_internals/build_pipeline_and_linking Linked here; shared tags: #c, #prog
- tuts/c/stdlib Linked here; shared tags: #c, #prog
- tuts/c/compilation_and_internals/memory_layout_and_abi shared tags: #c, #memory, #prog
- tuts/c/pointers_and_memory/pointers_and_pointer_arithmetic shared tags: #c, #memory, #prog
- tuts/c/tutorials/arena_allocator_pattern shared tags: #c, #memory, #prog
- tuts/c shared tags: #c, #prog
- tuts/c/builtins shared tags: #c, #prog