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
What is a dangling pointer in C++?
Answer: A pointer that points to memory that has been freed or gone out of scope
A dangling pointer refers to memory that has already been deallocated or is no longer valid, leading to undefined behavior if dereferenced.
Which smart pointer transfers ownership when assigned to another smart pointer of the same type?
Answer: std::unique_ptr
std::unique_ptr uses move semantics, so assigning it to another unique_ptr transfers ownership and sets the source to nullptr.
What does the placement new operator do in C++?
Answer: Constructs an object at a pre-allocated memory address
Placement new constructs an object at a specific, pre-allocated memory location without performing any additional allocation.
What is the purpose of std::allocator in C++?
Answer: To provide a customizable memory allocation strategy for containers
std::allocator is the default allocator used by STL containers, providing a customizable interface for memory allocation and deallocation.
What happens when you delete a pointer that was not allocated with new?
Answer: It is undefined behavior
Deleting a pointer that wasn't allocated with new is undefined behavior and can corrupt the heap or crash the program.
Which of the following correctly declares a pointer to a constant integer?
Answer: const int* ptr
const int* ptr (or int const* ptr) declares a pointer to a constant integer, meaning the value pointed to cannot be modified through the pointer.
What is the result of dereferencing a nullptr in C++?
Answer: Undefined behavior, typically a segmentation fault
Dereferencing nullptr is undefined behavior, commonly manifesting as a segmentation fault or access violation at runtime.