C++20 sections · 1024 units
Open in Course

Memory Safety Basics

Safe pointer habits

Initialize every pointer to nullptr or valid address immediately. Never leave uninitialized. Check for nullptr before dereferencing if there's any chance it could be null. Don't return pointers to local variables.

Those disappear when function ends, leaving dangling pointers. Return by value or allocate dynamic memory. Use references instead of pointers when possible. They eliminate null errors and can't dangle as easily.