Flashcards · C++ · Free
C++ flashcards, generated for you.
Example C++ study cards to learn from right now — then generate a full set from your own notes (plus a practice quiz) and export to Quizlet or Anki. Free, no account needed.
Example C++ flashcards
What is the difference between declaring a variable and defining it in C++?
Declaration announces a variable's type and name; definition allocates memory. A declaration can occur multiple times, but definition happens once. Example: `extern int x;` (declaration) vs `int x = 5;` (definition).
Why does `int arr[] = {1,2,3};` decay to a pointer, and what is the gotcha?
Arrays decay to pointers to their first element in most contexts. The gotcha: `sizeof(arr)` inside a function gives pointer size (4–8 bytes), not array size. Use `std::array` or pass size explicitly to avoid this.
What is the difference between `pass-by-value`, `pass-by-reference`, and `pass-by-pointer`?
Pass-by-value: copies data (slow for large objects). Pass-by-reference (`int& x`): alias to original, cannot be null. Pass-by-pointer (`int* x`): can be null, requires dereferencing. Use references for efficiency and clarity; pointers only when null is needed.
Explain the `const` qualifier in three contexts: `const int`, `int* const`, and `const int*`.
`const int`: value cannot change. `int* const`: pointer itself cannot change (points to same address). `const int*`: pointer can change, but pointed-to value cannot. Read right-to-left: `const int*` = pointer to const int.
What is undefined behavior (UB) and give two common C++ examples.
Undefined behavior: compiler makes no guarantees about outcome; program may crash, produce garbage, or appear to work. Examples: (1) accessing out-of-bounds array index, (2) dereferencing null or uninitialized pointers, (3) signed integer overflow.
Explain the `static` keyword when applied to a local variable inside a function.
`static` local variables are initialized once and retain their value between function calls. Memory is allocated at program start, not on stack. Useful for lazy initialization and maintaining state without global scope.
What does RAII mean and why is it important in C++?
Resource Acquisition Is Initialization: resources (memory, files, locks) are tied to object lifetime. Constructor acquires, destructor releases. Ensures resources are freed even if exceptions occur. Foundation of C++ safety and the reason `std::unique_ptr`, `std::lock_guard` exist.
Why is `std::vector` preferred over raw `new`/`delete`, and what is the gotcha with `reserve` vs `resize`?
`std::vector` is RAII and bounds-safe. `reserve(n)` allocates capacity but doesn't create elements; size unchanged. `resize(n)` creates/removes elements and updates size. Common gotcha: `reserve` doesn't let you access `[i]`; use `resize` or `push_back`.
Explain move semantics and `std::move`. Why does it matter?
Move semantics transfers ownership of resources (e.g., heap memory) instead of copying. `std::move` casts an lvalue to an rvalue, enabling move constructors/assignment. Avoids expensive copies. Gotcha: `std::move` does not move; it allows the compiler to move. Always use for large temporaries.
What are virtual functions, and what is the most dangerous gotcha with them?
Virtual functions enable runtime polymorphism: derived class overrides are called through base pointers/references. Dangerous gotcha: forgetting `virtual` in the base class means no dynamic dispatch—derived implementation never called. Also: virtual destructors are mandatory in polymorphic classes to prevent memory leaks.
Make your own C++ study set
Flashcards for related topics
Studying C++ to build with AI? MindloomHQ turns it into real skills — structured courses, agent projects, and certificates.
Explore MindloomHQ →