C/C++ Concepts — Textbook Reference

Visual primers for ideas you hit every day with g16. Reference-first, with textbook diagrams. Compile examples with g16 -std=gnu17 (C) or g16 -std=gnu++26 (C++).

Pointers

A pointer holds an address. *p reads the value at that address; &x takes the address of x.

x = 42 p 0x7ffd…a4 *p → 42
Pointer p stores the address of x; dereferencing reads 42.
int x = 42;
int *p = &x;
printf("%d\n", *p);   /* 42 */
*p = 7;               /* x is now 7 */

C++ adds nullptr, references int& r = x, and smart pointers (std::unique_ptr) for RAII.

Arrays & contiguous memory

Arrays are a fixed-length contiguous block. Index i lands at base + i * sizeof(T). In C, array names decay to a pointer to the first element.

index value 10 20 30 40 50 [0] [1] [2] [3] [4]
int a[5] — five elements in one contiguous slab; cache-friendly iteration.

C++ prefer std::array<T,N> or std::vector<T> with bounds-aware interfaces; Field code often uses stack arrays in hot paths.

Sorting methods

APITypical complexityNotes
qsort (C)O(n log n) avgFunction pointer comparator; not type-safe
std::sortO(n log n)Introsort; mutates range; random-access iterators
std::stable_sortO(n log n)Preserves order of equal elements
before after std::sort
Comparison sort reorders elements; choose stable sort when satellite data order matters.

Stack vs heap

Stack

Automatic storage: locals, small fixed arrays. Freed on scope exit. Fast, LIFO, limited size.

Heap

malloc / new — explicit lifetime. Larger, flexible. Pair with free / delete or smart pointers.

Templates (C++)

Compile-time generics: one definition, many types. Instantiations happen at compile time — zero runtime polymorphism cost when inlined.

template <typename T>
T add(T a, T b) { return a + b; }

RAII

Resource Acquisition Is Initialization — bind resource lifetime to object lifetime. Destructor releases handles, locks, memory. Core to modern C++ and Grok16 field mandates (no leaks in L2).

Move semantics

Rvalue references (T&&) and std::move transfer ownership without copying heavy buffers — critical for FieldX86 vectors and matrix paths under -O3.