← Qube Research & Technologies Interview Insights
This sounds like a textbook question until you're actually in it and they keep pushing.
Start by defining raw pointers as thin, non-owning abstractions and smart pointers as RAII-based ownership wrappers. Then compare ownership semantics, memory safety, and performance overhead, and finish with concrete use cases for each in modern C++.
Pro tip: Emphasize that raw pointers are still essential for non-owning observation and that smart pointers are not a silver bullet—mention that passing raw pointers or references as function parameters is often preferred to avoid unnecessary ownership transfer.
Explain that raw pointers are lightweight, non-owning, and require manual memory management, making them error-prone but necessary for low-level or non-owning scenarios.
Introduce smart pointers (unique_ptr, shared_ptr, weak_ptr) as RAII wrappers that automate memory management and encode ownership semantics.
Contrast ownership: raw pointers have no ownership, unique_ptr is exclusive, shared_ptr is shared, weak_ptr is non-owning. Discuss how smart pointers prevent leaks and dangling pointers.
Note that raw pointers have zero overhead, while smart pointers may incur reference counting (shared_ptr) or minor abstraction costs, though often negligible.
Give examples: use raw pointers for non-owning parameters, observers, or interfacing with C APIs; use unique_ptr for exclusive ownership, shared_ptr for shared ownership, and weak_ptr to break cycles.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.