What this C++ memory-safety question tests
This is an easy C++ question that assesses whether you can spot unsafe patterns in code at a glance. It's the kind of quick reasoning check that appears in phone screens and early rounds, where interviewers want to know if you think carefully about object lifetime and pointer validity before writing production code.
To answer questions like this, you need to trace through the code and ask yourself: does this pointer or reference outlive the object it refers to? Are there use-after-free risks? Does the code respect the ownership semantics of the language? Even simple-looking snippets can hide subtle violations of memory safety that would crash or corrupt state in a real system.
- Object lifetime and scope
- Dangling pointers and references
- Heap vs. stack allocation
- Ownership and cleanup order