C++ Smart Pointer Quiz
40 in-depth questions covering C++ smart pointer types, ownership semantics, memory management, and cyclic reference avoidance — with 16 code examples to solidify understanding.
Question 1
What is the purpose of std::unique_ptr in C++?
Question 2
What is the difference between std::unique_ptr and raw pointers?
std::unique_ptr<int> ptr = std::make_unique<int>(42);
// Automatic cleanup when ptr goes out of scope
int* raw = new int(42);
delete raw; // Manual cleanup requiredQuestion 3
What is the purpose of std::shared_ptr?
#include <memory>
std::shared_ptr<int> ptr1 = std::make_shared<int>(42);
std::shared_ptr<int> ptr2 = ptr1; // Reference count = 2
// Object deleted when last shared_ptr is destroyedQuestion 4
What is std::weak_ptr used for?
Question 5
What is ownership semantics in smart pointers?
Question 6
What is the difference between std::make_unique and std::make_shared?
auto unique = std::make_unique<int>(42); // Allocates int separately
auto shared = std::make_shared<int>(42); // Allocates int and control block togetherQuestion 7
What is a cyclic reference in smart pointers?
class A { std::shared_ptr<B> b; };
class B { std::shared_ptr<A> a; };
auto a = std::make_shared<A>();
auto b = std::make_shared<B>();
a->b = b; // a owns b
b->a = a; // b owns a
// Memory leak: objects never deleted due to circular ownershipQuestion 8
What is the purpose of custom deleters in smart pointers?
auto fileDeleter = [](FILE* f) { if (f) fclose(f); };
std::unique_ptr<FILE, decltype(fileDeleter)> filePtr(
fopen("data.txt", "r"), fileDeleter); // Custom cleanupQuestion 9
What is the difference between unique_ptr and shared_ptr performance?
Question 10
What is the purpose of std::weak_ptr::lock()?
std::weak_ptr<int> weak = someSharedPtr;
if (auto shared = weak.lock()) {
// Object still exists, use it
*shared = 42;
} else {
// Object has been deleted
}Question 11
What is the difference between move semantics and smart pointer ownership transfer?
std::unique_ptr<int> ptr1 = std::make_unique<int>(42);
std::unique_ptr<int> ptr2 = std::move(ptr1); // Ownership transfer
// ptr1 is now nullptrQuestion 12
What is the difference between shared_ptr thread safety guarantees?
Question 13
What is the purpose of std::enable_shared_from_this?
class NetworkConnection : public std::enable_shared_from_this<NetworkConnection> {
public:
void connect() {
// Get shared_ptr to this object safely
auto self = shared_from_this();
// Pass self to callbacks without ownership issues
}
};Question 14
What is the difference between unique_ptr and auto_ptr?
Question 15
What is the purpose of std::shared_ptr::use_count()?
auto ptr1 = std::make_shared<int>(42);
std::cout << ptr1.use_count(); // 1
auto ptr2 = ptr1;
std::cout << ptr1.use_count(); // 2Question 16
What is the difference between smart pointers and garbage collection?
Question 17
What is the difference between unique_ptr and scoped_ptr?
Question 18
What is the difference between shared_ptr and intrusive_ptr?
Question 19
What is the difference between weak_ptr and raw pointers?
Question 20
What is the difference between smart pointer ownership and object ownership?
Question 21
What is the difference between unique_ptr reset and assignment?
std::unique_ptr<int> ptr = std::make_unique<int>(42);
ptr.reset(); // Delete current object, set to nullptr
ptr = std::make_unique<int>(100); // Delete current, assign newQuestion 22
What is the difference between shared_ptr aliasing and ownership?
struct Data { int value; std::string name; };
auto data = std::make_shared<Data>();
std::shared_ptr<int> valuePtr(data, &data->value); // Aliases data
// valuePtr keeps data alive but points to value memberQuestion 23
What is the difference between smart pointer overhead and raw pointer overhead?
Question 24
What is the difference between weak_ptr expired and lock?
std::weak_ptr<int> weak = someSharedPtr;
if (!weak.expired()) {
auto shared = weak.lock(); // Safe access
// Use shared...
}Question 25
What is the difference between unique_ptr for arrays and vectors?
std::unique_ptr<int[]> arr = std::make_unique<int[]>(100);
// Array syntax: arr[50]
std::vector<int> vec(100);
// Vector interface: vec.at(50), vec.size()Question 26
What is the difference between shared_ptr thread safety and data race freedom?
Question 27
What is the difference between custom deleter syntax for unique_ptr and shared_ptr?
auto deleter = [](int* p) { delete p; };
std::unique_ptr<int, decltype(deleter)> uptr(new int(42), deleter);
std::shared_ptr<int> sptr(new int(42), deleter);Question 28
What is the difference between weak_ptr use_count and shared_ptr use_count?
Question 29
What is the difference between smart pointer composition and inheritance?
Question 30
What is the difference between unique_ptr null assignment and reset?
std::unique_ptr<int> ptr = std::make_unique<int>(42);
ptr = nullptr; // Delete object, set to null
ptr.reset(); // Same effect: delete object, set to nullQuestion 31
What is the difference between shared_ptr make_shared and constructor?
auto ptr1 = std::make_shared<int>(42); // One allocation
auto ptr2 = std::shared_ptr<int>(new int(42)); // Two allocationsQuestion 32
What is the difference between weak_ptr and observer_ptr?
Question 33
What is the difference between smart pointer move and copy?
std::unique_ptr<int> ptr1 = std::make_unique<int>(42);
std::unique_ptr<int> ptr2 = std::move(ptr1); // Transfer ownership
std::shared_ptr<int> sptr1 = std::make_shared<int>(42);
std::shared_ptr<int> sptr2 = sptr1; // Share ownershipQuestion 34
What is the difference between unique_ptr and optional?
Question 35
What is the difference between shared_ptr atomic operations and mutex protection?
Question 36
What is the difference between weak_ptr and dangling pointer detection?
Question 37
What is the difference between smart pointer inheritance and polymorphism?
std::shared_ptr<Base> ptr = std::make_shared<Derived>();
// Polymorphic access through base class pointerQuestion 38
What is the difference between unique_ptr release and reset?
std::unique_ptr<int> ptr = std::make_unique<int>(42);
int* raw = ptr.release(); // Release ownership, get raw pointer
// Must manually delete raw
delete raw;
ptr.reset(); // Delete owned object, set to nullptrQuestion 39
What is the difference between shared_ptr and reference counting overhead?
Question 40
What are the fundamental principles for effective smart pointer usage in C++?
