C++ Basics: const vs static, Non-Virtual Base Destructors, and Stack vs Heap Memory

Read the full interview experience this question came from →

Quick Overview

Three C++ fundamentals questions: how const and static variables differ, what happens when a derived object is deleted through a base-class pointer whose destructor is not virtual, and how stack and heap memory differ. It tests precise knowledge of storage duration, linkage, object lifetime and undefined behavior.

C++ Basics: const vs static, Non-Virtual Base Destructors, and Stack vs Heap Memory

Company: AMD

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

This round asked several short C++ questions. Answer each one precisely and back it with a small code example. ### Clarifying Questions - For `static`, does the interviewer mean a function-local static variable, a namespace-scope static variable, a static data member of a class, or all of them? - Which C++ standard should the answers assume? Some details, such as thread-safe initialization of local statics and `inline` variables, depend on it. ### Part 1 — `const` versus `static` variables What is the difference between a `const` variable and a `static` variable in C++? Can a variable be both? ```hint Two different axes One keyword is about whether the value can change; the other is about lifetime and visibility. Work out what each one controls, then consider the combination. ``` #### What This Part Should Cover - What `const` guarantees, and what it does not change - The meanings of `static` in each context where it applies to variables - Lifetime, initialization timing and linkage in each case - The combination of the two, and the default linkage of a namespace-scope `const` variable ### Part 2 — Deleting a derived object through a base pointer without a virtual destructor What happens when you `delete` a pointer to a base class, the object it points to is actually of a derived class, and the base class does not declare a virtual destructor? Use this program as the example: ```cpp #include <iostream> #include <vector> struct Base { ~Base() { std::cout << "~Base\n"; } }; struct Derived : Base { std::vector<int> data = std::vector<int>(1000); ~Derived() { std::cout << "~Derived\n"; } }; int main() { Base* p = new Derived(); delete p; } ``` ```hint What the standard promises Separate what the language standard says about this program from what you would typically observe when you run it. ``` #### What This Part Should Cover - What the standard says about this program, and why - What typically happens in practice: which destructors run and which resources are lost - The fix, and the alternatives for classes that are not meant to be deleted polymorphically - How `std::unique_ptr` and `std::shared_ptr` behave in the same situation ### Part 3 — Stack versus heap memory What is the difference between stack memory and heap memory? ```hint Who frees it, and when Compare the two by who decides when the memory is released, how large each can grow, and what each costs to allocate. ``` #### What This Part Should Cover - Allocation and deallocation mechanics and their cost - Lifetime and ownership of objects in each - Size limits and failure modes of each - When to choose each, and how modern C++ combines them ### What a Strong Answer Covers - Precise language rules instead of approximate intuitions - A clear line between undefined behavior and what one compiler happens to do - Small, correct code examples - Practical guidance on RAII, smart pointers and destructors in base classes ### Follow-up Questions - Is the initialization of a function-local `static` variable thread-safe? When exactly does it happen? - Why doesn't C++ make every destructor virtual by default? - Can you return a pointer to a local variable? What about a reference to a function-local `static` variable? - Where does a `const` global variable usually live in memory, and what happens if you modify it through `const_cast`?

Overview: Three C++ fundamentals questions: how const and static variables differ, what happens when a derived object is deleted through a base-class pointer whose destructor is not virtual, and how stack and heap memory differ. It tests precise knowledge of storage duration, linkage, object lifetime and undefined behavior.

Read the full AMD Software Engineer interview experience this question came from

|Home/Software Engineering Fundamentals/AMD
AMD logo
AMD
Oct 9, 2026
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
0
0

This round asked several short C++ questions. Answer each one precisely and back it with a small code example.

Clarifying Questions Guidance

  • For static , does the interviewer mean a function-local static variable, a namespace-scope static variable, a static data member of a class, or all of them?
  • Which C++ standard should the answers assume? Some details, such as thread-safe initialization of local statics and inline variables, depend on it.

Part 1 — const versus static variables

What is the difference between a const variable and a static variable in C++? Can a variable be both?

What This Part Should Cover Guidance

  • What const guarantees, and what it does not change
  • The meanings of static in each context where it applies to variables
  • Lifetime, initialization timing and linkage in each case
  • The combination of the two, and the default linkage of a namespace-scope const variable

Part 2 — Deleting a derived object through a base pointer without a virtual destructor

What happens when you delete a pointer to a base class, the object it points to is actually of a derived class, and the base class does not declare a virtual destructor? Use this program as the example:

#include <iostream>
#include <vector>

struct Base {
    ~Base() { std::cout << "~Base\n"; }
};

struct Derived : Base {
    std::vector<int> data = std::vector<int>(1000);
    ~Derived() { std::cout << "~Derived\n"; }
};

int main() {
    Base* p = new Derived();
    delete p;
}

What This Part Should Cover Guidance

  • What the standard says about this program, and why
  • What typically happens in practice: which destructors run and which resources are lost
  • The fix, and the alternatives for classes that are not meant to be deleted polymorphically
  • How std::unique_ptr and std::shared_ptr behave in the same situation

Part 3 — Stack versus heap memory

What is the difference between stack memory and heap memory?

What This Part Should Cover Guidance

  • Allocation and deallocation mechanics and their cost
  • Lifetime and ownership of objects in each
  • Size limits and failure modes of each
  • When to choose each, and how modern C++ combines them

What a Strong Answer Covers Guidance

  • Precise language rules instead of approximate intuitions
  • A clear line between undefined behavior and what one compiler happens to do
  • Small, correct code examples
  • Practical guidance on RAII, smart pointers and destructors in base classes

Follow-up Questions Guidance

  • Is the initialization of a function-local static variable thread-safe? When exactly does it happen?
  • Why doesn't C++ make every destructor virtual by default?
  • Can you return a pointer to a local variable? What about a reference to a function-local static variable?
  • Where does a const global variable usually live in memory, and what happens if you modify it through const_cast ?
Loading comments...