Run-Time Memory Organization in Programs: Logical vs. Physical Addresses

Understand how memory is organized during program execution, including the mapping between logical and physical addresses. This guide explains different memory sections (code, data, stack, heap), their roles, and how the operating system manages memory allocation and deallocation.



Run-Time Memory Organization in Programs

Logical and Physical Address Spaces

When a program runs, it operates within its own logical address spaceā€”a set of addresses that the program uses to refer to its data and instructions. This logical address space is independent of the physical memory (RAM) locations where the program's data and instructions are actually stored. The operating system (OS) plays a key role in mapping this logical address space to a physical address space. The physical addresses refer to the actual RAM locations where data is stored. Both the compiler and the target machine are involved in this process.

Subdivision of Run-Time Memory

Run-time memory is organized into addressable units (bytes). Larger data structures (multiple bytes) are stored in consecutive memory locations. The address of the first byte is used as the reference point for the entire data structure.

Memory Areas for Different Data Types

Run-time memory typically has different sections for various data:

  • Executable Code: The program's machine-readable instructions.
  • Static Data: Data with a fixed size, allocated before runtime (e.g., global variables).
  • Dynamic Data (Heap): Data whose size changes during runtime. Requires manual memory management (e.g., using `malloc` and `free` in C).
  • Automatic Data (Stack): Memory for local variables and function calls; managed automatically during execution (using a stack data structure).