Integrated Daily Battle Plan - Day1 Cheatsheet
2025-12-18 17:55
Tags: [[_Integrated Daily Battle Plan]], [[study]]
Integrated Daily Battle Plan - Day1
Engineering Tech Cheat Sheet: Advanced C++ & Robotics Architecture
Owner: Jo, SeungHyeon (Senior Robotics Software Engineer)
Version: 1.0 (Integration of C++17/20, OS Internals, and System Design)
1. Advanced Memory Management (Smart Pointers)
A. Internal Structure & Performance
-
Structure:
std::shared_ptr<T>contains Two Pointers:-
Managed Object Pointer: Points to data (
T*). -
Control Block Pointer: Points to metadata on Heap.
-
-
Control Block: Contains
Strong Ref Count,Weak Ref Count, Custom Deleter.-
Atomic Operations:
Ref Countincrements/decrements use atomic hardware instructions (lock xadd). -
Cost: Atomic ops cause CPU Cache Line Locking/Flush. Expensive in high-frequency loops (e.g., 100Hz LiDAR callback).
-
To visually clarify item 1.A of your Cheat Sheet (the
std::shared_ptrstructure), we are providing a diagram. You should be able to draw this on a whiteboard during an interview.
B. std::make_shared vs new (The Trade-off)
| Feature | **std::shared_ptr |
**std::make_shared |
|---|---|---|
| Allocations | 2 (Object + Control Block separate) | 1 (Contiguous Block) |
| Cache Locality | Poor (Potential cache miss) | Excellent (Spatial Locality) |
| Weak Ptr Trap | Object memory released when Strong=0 | Memory Bloat Risk |
-
⚠️ The Trap (Memory Bloat): In
make_shared, Control Block and Object are one memory chunk. Even ifStrong Count == 0, the ENTIRE memory (100MB LiDAR Data) is NOT released untilWeak Count == 0.-
Rule: For large data with
weak_ptrobservers -> Usenew.
-
Rule: For large data with
C. Parameter Passing
-
Bad:
void func(std::shared_ptr<T> p)-> Calls copy ctor -> Atomic Inc/Dec -> Performance Hit. -
Good:
void func(const std::shared_ptr<T>& p)-> No Ref Count change -> Zero Overhead.
2. System Architecture & Low-Latency Design
A. Zero-Copy vs. Socket Communication
-
The Problem (Socket/TCPROS): Even on localhost, data travels:
User Space->Kernel Space->User Space. (Requires Serialization + 4 Data Copies). -
The Solution (Shared Memory):
mmapsame physical RAM to virtual address space of multiple processes. (0 Copies).
B. The Virtual Memory Trap
-
Scenario: Sending
std::vectoror pointers via Shared Memory. -
Failure:
0x1234in Process A ≠0x1234in Process B. Pointers become invalid (Segfault). -
Fix: Use Relative Pointers (Offset based) or Fixed-size structures (Flattened Data).
C. Serialization Strategy
-
JSON: Human readable, but Heavy parsing & Large payload. (Use for Configs only).
-
Protobuf / FlatBuffers:
-
Schema Evolution: Tag-based fields allow adding/removing fields without breaking legacy receivers (Forward/Backward Compatibility).
-
Performance: Binary format, smaller size. FlatBuffers allows access without unpacking (Zero-copy read).
-
3. C++ Design Patterns (Safety-Critical)
A. Singleton (Static Initialization Fiasco)
-
Danger: Global variables in different
.cppfiles have undefined initialization order. -
Fix (Meyers’ Singleton): Use
staticlocal variable inside a function. Guaranteed to initialize on first call.C++
static Config& getInstance() { static Config instance; // Lazy Init + Thread-Safe (C++11+) return instance; }
B. PIMPL (Pointer to Implementation)
-
Goal: Break compilation dependency (reduce build time) & ABI stability.
-
Implementation:
-
Header:
std::unique_ptr<Impl> pImpl;+ Forward Declaration. -
Cpp: Define
struct Impl { headers... }.
-
-
Critical Detail: Destructor
~Class()must be defined in .cpp. (Compiler needssizeof(Impl)to generate delete code).
4. Concurrency & Multi-threading
A. std::async Trap
-
Issue: Default policy allows
deferredexecution (runs serially on main thread when.get()is called). -
Fix: Always force new thread:
std::async(std::launch::async, ...)
B. Race Conditions
-
Shared Ptr: Ref Count is thread-safe, but Managed Object is NOT.
- Simultaneous
read&writeto the sameshared_ptrglobal instance requiresstd::mutex.
- Simultaneous
5. JavaScript Internals (for C++ Mindset)
A. Scope & Memory
-
Hoisting: Variable declarations move to top.
var= garbage init,let/const= RAII-like safety (TDZ). -
Closure Trap:
varin loops shares one memory address. Useletfor block scoping.
B. Event Loop Priority
-
Call Stack (Synchronous)
-
Microtask Queue (
Promise,process.nextTick) -> High Priority (ISR-like). -
Macrotask Queue (
setTimeout, I/O) -> Low Priority.
6. Suggested Action Items
-
Build a Sandbox: Create a
cpp_experimentsrepo. -
Verify
make_sharedBloat: Write a test allocating 100MB, holding aweak_ptr, and checking RAM usage (RSS) viatop. -
Implement PIMPL: Refactor one of your heavy classes to use PIMPL and measure compile time difference.
Cool Wind on Study