Write a function int safeDeref(int* ptr, int defaultVal) that returns *ptr if ptr is not null, otherwise returns defaultVal. This pattern protects against null pointer crashes. Your implementation needs an if statement: if(ptr != nullptr) return *ptr; else return defaultVal;.
Test with both null and valid pointers to see both code paths execute. I use this pattern when working with optional values. Instead of crashing when a pointer is null, you provide a sensible default.
This makes your code more solid and predictable.