0
Explore
0

Compaction, Overflow and Underflow in Array

Updated on January 4, 2026

Arrays can face issues like compaction, overflow, and underflow during operations. These situations occur when elements are rearranged, when the array exceeds its capacity, or when elements are removed from an empty structure. Understanding these concepts helps in managing arrays efficiently and avoiding common runtime errors.

Compaction

Garbage collection involves reclaiming unused memory nodes and returning them to the available memory pool. This process typically consists of two phases: the marking phase, where all active nodes are identified, and the compaction phase, where free memory partitions are consolidated into contiguous blocks that can be allocated to new processes.

Compaction is the process of relocating all allocated memory blocks to one end of memory, creating a single large contiguous free space at the other end. This consolidated free space can then be efficiently allocated to new processes.

  • Compaction addresses fragmentation by consolidating all allocated memory blocks at one end of memory.
  • It primarily reduces external fragmentation by reorganizing memory contents to combine all free spaces into a single large block.
  • The process merges scattered free spaces into one continuous block of available memory.
  • Compaction requires significant CPU resources due to the need to move and update memory references.
  • Instead of numerous small free spaces scattered throughout memory, compaction creates one large free space suitable for new allocations.
  • The system maintains relocation information to update references to moved memory blocks, ensuring program correctness after compaction.

Example: Consider an array A = [3, 5, -, -, 8, 9], where - represents unused memory locations. After compaction, the array becomes A = [3, 5, 8, 9, -, -], with all active elements moved to the beginning and free space consolidated at the end.

Overflow

Overflow occurs when attempting to insert new data into a data structure that has reached its maximum capacity, with no available space for additional elements.

Example: Given an array arr = [3, 6, 1, 8, 3] with a fixed size of 5, attempting to insert another element (such as 9) would cause overflow, as all array positions are occupied.

In programming, overflow handling typically involves:

  • Creating a larger array
  • Copying existing elements to the new array
  • Adding the new element to the expanded storage
  • Deallocating the original array (if necessary)

Underflow

Underflow occurs when attempting to remove an element from an empty data structure. Since there are no elements to delete, the operation cannot be performed. It means that given data structure is empty so if empty then there is no any element is available to delete, this situation is called underflow. Underflow is less commonly discussed but is important in the context of data structures like stacks and queues.

This situation commonly occurs in:

  • Stacks: When attempting to pop from an empty stack
  • Queues: When attempting to dequeue from an empty queue
  • Arrays/Lists: When attempting to remove from an empty collection

Although underflow receives less attention than overflow, it’s equally important to handle in practical implementations to prevent program crashes or undefined behavior.

Example: Given an empty array arr = [], attempting to remove an element would result in underflow, as there are no elements present.

Proper underflow prevention involves checking whether the data structure is empty before attempting removal operations.

Summary

  • Compaction: Memory optimization technique that consolidates free space by relocating allocated blocks
  • Overflow: Condition that occurs when inserting into a full data structure
  • Underflow: Condition that occurs when removing from an empty data structure

Proper management of these conditions is essential in algorithm design and implementation, particularly in memory-constrained environments and when working with fixed-size data structures.