WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Intermediate · question 36 of 100

What are smart pointers in C++? Explain the difference between unique_ptr, shared_ptr, and weak_ptr.?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

Smart pointers in C++ are objects that behave like pointers, but provide additional memory management capabilities. They automatically manage the lifetime of the underlying object and automatically free the memory when the object is no longer needed. This helps prevent memory leaks and other memory-related errors.

There are three types of smart pointers in C++: unique_ptr, shared_ptr, and weak_ptr.

unique_ptr: A unique_ptr is a smart pointer that owns the object exclusively. It cannot be copied or shared, and is destroyed when the unique_ptr goes out of scope or is explicitly deleted. Here’s an example:

    #include <memory>
    #include <iostream>
    
    int main() {
        std::unique_ptr<int> p1(new int(10));
        
        std::cout << *p1 << std::endl;  // prints 10
        
        // std::unique_ptr<int> p2(p1);  // compilation error: unique_ptr cannot be copied
        
        std::unique_ptr<int> p3 = std::move(p1);
        
        std::cout << *p3 << std::endl;  // prints 10
        // std::cout << *p1 << std::endl;  // undefined behavior: p1 is now null
        
        return 0;
    }

In this example, we create a unique_ptr p1 that points to an integer value 10. We then try to copy p1 into p2, but this results in a compilation error because unique_ptr objects cannot be copied. Instead, we use the std::move function to transfer ownership of the underlying object from p1 to p3. After the move, p1 is null and cannot be used to access the object.

shared_ptr: A shared_ptr is a smart pointer that can be shared among multiple objects. It keeps track of the number of objects that reference the underlying object, and automatically frees the memory when the last object goes out of scope or is explicitly deleted. Here’s an example:

    #include <memory>
    #include <iostream>
    
    int main() {
        std::shared_ptr<int> p1(new int(10));
        std::shared_ptr<int> p2 = p1;
        
        std::cout << *p1 << " " << *p2 << std::endl;  // prints 10 10
        
        *p2 = 20;
        
        std::cout << *p1 << " " << *p2 << std::endl;  // prints 20 20
        
        return 0;
    }

In this example, we create a shared_ptr p1 that points to an integer value 10. We then create a second shared_ptr p2 that points to the same object as p1. We can use either p1 or p2 to access the underlying object, and changes made through one pointer are reflected in the other.

weak_ptr: A weak_ptr is a smart pointer that can observe the object managed by a shared_ptr, but does not own the object. It does not increment the reference count, and can be converted to a shared_ptr if the object still exists.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics