FlashKeepers

C and C++ · College · College and University

C++ VI: Smart Pointers and RAII

unique_ptr, shared_ptr, weak_ptr, RAII, ownership semantics, exception safety and resource management idioms.

About this set

35 basic cards for College C and C++. Set 6 of 7 in C++. It opens with "What ownership rule does unique_ptr enforce?" and "How do you transfer ownership between unique_ptrs?". Study it alongside the other College sets below.

35 cards · basic cards · AI-written, checked twice. Edit anything.

Study this set free Look inside first Get FlashKeepers for iPhone

Opens in your browser, no account. Get the iPhone and iPad app · Android app

What ownership rule does unique_ptr enforce?
Show answerExclusive ownership; only one unique_ptr can own a resource at a time
How do you transfer ownership between unique_ptrs?
Show answerUse 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 answerZero or minimal; only when using a non-empty custom deleter does it add size
Can unique_ptr be copy-constructed or copy-assigned?
Show answerNo; unique_ptr is move-only, copying is explicitly deleted
What happens when a unique_ptr goes out of scope?
Show answerIts destructor calls the deleter (usually delete) on the owned resource
How do you get a raw pointer from unique_ptr without transferring ownership?
Show answerCall the get() member function
What is a custom deleter in unique_ptr?
Show answerA callable object passed as a template parameter to customize resource destruction
How does unique_ptr manage arrays differently from single objects?
Show answerUse unique_ptr<T[]> for arrays; it provides operator[] and disables operator*
What is reference counting in shared_ptr?
Show answerA 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 answershared_ptr allows multiple pointers to own the same resource; unique_ptr allows only one owner
What is a control block in shared_ptr?
Show answerA shared data structure holding the reference count, weak count, and deleter
How many internal pointers does a shared_ptr typically maintain?
Show answerTwo: one to the resource and one to the control block
What is the most efficient way to create a shared_ptr?
Show answerUse std::make_shared, which allocates the control block and resource in one operation
What does use_count() return on a shared_ptr?
Show answerThe 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 answerA cyclic reference prevents either from being deleted, creating a memory leak

20 more cards in the app

Study this set free