unique_ptr, shared_ptr, weak_ptr, RAII, ownership semantics, exception safety and resource management idioms.
35 cards · basic cards · AI-written, checked twice. Edit anything.
- What ownership rule does unique_ptr enforce?
Show answer
Exclusive ownership; only one unique_ptr can own a resource at a time
- How do you transfer ownership between unique_ptrs?
Show answer
Use move semantics with std::move or return a unique_ptr from a function
- What is the typical memory overhead of unique_ptr compared to a raw pointer?
Show answer
Zero or minimal; only when using a non-empty custom deleter does it add size
- Can unique_ptr be copy-constructed or copy-assigned?
Show answer
No; unique_ptr is move-only, copying is explicitly deleted
- What happens when a unique_ptr goes out of scope?
Show answer
Its destructor calls the deleter (usually delete) on the owned resource
- How do you get a raw pointer from unique_ptr without transferring ownership?
Show answer
Call the get() member function
- What is a custom deleter in unique_ptr?
Show answer
A callable object passed as a template parameter to customize resource destruction
- How does unique_ptr manage arrays differently from single objects?
Show answer
Use unique_ptr<T[]> for arrays; it provides operator[] and disables operator*
- What is reference counting in shared_ptr?
Show answer
A count of how many shared_ptr instances own the same resource; deletion occurs when count reaches zero
- What is the fundamental difference between shared_ptr and unique_ptr?
Show answer
shared_ptr allows multiple pointers to own the same resource; unique_ptr allows only one owner
- What is a control block in shared_ptr?
Show answer
A shared data structure holding the reference count, weak count, and deleter
- How many internal pointers does a shared_ptr typically maintain?
Show answer
Two: one to the resource and one to the control block
- What is the most efficient way to create a shared_ptr?
Show answer
Use std::make_shared, which allocates the control block and resource in one operation
- What does use_count() return on a shared_ptr?
Show answer
The current number of shared_ptr instances that share ownership of the resource
- What memory problem can occur when two shared_ptrs point to each other?
Show answer
A cyclic reference prevents either from being deleted, creating a memory leak