CPP Memory Management & Pointers 3 — Questions and Answers
Question 1: What is the role of std::weak_ptr in C++?
- To provide a non-owning reference that does not affect the reference count of a shared_ptr (Correct answer)
- To weakly enforce memory alignment for heap objects
- To act as a fallback when unique_ptr ownership transfer fails
- To observe stack-allocated objects safely
Correct answer: To provide a non-owning reference that does not affect the reference count of a shared_ptr
std::weak_ptr holds a non-owning reference to an object managed by shared_ptr without incrementing the reference count, helping break circular references.
Question 2: What is the difference between stack and heap memory in C++?
- Stack memory is manually managed; heap memory is automatic
- Stack memory is automatically managed and limited in size; heap memory is manually managed and larger (Correct answer)
- Stack memory stores global variables; heap stores local variables
- Stack memory is slower to access than heap memory
Correct answer: Stack memory is automatically managed and limited in size; heap memory is manually managed and larger
Stack memory is automatically managed by the compiler with a limited size, while heap memory is manually allocated/deallocated and can be much larger.
Question 3: Which scenario is most likely to cause a memory leak in C++?
- Using std::vector instead of raw arrays
- Allocating memory with new inside a function and returning without calling delete (Correct answer)
- Using nullptr to initialize a pointer
- Passing a pointer by reference to a function
Correct answer: Allocating memory with new inside a function and returning without calling delete
Allocating heap memory with new and not calling delete before the pointer goes out of scope causes a memory leak.
Question 4: What does new[] allocate and how must it be freed?
- A single object; freed with delete
- An array of objects; freed with delete[] (Correct answer)
- A block of raw bytes; freed with free()
- A vector; freed automatically
Correct answer: An array of objects; freed with delete[]
new[] allocates an array of objects and must be freed with delete[] to ensure all destructors are called and memory is properly released.
Question 5: What is the output of: int arr[3] = {10,20,30}; int* p = arr; std::cout << *(p+2);
- 10
- 20
- 30 (Correct answer)
- Undefined behavior
Correct answer: 30
p+2 advances the pointer by 2 integer positions, pointing to the third element (30), and dereferencing it yields 30.
Question 6: What does the RAII idiom guarantee in C++ memory management?
- Resources are allocated at runtime on demand
- Resources are acquired in a constructor and released in the corresponding destructor (Correct answer)
- Memory is reference-counted automatically for all objects
- Stack objects are moved to the heap when the scope ends
Correct answer: Resources are acquired in a constructor and released in the corresponding destructor
RAII (Resource Acquisition Is Initialization) ensures that resources are tied to object lifetimes, with acquisition in the constructor and release in the destructor.
Question 7: In C++, what is a void pointer (void*)?
- A pointer that points to nothing and cannot be used
- A generic pointer that can hold the address of any data type but must be cast before dereferencing (Correct answer)
- A pointer to a function returning void
- A pointer automatically initialized to nullptr
Correct answer: A generic pointer that can hold the address of any data type but must be cast before dereferencing
A void* can store the address of any type but cannot be dereferenced directly; it must be cast to the appropriate type first.
What is the role of std::weak_ptr in C++?