Section 7 of 7 · Programming & Data Foundations
C & C++ Systems Literacy
GPU kernels, runtimes and Python extensions cross native boundaries. You need enough layout, lifetime and build fluency to make a safe bounded change and understand the resulting machine behavior.
9 units 18 topics 0/9
Units
9 in this section
- 7.1 Compiling, linking, loading, ABI and C linkage explain4 h
- 7.2 Integer representation, pointers, references and spans explain · implement4 h
- 7.3 Struct layout, alignment, strides and locality derive · measure5 h
- 7.4 Storage duration, RAII, smart pointers and move semantics explain · implement6 h
- 7.5 Templates, containers and iterator invalidation explain4 h
- 7.6 Undefined behavior, sanitizers and debugger evidence measure4 h
- 7.7 Threads, data races, memory ordering and atomics explain4 h
- 7.8 Python and native code: dtype, stride, ownership and GIL contracts explain5 h
- 7.9 Repeatable CMake builds and test targets implement5 h
Before moving on
Checkpoint
Build a tiny Python-callable C++ component, explain its layout and ownership, and repair one seeded lifetime or bounds error from sanitizer evidence in a clean build.
Deliberately out of scope
Leave for later
- Advanced template metaprogramming and compiler construction
- Large native application architecture
- Lock-free data structures beyond literacy
Resources for this section
Shared by every unit above
- AlgoMaster C++ Use the sections on compilation, pointers and references, memory, RAII and smart pointers, copy and move, STL containers, templates, errors, concurrency and paired exercises. Skip competitive-programming breadth already covered in Algorithms.
- C++ Core Guidelines Use the Philosophy, Interfaces, Resource management, Expressions, Containers and concurrency rule groups to review ownership and safety decisions in the native component.
- cppreference Consult language lifetime, object model, value categories, memory model, atomics, containers and iterator invalidation while reading or modifying code.
- Clang AddressSanitizer and UndefinedBehaviorSanitizer Compile seeded memory defects with AddressSanitizer and UndefinedBehaviorSanitizer, preserve the reports, repair the root cause and keep regression tests.
- pybind11 documentation Follow First steps and NumPy support to expose one small native function with explicit shape, dtype, stride, ownership, GIL and exception behavior.
- CMake tutorial Use the introductory steps for targets, libraries, testing and install rules. Produce one clean debug build and one optimized build without copied one-off commands.
- Algorithmica - AoS and SoA Read the AoS and SoA experiment for a measured case where keeping each record together wins, the counterpart to a single-field scan.
- `ld.so`: dynamic linker/loader Read DESCRIPTION, Dynamic string tokens and
LD_DEBUGto distinguish link-time symbols from runtime shared-library discovery on Linux. - Memory Layout Transformations Read Overview and Topics through AOS Form and SOA Form for field-wise access and locality trade-offs. Skip Intel array-notation syntax and the LIBOR example.
- GDB manual: Backtraces Use section 8.2 for frame numbers,
bt,bt -fulland optimized-out arguments when inspecting a native failure.
Apply the section
Mini-project · Native run-duration histogram with a strict pybind11 binding and a sanitizer-backed repair (12 to 16 h)
Move the run-duration histogram into C++ behind a strict Python boundary. Build it with CMake in debug, release and sanitizer trees, test it against NumPy, and follow one sanitizer report from evidence to regression test.
Steps
- Write the histogram core over spans with an owning
Histogramclass, and test copy independence, moves and bounds errors in a native test executable, using the AlgoMaster RAII and Copy-and-Swap Idiom exercises as reference. - Seed an off-by-one that overflows the bins, capture the sanitizer report in a sanitizer build tree, repair it, connect the report to the frame with
gdb, and keep the input as a regression test. - Test the histogram against
numpy.histogramon random values and on values at and beside every edge, then adopt NumPy's edge rule or document and test your own. - Bind the core with pybind11 under dtype, shape, stride, alignment, ownership, exception and GIL contracts, and test every accepted and rejected input plus output lifetime.
- Build debug, release and sanitizer trees with CMake following the CMake Tutorial building and testing steps and register the native tests with CTest, then document the exported symbols and linked libraries and diagnose one link or loader failure from its message.
- Document the record layout from measured offsets, compare array-of-struct and struct-of-array scans with equal checksums, and split one call across threads with private bins merged after
join, alongside a shared atomic-bin variant.
Deliverables
- The native component: core library, owning
Histogram, native tests, extension module and CMake trees. - The sanitizer write-up with the preserved report, the
gdbsession and the regression test. - Binding and oracle tests plus a boundary note with symbols, libraries, the diagnosed failure and the layout comparison.
Checks
- rubric · Self-grade with the rubric below
Rubric
| Criterion | Pass | Strong |
|---|---|---|
| Oracle agreement | The binding agrees with numpy.histogram on random data and at every edge, or a documented edge rule is tested instead. | Edge cases come from a generator, and a naive index version is kept as a comparison that fails. |
| Strict boundary | Wrong dtype, lists, strided, reversed and multi-dimensional inputs are rejected with the stated exception types, and the result outlives the input. | The binding names the owner of every buffer and the GIL state at each point. |
| Sanitizer repair | The report is preserved and explained, and the regression fails on the seeded code and passes after the repair. | The write-up names what the sanitizers cannot prove here, such as a wrong but in-bounds bin. |
| Build and ABI | Debug, release and sanitizer trees build from scratch, CTest catches a false check, and one failure is diagnosed from its message. | The note says which upgrades force a rebuild of the extension and why. |
| Layout evidence | Predicted offsets match the measured ones, both layouts agree, and raw timings are kept. | The report separates the measured difference from cache explanations it did not test. |
Feeds into: The Python-native histogram, its sanitizer-backed repair and the evidence-first debugging report for the capstone.