Security Fundamentals
Prerequisites: Performance optimization & profiling
C++ inherits every memory-safety concern covered in Learn C's Security Fundamentals lesson — buffer overflows, use-after-free, uninitialized reads — but modern C++ idioms meaningfully reduce how often they occur in practice:
std::string/std::vectorinstead of raw arrays — both manage their own bounds and memory, eliminating the classic buffer-overflow class of bug by construction.- Smart pointers instead of raw
new/delete— removes most use-after-free and double-free bugs, the same way RAII removes most resource leaks. .at()instead of[]when bounds safety genuinely matters more than the small performance cost of the check.- Compiling with sanitizers during development
(
-fsanitize=address,undefined), exactly as recommended for C.
Common mistakes
- Assuming "modern C++" style automatically makes code memory-safe
— it drastically reduces the chance of these bugs, but raw
pointers,
reinterpret_cast, and manual memory tricks are all still legal C++ and still just as dangerous when used.