All questions
Showing of 59What is the difference between stack and heap memory?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Stack memory is managed for you by the compiler; heap memory you ask for and give back yourself.
The stack holds function locals and parameters. Each call pushes a frame, each return pops it, so cleanup is automatic and free. It is small, often one to eight megabytes, and blowing past it with deep recursion or a huge local array crashes the process.
The heap is a separate pool you allocate from at runtime. Objects there live until something frees them, so they can outlive the function that created them. Allocation costs a real call into the allocator, and forgetting to release leaks.
Default to the stack. Reach for the heap when the object must outlive the current scope. Also use it when the size is known only at runtime, or the object is too big for a frame.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the difference between a pointer and a reference?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
A reference is another name for an existing object; a pointer is an object that stores an address.
That difference drives everything else. A reference must be initialized when declared and can never be reseated to a different object. A pointer can be null, can be reassigned, and can be moved through an array with arithmetic. A pointer also has its own storage and its own address; a reference usually has neither.
Practically: use a reference when the thing must exist and will not change identity. Use a pointer when absence is meaningful, when you need to rebind, or when you walk memory.
References make the contract clearer at the call site. A function taking const std::string& promises it will not be handed nothing, so it needs no null check.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the difference between a const pointer and a pointer to const?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Read the declaration from right to left and the two fall out cleanly. const binds to whatever sits on its left, unless nothing is there, in which case it binds right.
int x = 1, y = 2;
const int* p = &x; // *p is read-only; p = &y is fine
int* const q = &x; // *q = 5 is fine; q = &y will not compile
A pointer to const means you may not write through that pointer. The object itself may still change through some other non-const handle. A const pointer means the address is fixed for the pointer's life, so it must be initialized at declaration.
Pointer to const is the one you write constantly. It is how a function says it will read your data and not modify it. That lets callers pass constants safely, and lets the compiler catch an accidental write.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What are smart pointers, and how do unique_ptr, shared_ptr and weak_ptr differ?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Smart pointers are class templates that own a heap object and free it automatically when they go out of scope. You get lifetime handling without ever writing delete.
unique_ptr models sole ownership. Exactly one owns the object, it cannot be copied, only moved, and it is as small and fast as a raw pointer. That makes it the default choice.
shared_ptr models shared ownership through a reference count. The object dies when the last owner does. It costs an extra control block and atomic count updates, so reach for it only when ownership has no single home.
weak_ptr owns nothing. It observes an object held by shared_ptr and tells you whether that object is still alive. You call lock() to get a usable shared_ptr, or nothing if it is gone.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is a memory leak, and how do you prevent one in C++?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
A memory leak is heap memory you allocated and never released, with no pointer left to release it. Nothing crashes right away, which is what makes leaks nasty. A long-running server just grows until the allocator or the OS kills it.
The classic shapes are a new with no matching delete, and an early return that skips the cleanup. A thrown exception does the same thing. So does overwriting a pointer that held the only handle to a block.
The fix in modern C++ is structural, not a matter of discipline. Store owned objects in unique_ptr, shared_ptr or a standard container. Their destructors run cleanup on every exit path, including the ones you forgot about.
For what still slips through, build with the address sanitizer or run a leak checker. Both name the allocation site, which is far more useful than the symptom.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What happens when a local object goes out of scope, and in what order?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Its destructor runs at the closing brace of the enclosing block, automatically. That happens whether you fall off the end, hit a return, or throw.
Order is strictly the reverse of construction. Locals die newest first, which matters when a later object depends on an earlier one. Inside one object, the destructor body runs, then members in reverse declaration order, then base classes.
{
Logger a;
Connection b(a);
} // ~b runs, then ~a
That reverse ordering is what keeps dependent resources safe. A connection that logs during shutdown can count on the logger still being alive. One caveat: only automatic-storage objects get this. A heap object made with new is untouched by scope exit; just the pointer dies.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is a virtual function, and what does it let you do?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
A virtual function is one the runtime picks based on the object's actual type. The type of the pointer or reference you call through does not decide it.
Declare it virtual in the base, override it in the derived class, then call through a base handle. The derived version runs. Without virtual, the call binds to the static type and your override is silently skipped.
This is what lets you write code against an interface. A render loop can hold a vector of Shape pointers and call draw() on each. It never learns whether they are circles or polygons, and adding a new shape leaves it untouched.
The cost is one indirection per call and, usually, no inlining. That is cheap but not free, so you do not mark everything virtual by reflex.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is a pure virtual function, and how does it differ from a virtual one?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
A pure virtual function is declared with = 0 and has no required implementation in the base. It says every concrete derived class must supply one.
struct Shape {
virtual double area() const = 0; // no body, derived classes must provide one
}; // Shape s; fails to compile, Shape is abstract
The difference from a plain virtual function is obligation. A virtual function ships a default that derived classes may replace. A pure virtual ships nothing to fall back on. So the compiler refuses to instantiate the class, and refuses any derived class that leaves it unimplemented.
Any class with at least one pure virtual is abstract. You use it as an interface: hold objects through a Shape pointer or reference. The compiler then proves that no half-finished type ever gets created.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is move semantics, and what problem does it solve?
What is an rvalue reference, and how does it differ from an lvalue reference?
What does std::move actually do, and what does it not do?
What does a move constructor do, and when is it chosen over copying?
What are templates in C++, and how do function and class templates differ?
What are the main categories of standard containers, and what separates them?
What is the difference between a C array and a std::vector?
What is the difference between a vector's size and its capacity, and how does it grow?
What is an exception, and what do throw, try and catch each do?
How do you define your own exception type, and what should it derive from?
How does the C++ compilation and linking process work, stage by stage?
What is the difference between a declaration and a definition, and why do headers matter?
What is RAII, and why is it fundamental to modern C++?
What is wrong with a raw pointer that owns the object it points to?
When is shared_ptr the right choice rather than unique_ptr?
How does shared_ptr keep track of how many owners an object has?
What happens when two objects hold shared_ptr to each other, and how do you fix it?
What happens when you pass a unique_ptr by value into a function?
What is a dangling pointer, and how do you avoid creating one?
How do new and delete differ from malloc and free?
Why is allocating on the stack faster than allocating on the heap?
What are the Rule of Three, the Rule of Five and the Rule of Zero?
What is the difference between std::move and std::forward?
Can an rvalue reference bind to an lvalue?
When does the compiler generate a move constructor for you, and when not?
What state is a moved-from object left in, and what can you do with it?
Should you wrap a return value in std::move?
How does the compiler actually implement virtual function dispatch?
How many vtables exist for a class with five objects, and when are they created?
Why does a base class need a virtual destructor, and what breaks without one?
Can a destructor be pure virtual, and what does that require of you?
Can a constructor be virtual in C++, and what do you do instead?
What is object slicing, and when does it bite you?
How do you choose between vector, list and deque for a given job?
What is the difference between map and unordered_map, and when do you pick each?
What is iterator invalidation, and which container operations cause it?
How do iterators work, and why do the iterator categories matter?
What is template specialization, and when do you actually need it?
What is constexpr, and how is it different from const?
What does the mutable keyword do, and when is using it justified?
What are the usual causes of undefined behaviour in C++ code?
What happens if you return a reference to a local variable?
Can a constructor throw, and what happens to the resources it already holds?
What happens between a throw and the catch that finally handles it?
How do exceptions differ from error codes, and when do you prefer each?
What is perfect forwarding, and how do you implement it correctly?
Why is shared_ptr slower than unique_ptr or a raw pointer?
Why does marking move operations noexcept matter so much in practice?
How does make_shared differ from constructing a shared_ptr from a raw new?
How do you make a copy assignment operator exception-safe?
What is SFINAE, and when would you use it over template specialization?
This answer is part of Pro.
The full written answer, with the trade-offs and follow-ups an interviewer will probe.
No matches
Try a different filter or search term.
51 of 59 C++ answers are in Pro.
Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.
- Full answers + code
- AI explanations, simpler or deeper
- 1,000 AI credits / month
- Cancel anytime
Change topic
Pick a different technology or stack. Your current topic stays put until you choose a new one.
MEAN
MongoDB, Express, Angular, Node.jsMERN
MongoDB, Express, React, Node.jsDjango
Python Full-Stack DevelopmentRuby on Rails
Convention over ConfigurationServerless on AWS
Serverless Architecture on AWSInterviewers also test these - they're common to every stack, whichever one you picked above.
Flutter Mobile
Flutter Cross-Platform Mobile DevelopmentInterviewers also test these - they're common to every stack, whichever one you picked above.
Spring Boot
Enterprise Java Development.NET
Microsoft EcosystemVue
Vue.js, Vite, TypeScript, Tailwind, Node.jsGo Backend
Golang, gRPC, PostgreSQL, Redis, RabbitMQInterviewers also test these - they're common to every stack, whichever one you picked above.
FastAPI
Python, FastAPI, SQLAlchemy, PostgreSQLReact Native
React, TypeScript, Redux, FirebaseiOS Native
Swift, SwiftUI, UIKit, FirebaseAndroid Native
Java, Jetpack Compose, FirebaseDevOps / Platform
Docker, Kubernetes, Terraform, CI/CDInterviewers also test these - they're common to every stack, whichever one you picked above.
AI Engineer
LLMs, RAG, Agents, EvalsAI-Powered Developer
Claude Code, Copilot, Agentic WorkflowsCore SWE Interview Prep
Data structures, algorithms, OS, concurrency, networking, gitInterviewers also test these - they're common to every stack, whichever one you picked above.
Interviewers also test these - they're common to every stack, whichever one you picked above.