Systems & software · topic D1 · 80 hours
Modern C++
The robot control side is almost all C++: rclcpp in ROS 2, real-time threads of controllers, drivers, middleware. Poor C++ and real-time behavior starts leaking from the first line of code.
What to learn
- Value/reference/move semantics, RAII, and the ownership model of smart pointers (unique first, shared sparingly)
- Templates and compile-time computation: enough to read other people's code and write generic interfaces
- Concurrency primitives: thread, mutex, condition_variable, atomic, plus priority inversion
- Real-time taboos: runtime dynamic allocation, exceptions, unbounded lock contention, implicit copies
The lesson · plain words
What makes robot C++ special is not syntax but discipline: every millisecond in the real-time loop must be accounted for, and every large object on the heap must have a clear owner and a clear release point.
Modern C++ (11/14/17) offers not new tricks but a system that 'lets the compiler manage memory for you'. This page covers the most valuable pieces of that system.
Copy or move: the fate of large objects
Point clouds, images, and trajectory arrays are easily several MB. One pass-by-value call means one full copy — across multiple call layers, just hauling memory eats your real-time budget. std::move transfers ownership directly, moving only a pointer.
Watch the animation: copy doubles the cost, while move is an O(1) pointer handover. The price is discipline: after the move the source object is an empty shell and must not be touched again — the ownership system buys its safety with exactly this constraint.
Interactive figure · drag it and see
RAII: resources follow objects
Locks are acquired at construction and released at destruction; the same goes for files, device handles, and memory. If an exception propagates up the stack, stack objects destruct automatically — resources never leak. This is C++'s biggest engineering advantage over C.
Practical rules: bare new/delete is essentially forbidden; use unique_ptr for exclusive ownership and shared_ptr for sharing. In robot code be restrained with shared_ptr — reference counting itself carries atomic overhead; in the real-time loop prefer unique_ptr or raw pointers with explicit ownership.
Templates and compile time: where the performance comes from
Eigen, std::function, callback registration — templates underneath all of them. Templates expand at compile time with zero runtime cost — that is the source of C++ real-time performance, and also why template error messages read like gibberish.
The minimum you need: read the first three lines of a template error message; know what auto, lambda, and constexpr each do; write one template function that accepts any callback. At that point you can read ROS 2 source code.
The engineering checklist for robot C++
① Hot paths that forbid exceptions need an alternative error channel (return codes / expected); ② preallocate containers (reserve) to avoid malloc inside the real-time loop; ③ be careful with iostream and log asynchronously; ④ virtual calls carry indirection overhead — keep them out of hot loops in a 1 kHz cycle.
How to learn: no grinding exercises. Rewrite one lab ROS 2 node from Python to C++; one rewrite beats ten reads of the tutorial — you will hit every item above.
Three questions — do you really get it?
1. Continuing to use the source object after std::move results in?
- AA compile error
- BUndefined behavior / reading an empty shell — a runtime accident
- CAn automatic re-copy
Show answer
B. After a move the source is in a 'valid but unspecified' state. The compiler does not stop you; discipline is on you.
2. The core guarantee of RAII is?
- AMemory never leaks
- BResource lifetime is bound to object lifetime, safe even through exceptions
- CThe program runs faster
Show answer
B. Stack unwinding destructs automatically. This is the foundation of unified management for locks, handles, and memory.
3. What should never appear in a 1 kHz real-time loop?
- ARuntime heap allocation (malloc)
- BFloating-point multiplication
- CLocal variables
Show answer
A. malloc has unbounded duration and may take locks. Preallocate and pre-construct; do only work of deterministic duration inside the loop.
Keep going deeper
Links without a marker stay on this site; the rest point to the Chinese edition until its translation batch lands.
How it fails in the field
- Calling new/delete inside a 1 kHz callback sends jitter straight to the millisecond level — a week of debugging before you discover it was memory allocation
- shared_ptr everywhere: the atomic operations of reference counting become a hidden bottleneck, and CPU usage climbs for no visible reason
You have learned enough when
The Chinese edition carries the full topic page for Modern C++, including the complete outline, depth requirements per career track, and further derivations: robotguide.dev/k/D1/.