Copyright © 2023 by
Rance D. Necaise
|
|
3.3 Program MemoryIf all references can be resolved, the linker will produce an executable program containing machine code instructions. Before it can be executed, however, it must be read from the external storage and loaded into memory. A third program, called a loader is used to load a program into memory, set the machine registers, and initialize the program counter. The loader is part of the operating system. When a program is loaded into memory, the operating system allocates it a specific amount of memory that comprises the programs address space. A program's memory is divided into three segments, with each segment having a specific purpose. While the layout of these segments can vary from one system to the next, modern computers use a layout similar to that shown in Figure 3.3.1. ![]() Figure 3.3.1: Common layout for a program's memory.
The text segment near the bottom holds the program's executable instructions. The data segment, which is just above the text segment, is comprised of two parts. The static data part contains data that is statically allocated during compile or assembly time. This is the storage area for global variables, the data that exist during the entire lifetime of the program execution. The dynamic data part, also referred to as the heap, contains data that is allocated during execution of the program. This is the area where dynamic variables and the instance variables of objects are allocated as the program executes. The stack segment, which resides at the top of the program's address space, contains the program stack that is used to store local variables and to manage function calls. The heap and the stack both grow and shrink as the program executes. As "dynamic memory" is allocated, the heap expands upwards and as data is pushed on to the stack, it expands downwards. This organization of memory allows the two expandable segments to be placed as far apart as possible so they can grow to use the program's entire address space. If a program uses its entire memory, the dynamic data and stack segment will collide, resulting in a stack-heap collision error. The reserved area at the low end of memory contains instructions that allow programs to make system calls in order to utilize system resources. We will discuss the use of this area in more detail later in the chapter. Modern operating systems allocate virtual memory to a program, one block or page at a time as needed. The blocks of virtual memory can be placed anywhere within the physical memory of a computer. This allows operating systems to more easily manage the execution of multiple applications at the same time by one or more users. In a later chapter, we will explore the construction and use of physical memory and the role of the operating system in the allocation of memory.
|