KatInterview

C++ Memory Semantics, Dynamic Dispatch, and Override Pitfalls

System DesignModel answer
MediumModel answerSystem Designc++polymorphismmemory-management

Last updated

Explain pointers vs references, vtable-based dispatch internals, and the override specifier, then diagnose three bugs in a polymorphic sensor class hierarchy.

Context

Embedded and real-time systems — robotics controllers, sensor fusion pipelines, medical devices — rely on C++ precisely because it gives you deterministic memory layout and zero-cost abstractions. But that power cuts both ways: a wrong const qualifier, a missing virtual, or a by-value parameter can silently corrupt behavior in ways that are hard to reproduce under a debugger.

This problem asks you to reason from first principles about three interconnected mechanisms, then apply that reasoning to find and fix concrete bugs.

Part 1: Conceptual Questions

Answer each of the following in a few focused paragraphs. Depth and precision matter more than length.

1. Pointers vs. References Compare the two on: syntax, memory representation, nullability, the ability to rebind, and the lifetime hazards unique to each. When would you reach for one over the other in a modern codebase?

2. Runtime Polymorphism Internals How does the compiler actually implement a virtual function call? Describe the roles of the vtable and the vptr, where each lives in memory, how they are initialized, and what the runtime cost of a virtual call looks like — both the direct cost and the indirect (optimization-blocking) cost.

3. The override Specifier What gap in C++03 did override close? Describe exactly which parts of a function signature must match for an override to be valid, and explain what happens — at both compile time and runtime — when the specifier is omitted and a signature mismatch goes undetected.

Part 2: Code Diagnosis

The following snippet models a sensor abstraction for a robotics platform. It compiles without warnings on many configurations, yet it contains three critical bugs or architectural flaws. Identify each one, explain the observable consequence (including what happens to heap memory), and provide a corrected version of the classes.

#include 
#include 

class Sensor {
public:
    Sensor() = default;
    ~Sensor() {
        std::cout << "Sensor destroyed\n";
    }

    virtual void configure() {
        std::cout << "Default sensor configuration\n";
    }

    virtual void processData(const double& timestamp) {
        std::cout << "Base processing at " << timestamp << "\n";
    }
};

class CameraSensor : public Sensor {
private:
    int* rawFrameBuffer;

public:
    CameraSensor() : rawFrameBuffer(new int[1024]) {}
    ~CameraSensor() {
        delete[] rawFrameBuffer;
        std::cout << "CameraSensor buffer freed\n";
    }

    // Intended to override base configure
    void configure() const {
        std::cout << "Camera customized configuration\n";
    }

    // Intended to override base processData
    void processData(double& timestamp) {
        std::cout << "Camera processing at " << timestamp << "\n";
    }
};

void runDiagnostics(Sensor s) {
    s.configure();
}

For your analysis, address:

  • What does runDiagnostics actually do when handed a CameraSensor instance, and why?
  • What happens to heap memory when a CameraSensor owned by a std::unique_ptr<Sensor> goes out of scope?
  • How would adding override to the CameraSensor methods change the compilation outcome?

Your answer should include a corrected, compiling version of both classes.

Hints

Hint 1

For the conceptual comparison, think about what the compiler is *required* to store in memory for each. A reference and a pointer often lower to the same machine representation, but the language rules around them are very different — focus on those rules rather than the assembly.

Hint 2

For the vtable question, trace what happens between the moment you write `new CameraSensor()` and the moment a virtual call resolves. Which constructor runs first? When exactly does the vptr get set, and to which table?

Hint 3

When hunting the three bugs, check the destructor, then check each method signature character by character against its base declaration — name, parameter types, and cv-qualifiers on `this`. Finally, look at how the base class is passed into `runDiagnostics` and think about what the copy constructor does with a derived object.