All questions
Showing of 65What 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 -
The compiler manages stack memory for you, while heap memory is something you request and give back yourself.
The stack holds function locals and parameters. Each call pushes a frame, the block that holds that call's locals, and each return pops it. So cleanup is automatic and free. The stack is small, often one to eight megabytes. Deep recursion or a huge local array can overflow it and crash 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 memory you never release leaks.
Default to the stack. Use the heap when an object must outlive the current scope or is too big for a frame. Also use it when the size is known only at runtime.
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, while 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, and a reference usually has neither.
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& always receives a real object, 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 -
A pointer to const cannot be used to change the object it points at. A const pointer cannot be made to point anywhere else. To tell them apart, read the declaration from right to left. 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 only stops writes through that pointer. The object itself may still change through some other non-const handle. A const pointer has a fixed address for its whole 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 manage 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. It can be moved but not copied. It is as small and fast as a raw pointer, which 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 use it only when no single object can be the owner.
weak_ptr owns nothing. It observes an object held by shared_ptr and can tell whether it is still alive. You call lock() to get a usable shared_ptr, which is empty if the object 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, so leaks are easy to miss. A long-running server just grows until the allocator or the operating system kills it.
The usual causes are a new with no matching delete, and an early return or a thrown exception that skips the cleanup. Overwriting the only pointer to a block leaks it too.
The fix in modern C++ is structural, not a matter of discipline. Store owned objects in unique_ptr, shared_ptr or a standard container, and their destructors free them on every exit path. For what still slips through, build with AddressSanitizer or run a leak checker. Both name the allocation site.
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 automatically when control leaves the enclosing block. That happens whether you reach the closing brace, return early, or throw.
The order is strictly the reverse of construction. Locals are destroyed 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
This order keeps dependent resources safe. A connection that logs during shutdown can rely on the logger still being alive.
Only objects with automatic storage get this, meaning ordinary locals, not static or heap objects. A heap object made with new is untouched by scope exit, and only the pointer to it is destroyed.
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 a member function whose call is resolved at run time by the object's dynamic type, the type it was created as. Declare it virtual in the base, override it in the derived class, then call through a base pointer or reference. The derived version runs. Without virtual, the call binds to the static type, the type of the pointer or reference, and your override is skipped.
This dynamic dispatch lets you code against an interface. A render loop can call draw() through Shape pointers without knowing the real shapes, so a new shape needs no change to the loop.
The cost is one indirection per call and, usually, no inlining. That is cheap but not free, so not every function should be virtual.
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 needs no 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 provides a default that derived classes may replace. A pure virtual function provides nothing to fall back on. So the compiler refuses to create an object of the class, or of any derived class that leaves the function unimplemented.
Any class with at least one pure virtual function is abstract. You use it as an interface by holding objects through a Shape pointer or reference.
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 cause bugs?
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 are concepts in C++20, and how do they improve templates?
What is a lambda in C++, and how do value and reference captures differ?
How does virtual inheritance solve the diamond problem?
What does std::atomic provide, and when do you still need a mutex?
What is std::string_view, and when can it dangle?
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?
What is the Curiously Recurring Template Pattern (CRTP) used for?
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.
C++ in pictures.
A quick recap of C++ in 32 short animated pictures, one idea at a time. About 8 min to get the whole topic back in your head before you walk in. Pause any picture, slow it down, or step through it.
Pro The first chapter is free to play. The other 28 pictures come with Pro.
C++ cheatsheet
- Stack, heap & pointers01
- Smart pointers02
- Lifetime, RAII & copies03
- Move semantics04
- Virtual functions & classes05
- Containers & iterators06
- Templates & constexpr07
- Exceptions & errors08
- Lambdas, threads & strings09
- Compile, link & debug10
- The model11
- + 5 more inside
This picture is part of Pro. The first chapter is free to play; the other 28 pictures come with Pro.
Get Pro · $10/mo Already Pro? Sign in57 of 65 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.