Memory Allocation Fundamentals Quiz Quiz

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.

  1. Stack vs. Heap Allocation

    Which of the following statements accurately describes a primary difference between stack and heap memory allocation in programming?

    1. Heap allocation is generally faster than stack allocation due to its simplicity.
    2. Stack allocation is generally faster because memory is managed automatically with function calls, while heap allocation requires explicit management.
    3. Stack and heap memory are both managed automatically by the hardware.
    4. Heap allocation always uses less memory than stack allocation.

    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.

  2. Dynamic Memory Errors

    A program allocates memory dynamically but fails to release it after use. What is this issue called, and what is a possible consequence?

    1. This is called stack underflow and only affects local variables.
    2. This is a segmentation fault, always resulting in data corruption.
    3. This is called a memory leak and can eventually cause the program to run out of usable memory.
    4. This is known as a buffer overflow and causes immediate program crashes.

    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.

  3. Fragmentation Types

    In memory allocation, what is external fragmentation, and when does it commonly occur?

    1. External fragmentation occurs only with fixed-size memory blocks and never with dynamic allocation.
    2. External fragmentation occurs when free memory is divided into small non-contiguous blocks that prevent allocating large contiguous spaces, often found in dynamic heaps.
    3. External fragmentation is when variables are stored outside the program's address space.
    4. External fragmentation happens when a program exceeds the stack size limit, causing a crash.

    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.

  4. Memory Allocation Functions

    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?

    1. Set any pointers that reference the released memory to null immediately after deallocation.
    2. Allocate new memory before releasing the existing one.
    3. Ignore the pointer after deallocating the memory.
    4. Reuse the deallocated pointer without any changes.

    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.

  5. Static vs. Dynamic Allocation

    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?

    1. Heap allocation, because all data should go on the heap.
    2. Garbage-collected allocation, because it always improves performance.
    3. Dynamic allocation, since it handles unpredictable sizes at runtime.
    4. Static allocation is appropriate, as it reserves space at compile time for the entire program duration.

    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.