C++ Programming

120 flashcards by MentisLibrary

Modern C++ as it is written today: the type and memory model, RAII and smart pointers, classes and the rule of five, move semantics, templates and concepts, the standard containers and algorithms, exceptions, threads, and the build toolchain.

What does auto do in a variable declaration?
It deduces the variable's type from its initializer at compile time. The variable is still statically typed; auto is not dynamic typing.
Which type should index a standard container?
std::size_t, the unsigned type that size() returns. Mixing a signed int with an unsigned size converts the int to unsigned, so a negative value compares as a huge number.
What does brace initialization {} give you that () does not?
It forbids narrowing conversions and never parses as a function declaration.
const vs constexpr
const means the value cannot be changed after initialization; it may still be computed at run time. constexpr additionally requires the value to be computable at compile time.
How does a reference differ from a pointer?
A reference is an alias that must be bound at initialization, can never be rebound, and can never be null. A pointer is an object holding an address that may be reseated or null.
What is nullptr and why is it preferred over NULL?
nullptr is a keyword of type std::nullptr_t that converts to any pointer type but not to an integer, so overload resolution picks the pointer overload.
Write a range-based for loop that does not copy each element.
Bind the loop variable to a reference: const auto& to read, auto& to modify.
What is the only difference between struct and class in C++?
Default member access and default inheritance access: public for struct, private for class. Everything else is identical.
What are the three meanings of static in C++?
At namespace scope it gives a name internal linkage. Inside a function it makes a variable persist across calls. Inside a class it makes a member belong to the class rather than to any object.
Why prefer enum class over a plain enum?
enum class enumerators are scoped to the enum name and do not implicitly convert to int, so two enums cannot be compared or mixed by accident.
When should a parameter be std::string_view instead of const std::string&?
When the function only reads the characters. std::string_view accepts a std::string, a string literal or a character buffer without allocating or copying.
What does sizeof measure?
The size in bytes of a type or expression, including padding, evaluated at compile time. It never evaluates its operand.
Is signed integer overflow defined in C++?
No. Overflow of a signed integer is undefined behavior, so the compiler may optimize on the assumption it cannot happen. Unsigned types wrap modulo 2^N and are well defined.
What is the difference between ++i and i++ for a class type?
++i increments and returns a reference to the object; i++ must first copy the old value, increment, then return the copy. For iterators the copy is real work.
Where does a stack variable live compared to a heap allocation?
An automatic variable lives on the stack and is destroyed when its scope exits. A new allocation lives on the free store until something calls delete.
What does new do that malloc does not?
new allocates storage and runs the constructor, returning a typed pointer; delete runs the destructor and then frees. malloc and free only move raw bytes.
When must you use delete[] instead of delete?
When the memory came from new[]. delete[] destroys every element; plain delete on an array is undefined behavior.
What is RAII?
Resource Acquisition Is Initialization: a resource is owned by an object, acquired in its constructor and released in its destructor, so scope exit frees it even when an exception is thrown.
What does std::unique_ptr guarantee?
Exclusive ownership of a heap object, released in the unique_ptr destructor. It cannot be copied, only moved, so exactly one owner exists at a time.
How does std::shared_ptr decide when to free?
It keeps a separate control block with a reference count. Each copy increments it, each destruction decrements it, and the object is deleted when the count reaches zero.
What problem does std::weak_ptr solve?
Cycles. Two shared_ptr objects pointing at each other never reach a count of zero. A weak_ptr observes without owning, and lock() returns a shared_ptr only if the object is still alive.
Why prefer std::make_unique and std::make_shared over a bare new?
They keep the allocation and the smart pointer construction in one expression, so no raw pointer is ever exposed. make_shared also puts the object and the control block in a single allocation.
What is a dangling pointer?
A pointer whose target has already been destroyed or freed. Reading or writing through it is undefined behavior, and it often appears to work until it corrupts unrelated data.
When does a destructor run?
When an automatic object leaves scope, when a temporary is destroyed at the end of its full expression, when delete is called on a pointer to it, and when the object containing it is destroyed.
What is a memory leak in C++, and what usually causes one?
Allocated storage that is never released because the last pointer to it was lost. The usual causes are a missing delete, an early return that skips cleanup, and shared_ptr cycles.
const int* p vs int* const p
const int* p is a pointer to constant data: you may reseat the pointer but not write through it. int* const p is a constant pointer: you may write through it but not reseat it.
What happens when an array is passed to a function?
It decays to a pointer to its first element, so the length is lost inside the function. Only the address is passed, never a copy of the elements.
What is a double free, and why is it dangerous?
Calling delete twice on the same address. It corrupts the allocator's bookkeeping, and the second call may free a block that has since been handed to unrelated code.
Why initialize members in the initializer list rather than in the constructor body?
Members are constructed before the body runs, so assigning in the body default-constructs and then overwrites. const members and reference members can only be set in the list.
What do = default and = delete do?
= default asks the compiler to generate the standard implementation of a special member. = delete removes it, so any call is a compile error rather than a link error.