CPP Memory Management & Pointers 2 — Questions and Answers
Question 1: What is a dangling pointer in C++?
- A pointer that has never been initialized
- A pointer that points to memory that has been freed or gone out of scope (Correct answer)
- A pointer declared with the nullptr keyword
- A pointer that points to a null address
Correct 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.
Question 2: Which smart pointer transfers ownership when assigned to another smart pointer of the same type?
- std::shared_ptr
- std::weak_ptr
- std::unique_ptr (Correct answer)
- std::auto_ptr
Correct 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.
Question 3: What does the placement new operator do in C++?
- Allocates memory on the heap only
- Constructs an object at a pre-allocated memory address (Correct answer)
- Replaces an existing object in place without reallocation
- Allocates aligned memory automatically
Correct 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.
Question 4: What is the purpose of std::allocator in C++?
- To track memory leaks at runtime
- To provide a customizable memory allocation strategy for containers (Correct answer)
- To automatically free memory when it goes out of scope
- To align heap allocations to cache lines
Correct 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.
Question 5: What happens when you delete a pointer that was not allocated with new?
- The program silently ignores the delete
- It is undefined behavior (Correct answer)
- A std::bad_alloc exception is thrown
- The memory is returned to the OS safely
Correct 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.
Question 6: Which of the following correctly declares a pointer to a constant integer?
- int* const ptr
- const int* ptr (Correct answer)
- int const* const ptr
- volatile int* ptr
Correct 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.
Question 7: What is the result of dereferencing a nullptr in C++?
- Returns 0
- Returns an uninitialized value
- Throws a std::null_ptr_exception
- Undefined behavior, typically a segmentation fault (Correct answer)
Correct answer: Undefined behavior, typically a segmentation fault
Dereferencing nullptr is undefined behavior, commonly manifesting as a segmentation fault or access violation at runtime.
What is a dangling pointer in C++?