Memory Management & Pointers Flashcards
7 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Memory Management & Pointers flashcards as text
What is the difference between shallow copy and deep copy in the context of pointers?
Answer: Shallow copy copies pointer values; deep copy allocates new memory and copies the pointed-to data
A shallow copy copies the pointer address (both copies point to the same data), while a deep copy allocates new memory and duplicates the pointed-to data.
What does std::unique_ptr::release() do?
Answer: Relinquishes ownership and returns the raw pointer without deleting the object
release() gives up ownership of the managed object and returns the raw pointer; the caller is then responsible for deleting it.
Which of the following is a correct way to check if a shared_ptr has no managed object?
Answer: All of the above
All three forms — comparing to nullptr, calling .get() == nullptr, and using operator bool — are valid ways to check if a shared_ptr is empty.
What is the purpose of the std::destroy_at function introduced in C++17?
Answer: To explicitly call the destructor of an object at a given address without freeing memory
std::destroy_at(ptr) explicitly calls the destructor of the object at the given address, complementing placement new by handling the destruction step.
In C++, what is a smart pointer's custom deleter used for?
Answer: To override the default heap deallocation with a user-defined cleanup function
A custom deleter is a callable passed to a smart pointer that replaces the default delete, enabling cleanup of resources acquired through non-standard means.
What problem does the Rule of Five address in C++11 and later?
Answer: Ensuring classes with raw pointers correctly implement destructor, copy constructor, copy assignment, move constructor, and move assignment
The Rule of Five states that if a class defines any of the five special member functions managing resources, it should explicitly define all five to avoid incorrect copy/move semantics.
What is the use of reinterpret_cast in pointer contexts?
Answer: Reinterprets the raw bit pattern of a pointer as a different pointer type, with no safety guarantees
reinterpret_cast reinterprets the bit pattern of a pointer as another type with virtually no safety checks, and its result is implementation-defined in most cases.