Memory management in C++ empowers developers with direct control over hardware resources, primarily through the allocation and deallocation of memory on the stack and the heap. While the stack automatically manages local variables, the heap requires explicit dynamic memory allocation using the new and delete operators. To mitigate risks like memory leaks and dangling pointers, modern C++ (C++11 and later) strongly advocates for the RAII (Resource Acquisition Is Initialization) idiom. This paradigm relies heavily on smart pointers such as std::unique_ptr, std::shared_ptr, and std::weak_ptr which automate memory cleanup through deterministic destructors. Understanding these mechanisms is crucial for writing efficient, robust, and safe C++ applications.
C++ Memory Management MCQs
1 min read
Correct Answer: a) Allocates memory and calls the constructor
Explanation:
The new operator allocates memory on the heap and then invokes the appropriate constructor for the object.
Correct Answer: b) delete
Explanation:
The delete operator is used to free memory that was allocated using the new operator, and it also calls the destructor.
Correct Answer: b) Stack
Explanation:
Local variables and function call frames are stored on the call stack, which is managed automatically by the compiler.
Correct Answer: c) std::bad_alloc
Explanation:
If the new operator cannot allocate the requested memory, it throws a std::bad_alloc exception by default.
Correct Answer: b) Use new(std::nothrow)
Explanation:
Using std::nothrow with the new operator makes it return a null pointer instead of throwing an exception if allocation fails.
Correct Answer: b) Allocated memory is never freed
Explanation:
A memory leak occurs when dynamically allocated memory is not properly deallocated, leading to wasted memory resources.
Correct Answer: b) A pointer that points to freed or deallocated memory
Explanation:
A dangling pointer occurs when memory is freed but the pointer still holds the address, leading to undefined behavior if accessed.
Correct Answer: c) Undefined behavior
Explanation:
Calling delete twice on the same pointer results in a double free error, which causes undefined behavior and often crashes the program.
Correct Answer: b) malloc
Explanation:
The malloc function allocates the specified number of bytes but leaves the memory uninitialized, containing garbage values.
Correct Answer: b) calloc
Explanation:
The calloc function allocates memory for an array of elements and initializes all bytes in the allocated storage to zero.
Correct Answer: b) Changes the size of a previously allocated memory block
Explanation:
The realloc function attempts to resize the memory block pointed to by ptr to the given size, preserving existing data when possible.
Correct Answer: c) cstdlib
Explanation:
The cstdlib header provides the declarations for C-style memory management functions like malloc, calloc, realloc, and free.
Correct Answer: b) new calls constructors, malloc does not
Explanation:
The new operator not only allocates memory but also invokes the object's constructor, whereas malloc only allocates raw memory.
Correct Answer: b) Resource Acquisition Is Initialization
Explanation:
RAII is a programming idiom where resource allocation is tied to object lifetime, ensuring resources are released when the object is destroyed.
Correct Answer: c) std::unique_ptr
Explanation:
std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of it when the unique_ptr goes out of scope, enforcing exclusive ownership.
Correct Answer: b) std::shared_ptr
Explanation:
std::shared_ptr uses reference counting to allow multiple smart pointers to own the same resource, freeing it when the count reaches zero.
Correct Answer: c) std::weak_ptr
Explanation:
std::weak_ptr holds a non-owning reference to an object managed by std::shared_ptr, preventing reference cycles that cause memory leaks.
Correct Answer: b) Using std::make_unique
Explanation:
std::make_unique is the recommended way to create a unique_ptr as it provides exception safety and avoids code duplication.
Correct Answer: b) It allocates the object and control block in a single memory allocation
Explanation:
std::make_shared improves performance and memory locality by allocating both the object and its reference counting control block in one operation.
Correct Answer: b) If a class needs a custom destructor, it likely needs a custom copy constructor and copy assignment operator
Explanation:
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it almost certainly requires all three.
Correct Answer: a) Move constructor and move assignment operator
Explanation:
The Rule of Five extends the Rule of Three by adding the move constructor and move assignment operator to handle resource transfer efficiently.
Correct Answer: b) Classes should rely on the compiler-generated special member functions by using smart pointers and standard containers
Explanation:
The Rule of Zero suggests that classes should not define any of the special member functions if they manage resources using RAII-compliant types like smart pointers.
Correct Answer: b) Constructing an object in pre-allocated memory
Explanation:
Placement new allows you to construct an object at a specific, pre-allocated memory address without allocating new memory.
Correct Answer: c) Explicitly call the destructor
Explanation:
Since placement new does not allocate memory, you must explicitly call the object's destructor to clean it up, and then manually free the underlying memory if needed.
Correct Answer: b) The default memory allocator used by standard library containers
Explanation:
std::allocator is the default allocator used by STL containers to handle memory allocation and deallocation for their elements.
Correct Answer: b) Placing data in memory at addresses that are multiples of the data size
Explanation:
Memory alignment ensures that data is stored at memory addresses that are optimal for the CPU to access, improving performance.
Correct Answer: b) Inserting empty bytes between struct members to satisfy alignment requirements
Explanation:
Structure padding adds unused bytes between members to ensure each member is properly aligned in memory, which can increase the overall size of the struct.
Correct Answer: c) Make the new and delete operators private or deleted
Explanation:
By making the new and delete operators private or explicitly deleting them, the compiler will prevent objects of the class from being allocated on the heap.
Correct Answer: a) A large block of memory divided into smaller, fixed-size chunks for fast allocation
Explanation:
A memory pool pre-allocates a large block of memory and divides it into fixed-size chunks, allowing for very fast allocation and deallocation of objects.
Correct Answer: b) Excessive recursion or allocating very large local arrays
Explanation:
Stack overflow occurs when the call stack exceeds its maximum size, usually due to deep recursion or allocating large arrays as local variables.
Correct Answer: c) Does nothing safely
Explanation:
Calling delete on a null pointer is perfectly safe in C++; the standard specifies that it simply does nothing.
Correct Answer: a) delete is for single objects, delete[] is for arrays
Explanation:
The delete operator is used to free memory for a single object, while delete[] is used to free memory allocated for an array of objects.
Correct Answer: c) Undefined behavior
Explanation:
Using delete instead of delete[] on an array results in undefined behavior because the destructors for the remaining elements are not called and memory may not be freed correctly.
Correct Answer: b) memory
Explanation:
The header contains the definitions for smart pointers like std::unique_ptr, std::shared_ptr, and std::weak_ptr.
Correct Answer: b) The reference count, weak count, and custom deleter
Explanation:
The control block of a shared_ptr manages the reference counts (strong and weak) and stores the custom deleter if one is provided.
Correct Answer: c) No, it must be locked to a shared_ptr first
Explanation:
A weak_ptr cannot be dereferenced directly; you must call its lock() method to obtain a shared_ptr, which can then be safely dereferenced.
Correct Answer: b) A std::shared_ptr if the object exists, or an empty shared_ptr if expired
Explanation:
The lock() method attempts to create a shared_ptr from the weak_ptr, returning an empty shared_ptr if the managed object has already been destroyed.
Correct Answer: b) To transfer resources from one object to another without copying
Explanation:
Move semantics allow resources (like dynamically allocated memory) to be transferred from a temporary object to a new object, avoiding expensive deep copies.
Correct Answer: b) Casts its argument to an rvalue reference
Explanation:
std::move does not move anything itself; it simply casts its argument to an rvalue reference, enabling the compiler to invoke move constructors or move assignment operators.
Correct Answer: a) The compiler omitting copy or move constructors in certain situations
Explanation:
Copy elision is an optimization where the compiler omits copy and move constructors, constructing the object directly in the target memory location.
Correct Answer: a) Return Value Optimization
Explanation:
RVO is a compiler optimization technique that eliminates unnecessary copying of objects when they are returned by value from a function.
Correct Answer: a) Named Return Value Optimization
Explanation:
NRVO is a specific form of RVO where the returned object is a named local variable, allowing the compiler to construct it directly in the caller's memory.
Correct Answer: b) A user-defined function or lambda used to release the managed resource
Explanation:
A custom deleter allows you to specify how the managed resource should be released, useful for resources that require specific cleanup functions like fclose or free.
Correct Answer: b) Yes, it can take a custom deleter as a template parameter or constructor argument
Explanation:
std::unique_ptr supports custom deleters, which are stored as part of the unique_ptr object and invoked when the resource is released.
Correct Answer: b) No, you must construct the unique_ptr directly to use a custom deleter
Explanation:
std::make_unique does not support custom deleters; to use a custom deleter with unique_ptr, you must construct it directly using the new operator or a raw pointer.
Correct Answer: b) 1 byte
Explanation:
An empty class in C++ has a size of at least 1 byte to ensure that every object has a unique memory address.
Correct Answer: b) To ensure that two distinct objects have distinct memory addresses
Explanation:
If an empty class had a size of 0, an array of such objects would have all elements at the same address, violating the requirement that distinct objects have distinct addresses.
Correct Answer: b) It allows containers to use move operations safely without risking exceptions during reallocation
Explanation:
Marking move constructors as noexcept guarantees they won't throw, allowing standard containers to use them during reallocation, which is faster and provides strong exception safety.
Correct Answer: b) The program terminates immediately
Explanation:
If a function marked noexcept throws an exception, the C++ runtime calls std::terminate, immediately ending the program.
Correct Answer: b) To adjust a pointer to a specified alignment boundary
Explanation:
std::align adjusts a pointer to the next address that satisfies a specified alignment requirement, reducing the available space accordingly.
Correct Answer: b) An allocator that manages memory in fixed-size chunks or slabs
Explanation:
A slab allocator pre-allocates memory in large slabs and then divides them into fixed-size chunks, providing fast allocation for objects of a specific size.
Correct Answer: b) To provide a standardized interface for querying and using custom allocators
Explanation:
std::allocator_traits provides a uniform way to access the properties and operations of any allocator, filling in default behaviors if the allocator doesn't define them.
Correct Answer: b) Creating an allocator for a different type from an existing allocator
Explanation:
The rebind mechanism allows an allocator designed for one type to be used to allocate memory for a different type, which is essential for containers like std::list.
Correct Answer: b) An instruction that enforces an ordering constraint on memory operations
Explanation:
A memory fence ensures that memory operations before the fence are completed before any operations after the fence, crucial for thread synchronization.
Correct Answer: b) Organizing data in memory so that related data is stored close together
Explanation:
Cache locality refers to accessing data that is physically close in memory, which increases the likelihood of cache hits and improves performance.
Correct Answer: c) It significantly improves performance by reducing cache misses
Explanation:
Good cache locality ensures that data needed together is loaded into the CPU cache together, reducing slow main memory accesses and speeding up execution.
Correct Answer: b) Two threads modifying independent variables that reside on the same cache line
Explanation:
False sharing occurs when threads on different cores modify variables that share the same cache line, causing unnecessary cache invalidations and performance degradation.
Correct Answer: b) By aligning thread-local data to the cache line size
Explanation:
Aligning data structures to the cache line size ensures that each thread modifies its own cache line, preventing false sharing.
Correct Answer: b) Specifies the alignment requirement for a variable or type
Explanation:
The alignas specifier is used to specify a custom alignment requirement for a variable, class, or struct, overriding the default alignment.
Correct Answer: b) The alignment requirement of the type in bytes
Explanation:
The alignof operator returns the alignment requirement of the specified type or expression, which is the number of bytes between successive addresses at which the object can be allocated.
Correct Answer: b) A file whose contents are mapped directly into the virtual address space of a process
Explanation:
A memory-mapped file allows a process to access the file's contents directly in memory as if it were an array, improving I/O performance.
Correct Answer: b) mmap
Explanation:
The mmap function is used in POSIX systems to map files or devices into memory, allowing direct memory access to file contents.
Correct Answer: b) An abstraction that gives each process the illusion of a large, contiguous block of memory
Explanation:
Virtual memory is a memory management technique where the OS uses hardware to map virtual addresses used by a process to physical addresses in RAM or on disk.
Correct Answer: b) An interrupt raised when a program accesses a memory page that is not currently in physical RAM
Explanation:
A page fault occurs when a program tries to access a block of memory that is mapped in the virtual address space but not currently loaded in physical RAM, prompting the OS to load it.
Correct Answer: b) Excessive paging activity where the OS spends more time swapping pages than executing processes
Explanation:
Thrashing occurs when the system's virtual memory subsystem is constantly swapping pages in and out of RAM, severely degrading performance.
Correct Answer: b) The new expression allocates memory and calls the constructor; operator new only allocates memory
Explanation:
The new expression is a language feature that calls operator new to allocate raw memory and then invokes the constructor, while operator new is just the memory allocation function.
Correct Answer: b) By providing a static member function named operator new in the class
Explanation:
You can overload operator new for a specific class by defining it as a static member function, which will be called when objects of that class are allocated with new.
Correct Answer: b) void* operator new(size_t size)
Explanation:
The global operator new function takes a size_t parameter representing the number of bytes to allocate and returns a void pointer to the allocated memory.
Correct Answer: b) Allocating memory for an array of objects
Explanation:
operator new[] is called by the new[] expression to allocate raw memory for an array of objects before their constructors are invoked.
Correct Answer: b) A mechanism to prevent the compiler and CPU from reordering memory operations across the barrier
Explanation:
A memory barrier ensures that memory operations issued before the barrier are completed before any operations after the barrier, maintaining correct execution order in concurrent programs.
Correct Answer: b) A template class that provides atomic operations for fundamental types
Explanation:
std::atomic is a template class that ensures operations on the contained value are performed atomically, making them thread-safe without explicit locks.
Correct Answer: b) It ensures thread-safe access to shared memory without data races
Explanation:
std::atomic provides thread-safe operations on shared memory locations, preventing data races and ensuring consistent state across multiple threads.
Correct Answer: b) A constraint on how memory operations around atomic operations are ordered and visible to other threads
Explanation:
Memory order specifies the synchronization and ordering constraints on memory operations, controlling how and when changes to atomic variables become visible to other threads.
Correct Answer: b) No synchronization or ordering constraints, only atomicity is guaranteed
Explanation:
memory_order_relaxed provides atomicity and ordering guarantees only for the atomic variable itself, with no synchronization constraints on surrounding memory operations.
Correct Answer: b) Sequential consistency, providing the strongest ordering guarantees
Explanation:
memory_order_seq_cst ensures that all threads see all modifications in the same order, providing the strictest synchronization and ordering guarantees.
Correct Answer: b) A compiler or runtime tool that detects memory errors like buffer overflows and use-after-free
Explanation:
Memory sanitizers are tools that instrument code to detect various memory-related errors during execution, helping to identify bugs that cause crashes or security vulnerabilities.
Correct Answer: b) Buffer overflows, use-after-free, and memory leaks
Explanation:
AddressSanitizer is a fast memory error detector that finds out-of-bounds accesses, use-after-free bugs, and memory leaks.
Correct Answer: b) Reads of uninitialized memory
Explanation:
MemorySanitizer is a detector of uninitialized memory reads, which can lead to unpredictable behavior and security vulnerabilities.
Correct Answer: c) Memory leaks
Explanation:
LeakSanitizer is a runtime memory leak detector that identifies dynamically allocated memory that was not properly freed before the program exited.
Correct Answer: b) A programming tool for memory debugging, memory leak detection, and profiling
Explanation:
Valgrind is an instrumentation framework for building dynamic analysis tools, commonly used to detect memory management and threading bugs in C++ programs.
Correct Answer: b) new is type-safe and returns a correctly typed pointer, while malloc returns void*
Explanation:
The new operator returns a pointer of the correct type, ensuring type safety, whereas malloc returns a void pointer that must be explicitly cast, which can lead to type errors.
Correct Answer: c) void*
Explanation:
The malloc function returns a void pointer (void*) to the allocated memory, which must be cast to the appropriate pointer type in C (though not required in C++).
Correct Answer: b) A pointer to the allocated type
Explanation:
The new operator returns a pointer of the exact type being allocated, providing type safety without the need for explicit casting.
Correct Answer: b) Extra memory to store the strong reference count, weak reference count, and deleter
Explanation:
The control block of a shared_ptr requires additional memory to manage reference counts and store the custom deleter, adding overhead compared to raw pointers.
Correct Answer: b) By using std::make_shared to allocate the object and control block together
Explanation:
std::make_shared reduces memory overhead and improves locality by allocating the object and its control block in a single memory allocation, rather than two separate ones.
Correct Answer: b) A smart pointer where the reference count is stored inside the managed object itself
Explanation:
In an intrusive smart pointer, the managed object contains the reference count as a member, eliminating the need for a separate control block and reducing memory overhead.
Correct Answer: b) boost::intrusive_ptr
Explanation:
The Boost library provides boost::intrusive_ptr, an intrusive reference-counting smart pointer where the reference count is managed by the object itself.
Correct Answer: b) To provide a strict weak ordering for weak_ptr and shared_ptr based on ownership, not value
Explanation:
std::owner_less is a function object that compares shared_ptr and weak_ptr based on their ownership (control block address) rather than the value of the managed object, useful for associative containers.
Correct Answer: b) Allow the object to safely generate a std::shared_ptr to itself
Explanation:
Inheriting from std::enable_shared_from_this allows an object to create a shared_ptr to itself, ensuring that the reference count is correctly updated and the object is not deleted prematurely.
Correct Answer: b) It throws a std::bad_weak_ptr exception
Explanation:
If shared_from_this() is called when no shared_ptr currently owns the object, it throws a std::bad_weak_ptr exception because the internal weak_ptr is empty.
Correct Answer: b) A large contiguous block of memory from which smaller allocations are made
Explanation:
A memory arena is a large, pre-allocated block of memory used to satisfy many smaller allocation requests, simplifying memory management and improving performance.
Correct Answer: b) Fast allocation and the ability to free all allocated memory at once
Explanation:
Memory arenas provide very fast allocation by simply incrementing a pointer, and allow all allocated memory to be freed simultaneously by resetting the arena, avoiding individual deallocation overhead.
Correct Answer: b) A simple allocator that satisfies allocations by incrementing a pointer
Explanation:
A bump allocator (or linear allocator) satisfies memory requests by simply moving a pointer forward, making it extremely fast but unable to free individual allocations.
Correct Answer: b) It cannot free individual objects, only the entire arena
Explanation:
Because a bump allocator only moves a pointer forward, it has no mechanism to track and free individual allocations; memory can only be reclaimed by resetting the entire arena.
Correct Answer: a) Polymorphic Memory Resource
Explanation:
std::pmr stands for Polymorphic Memory Resource, a library that provides a unified interface for different memory allocation strategies, allowing containers to use custom allocators seamlessly.
Correct Answer: b) A memory resource that never frees individual allocations until the resource itself is destroyed
Explanation:
monotonic_buffer_resource is a type of memory arena that allocates memory monotonically and only releases all memory when the resource object is destroyed, making it very fast for temporary allocations.
Correct Answer: b) A memory pool that is not thread-safe and intended for single-threaded use
Explanation:
unsynchronized_pool_resource is a memory pool that does not use internal synchronization, making it faster for single-threaded applications but unsafe for concurrent access.
Correct Answer: b) std::pmr::new_delete_resource
Explanation:
The default memory resource for std::pmr containers is std::pmr::new_delete_resource, which uses the standard global new and delete operators for memory allocation and deallocation.
Related Posts
New
NewNew

C++ Constructors and Destructors MCQs
In C++, constructors and destructors are special member functions critical for the Resource Acquisition Is Initialization (RAII) paradigm and safe…
October 20, 2026By MCQs Generator
No Image
400+ C++ Interview Questions MCQ
1. What does the 'new' operator do in C++? a) Allocates memory and calls the constructor b) Allocates memory without…
October 19, 2026By MCQs Generator
No Image
C++ Arrays / Lists - MCQs
1. What does the 'new' operator do in C++? a) Allocates memory and calls the constructor b) Allocates memory without…
October 20, 2026By MCQs Generator








