🌍 C++ Programming · subject
C++ Programming Best Practices Syllabus
Every chapter and topic of Best Practices examined in C++ Programming — 3 chapters, 6 topics, plus 51 flashcards written against it.
Best Practices syllabus — full chapter and topic list
Expand any chapter to see its topics and sub-topics. This is the whole examinable outline for Best Practices in C++ Programming, not a summary of it.
-
Code Optimization
2 topics- Memory Management
- Efficient Algorithms
-
Code Readability
2 topics- Naming Conventions
- Commenting and Documentation
-
Testing and Debugging
2 topics- Unit Testing
- Debugging Techniques
Best Practices flashcards for C++ Programming
18 of 51 cards from the Best Practices deck — real questions with worked answers.
What is the core distinction between the stack and the heap in C++ memory management?
The stack stores automatic (local) variables with automatic storage duration, is managed by the compiler, and is fast (LIFO). The heap (free store) stores dynamically allocated objects that persist until explicitly freed, is manually managed, and is slower.
In C++, which operator pairs must be matched to avoid undefined behavior when freeing dynamic memory?
Use `delete` with memory from `new`, and `delete[]` with memory from `new[]`. Mismatching them (e.g., `delete` on a `new[]` array) is undefined behavior.
Define a memory leak in C++ and give its primary cause.
A memory leak is heap memory that is allocated but never deallocated, so it remains reserved for the program's lifetime. It is caused by losing all pointers to a block (e.g., overwriting or letting a pointer go out of scope) before calling `delete`.
What does RAII stand for, and what problem does it solve?
RAII means Resource Acquisition Is Initialization. A resource (memory, file, lock) is tied to an object's lifetime: acquired in the constructor and released in the destructor, so it is automatically freed when the object goes out of scope, preventing leaks even during exceptions.
Compare `std::unique_ptr` and `std::shared_ptr`.
`unique_ptr` provides exclusive, non-copyable ownership of a resource (movable only) with zero overhead. `shared_ptr` provides shared ownership via a reference count; the resource is freed when the count reaches zero, at the cost of atomic counting overhead.
What is the purpose of `std::weak_ptr` and which problem does it prevent?
`std::weak_ptr` is a non-owning reference to an object managed by a `shared_ptr`; it does not affect the reference count. It prevents cyclic reference leaks, where two `shared_ptr`s pointing to each other keep the count above zero forever.
What is a dangling pointer?
A dangling pointer is a pointer that still references memory that has already been freed (or an object that has gone out of scope). Dereferencing it is undefined behavior.
What is the difference between shallow copy and deep copy in C++?
A shallow copy duplicates pointer values, so both objects share the same underlying memory (risking double-free). A deep copy allocates new memory and copies the pointed-to data, giving each object its own independent resources.
State the Rule of Three in C++.
If a class needs a user-defined destructor, copy constructor, or copy assignment operator, it almost certainly needs all three, because it manages a resource that requires custom copy/cleanup semantics.
State the Rule of Five in modern C++.
If a class defines any of the destructor, copy constructor, copy assignment, it should also consider the move constructor and move assignment operator; managing a resource typically requires all five special member functions.
What is Big O notation used to describe?
Big O notation describes the asymptotic upper bound on an algorithm's running time or space usage as a function of input size $n$, characterizing worst-case growth while ignoring constants and lower-order terms.
Order these common complexities from fastest to slowest growth: $O(n^{2})$, $O(1)$, $O(n \log n)$, $O(\log n)$, $O(n)$, $O(2^{n})$.
$O(1) < O(\log n) < O(n) < O(n \log n) < O(n^{2}) < O(2^{n})$.
What is the average and worst-case time complexity of QuickSort?
Average case is $O(n \log n)$; worst case is $O(n^{2})$, occurring when pivot choices are consistently poor (e.g., already-sorted input with a naive pivot).
What is the time complexity of binary search, and what precondition does it require?
Binary search runs in $O(\log n)$ time and requires the data to be sorted.
What is the average-case lookup complexity of a hash table (e.g., `std::unordered_map`)?
$O(1)$ on average for insertion, deletion, and lookup, degrading to $O(n)$ in the worst case when many keys collide into the same bucket.
Distinguish time complexity from space complexity.
Time complexity measures how the number of operations grows with input size; space complexity measures how the amount of additional memory grows with input size. Both are typically expressed in Big O notation.
What is the difference between Big O, Big Omega ($\Omega$), and Big Theta ($\Theta$)?
Big O ($O$) is an asymptotic upper bound, Big Omega ($\Omega$) is a lower bound, and Big Theta ($\Theta$) is a tight bound that holds when the upper and lower bounds match.
Give the time complexity of accessing an element by index in a `std::vector` versus a `std::list`.
`std::vector` (contiguous array) gives $O(1)$ random access by index. `std::list` (doubly linked list) requires $O(n)$ traversal to reach an arbitrary element.
Planning Best Practices for C++ Programming
Best Practices is about 9% of the C++ Programming syllabus by topic count — 6 of 65 topics, spread over 3 chapters. At roughly 45 minutes per topic plus 12 minutes per sub-topic, a first pass runs to about 5 hours.
The heaviest chapters are Code Optimization (2 topics), Code Readability (2 topics), Testing and Debugging (2 topics) . Front-load those while your energy is high; the short chapters are better revision filler later.
Work top-down: read the chapter, then tick topics off individually rather than marking the whole chapter done. Sub-topics are where silent gaps hide.
Best Practices (C++ Programming) FAQ
What is in the C++ Programming Best Practices syllabus?
Best Practices is split into 3 chapters — Code Optimization, Code Readability and Testing and Debugging, containing 6 topics and 0 sub-topics in total.
How many chapters are there in Best Practices for C++ Programming?
3 chapters. Best Practices accounts for about 9% of the topics in the whole C++ Programming syllabus (6 of 65).
How long should I spend on Best Practices for C++ Programming?
Budget around 5 hours for a first pass through Best Practices — about 45 minutes per topic plus 12 minutes per sub-topic across its 6 topics. Add revision cycles on top.
Are there flashcards for C++ Programming Best Practices?
Yes — a 51-card Best Practices deck. Sample cards are printed on this page, and the full deck is free in the Examius app with spaced repetition scheduling.