Assess your understanding of memory allocation, dynamic memory management, and related concepts with these carefully crafted questions. This quiz covers essential principles, terminology, and scenarios to deepen your knowledge of memory allocation in computer systems.
Which of the following statements accurately describes a primary difference between stack and heap memory allocation in programming?
Explanation: Stack allocation leverages automatic memory management tied to function entry and exit, making it quick and efficient. Heap allocation involves dynamic memory management, requiring explicit requests and releases from the programmer, which can be slower. Saying heap allocation always uses less memory is misleading, as usage depends on program design. Both memories are not managed solely by hardware—heap often needs software intervention. The claim that heap is faster than stack is typically false since the heap requires more overhead.
A program allocates memory dynamically but fails to release it after use. What is this issue called, and what is a possible consequence?
Explanation: Failing to release dynamically allocated memory is termed a memory leak, which can gradually consume available memory and degrade performance or lead to program failure. Buffer overflows relate to writing past allocated space, not failing to free memory. Segmentation faults stem from accessing invalid memory, not memory leaks. Stack underflow is unrelated and concerns incorrect stack manipulation, typically with local variables.
In memory allocation, what is external fragmentation, and when does it commonly occur?
Explanation: External fragmentation divides free memory into scattered pieces, making it difficult to allocate larger blocks even if total free memory is sufficient—this is common in dynamic heap allocation. The second option confuses stack overflow (not related to fragmentation). Option three is false as fragmentation can arise with variable-size allocations too. The final option misrepresents the term, as external fragmentation is about memory layout, not address space.
Suppose a programming language provides 'allocate' and 'deallocate' functions for managing heap memory. What must a programmer do to prevent dangling pointers when releasing memory?
Explanation: To prevent dangling pointers—pointers that still reference deallocated memory—the programmer should set such pointers to null post-deallocation. Reusing the pointer as-is may cause undefined behavior. Allocating new memory before releasing the old one does not resolve whether existing pointers might dangle. Ignoring the pointer leaves it in a potentially dangerous state, as later dereferencing could cause errors.
If a variable's size and lifetime are determined at compile time and do not change during execution, which memory allocation method is most appropriate for it?
Explanation: For variables with known size and lifetime at compile time, static allocation is the correct choice, providing efficient, predictable resource availability. Dynamic allocation is suited for varying sizes or uncertain lifetimes. Garbage collection is unrelated to compile-time allocation and can introduce overhead. Not all data belongs on the heap—heap is for dynamic cases, not for fixed-size, persistent variables.