Fundamentals 64 min read

C++ Interview Topics: Smart Pointers, Memory Management, Polymorphism, and More

This article provides a comprehensive overview of common C++ interview questions, covering smart pointer implementations, memory management techniques, object-oriented concepts such as polymorphism and virtual functions, STL containers, threading, and modern C++ features like move semantics and coroutines.

Deepin Linux
Deepin Linux
Deepin Linux
C++ Interview Topics: Smart Pointers, Memory Management, Polymorphism, and More

The article begins with an introduction to a C++ interview session, outlining the need to assess understanding of fundamentals, object‑oriented programming, templates, and the STL.

Smart Pointer Implementation

It explains the original std::auto_ptr and its drawbacks, then walks through a step‑by‑step custom implementation of a basic smart pointer ( MyAutoPtr) that provides automatic destruction, dereference operators, copy‑construction, and assignment handling.

template <class T>
class MyAutoPtr {
public:
    MyAutoPtr(T *ptr): _ptr(ptr) {}
    ~MyAutoPtr() { delete _ptr; }
    T* get() const { return _ptr; }
    T& operator*() { return *_ptr; }
    T* operator->() { return _ptr; }
private:
    T* _ptr;
};

Further improvements add proper copy‑construction and move semantics to avoid double‑deletion and resource leaks.

Memory Management and RAII

The text emphasizes RAII (Resource Acquisition Is Initialization) as a core C++ technique: resources are acquired in constructors and released in destructors, ensuring deterministic cleanup even when exceptions occur. It highlights std::unique_ptr and std::shared_ptr as standard RAII‑based smart pointers.

#include <memory>
std::unique_ptr<MyClass> p(new MyClass);
std::shared_ptr<MyClass> s = std::make_shared<MyClass>();

Object‑Oriented Features

Polymorphism is described through virtual functions and vtables. A base class with a virtual display() method is overridden in derived classes, and dynamic binding selects the correct implementation at runtime.

class Base { public: virtual void display() { std::cout << "Base"; } };
class Derived : public Base { public: void display() override { std::cout << "Derived"; } };
Base* p = new Derived();
p->display(); // prints "Derived"

STL Containers

The article briefly covers std::vector growth strategy (doubling capacity) and the internal structure of std::map (red‑black tree) versus std::unordered_map (hash table), explaining why the standard library chooses red‑black trees for ordered maps.

Thread Synchronization

Common synchronization primitives are listed: mutexes, condition variables, semaphores, barriers, and atomic operations. The differences between std::lock_guard (simple scoped lock) and std::unique_lock (flexible, supports condition variables) are highlighted.

Modern C++ Features

Right‑value references and move semantics are introduced, showing how std::move transfers ownership without copying. A coroutine example demonstrates C++20's co_yield and co_await syntax for generator‑style code.

#include <coroutine>
struct generator {
    struct promise_type {
        int current;
        auto get_return_object() { return generator{handle.from_promise(*this)}; }
        auto initial_suspend() { return std::suspend_always{}; }
        auto final_suspend()   { return std::suspend_always{}; }
        void return_void() {}
        auto yield_value(int v) { current = v; return std::suspend_always{}; }
    };
    // ... implementation omitted for brevity ...
};

generator simple() {
    co_yield 1; co_yield 2; co_yield 3;
}

Overall, the article serves as a dense reference for C++ interview preparation, covering classic pitfalls (e.g., dangling pointers, copy‑elision issues) and modern language facilities.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PolymorphismC++STLmodern C++
Deepin Linux
Written by

Deepin Linux

Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.