C++20 sections · 1024 units
Open in Course

Common Pointer Errors

Avoid common crashes

A dangling pointer points to memory no longer valid. Take address of local variable, function returns, memory gets reused. Dereferencing reads garbage or crashes. Dereferencing nullptr crashes.

Check if(ptr != nullptr) before using a pointer that might be null. This prevents mysterious crashes. Other errors: deleting memory twice, using after deletion. For now, initialize pointers and check for nullptr.