← All CPP Flashcard Decks

Memory Management & Pointers Flashcards

7 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.

Read the first 7 Memory Management & Pointers flashcards as text
  1. What is a memory pool and why is it used in C++?

    Answer: A pre-allocated block of memory used to serve allocation requests faster than the general heap

    A memory pool pre-allocates a large block of memory and serves object allocations from it, reducing fragmentation and improving allocation speed.

  2. What is the effect of using delete on a pointer twice (double free)?

    Answer: Undefined behavior, often causing heap corruption or a crash

    Calling delete twice on the same pointer is undefined behavior and typically causes heap corruption or a program crash.

  3. Which C++ feature allows a class to define custom behavior for the new and delete operators?

    Answer: Operator overloading of new and delete

    C++ allows classes to overload operator new and operator delete to implement custom memory allocation strategies.

  4. What is the output of: int x = 5; int& ref = x; ref = 10; std::cout << x;

    Answer: 10

    ref is a reference to x, so assigning 10 to ref modifies x directly, and outputting x yields 10.

  5. How does std::make_shared differ from directly calling new with shared_ptr?

    Answer: make_shared performs a single allocation for the object and control block; new with shared_ptr performs two

    std::make_shared allocates the object and the reference-count control block in a single memory allocation, improving cache locality and performance.

  6. What does the const keyword mean when applied to a pointer itself (e.g., int* const ptr)?

    Answer: The pointer address cannot be changed after initialization

    int* const ptr declares a constant pointer — the address stored in ptr cannot be changed, but the value at that address can be modified.

  7. What is heap fragmentation and how does it affect C++ programs?

    Answer: It occurs when many small allocations and deallocations leave non-contiguous free blocks, reducing effective available memory

    Heap fragmentation occurs when repeated allocations and deallocations leave the heap with many small, scattered free blocks that may be unusable for larger allocations.