This segmenting of the memory space into discrete blocks with specific tasks carried over into the programming languages of the day and the concept is still widely in use within modern programming languages.
== Program memory ==
A computer program memory can be largely categorized into two sections: read-only and read/write. This distinction grew from early systems holding their main program in [[read-only memory]] such as [[Mask ROM]], [[EPROM]], [[Programmable read-only memory|PROM]] or [[EEPROM]]. As systems became more complex and programs were loaded from other media into RAM instead of executing from ROM, the idea that some portions of the program's memory should not be modified was retained. These became the ''.text'' and ''.rodata'' segments of the program, and the remainder which could be written to divided into a number of other segments for specific tasks.
===Text Code ===
{{main|Code segment}}
The '''code segment''', also known as a '''text segment ''' or simply as '''text''', is where a portion of an [[object file]] or the corresponding section of the program's [[address space]] that contains [[executable]] instructions is storedcode and is generally read-only and fixed size. ▼
▲The '''code segment''', also known as a '''text segment''' or simply as '''text''', is where a portion of an [[object file]] or the corresponding section of the program's [[address space]] that contains [[executable]] instructions is stored and is generally read-only and fixed size.
[[File:Program memory layout.pdf|thumb|383x383px|This shows the typical layout of a simple computer's program memory with the text, various data, and stack and heap sections.]]
The ''.'data segment''' segment contains any global orinitialized static variables, whichi.e. haveglobal a pre-defined valuevariables and canlocal be modified. That is anystatic variables thatwhich arehave nota defined within a functionvalue (and thus can be accessed from anywhere) or are defined in a function but are defined as ''static'' so they retain their address across subsequent callsmodified. Examples, in C, include:
char string[] = "Hello World"; ▼
The values for these variables are initially stored within the read-only memory (typically within ''.text'') and are copied into the ''.data'' segment during the start-up routine of the program. ▼
▲ char stringa[] = "Hello World";
▲The values for these variables are initially stored within the read-only memory (typically within ''.text''the code segment) and are copied into the ''.data '' segment during the start-up routine of the program.
Note that in the above example, if these variables had been declared from within a function, they would default to being stored in the local stack frame.
=== BSS ===
{{Main main| BSS segment}}
The '''BSS segment''', also known as ''uninitialized data'', iscontains usuallyuninitialized adjacent to thestatic data, segment.both Thevariables BSSand segmentconstants, contains alli.e. global variables and local static variables that are initialized to zero or do not have explicit initialization in source code. For instance, a variable defined as <code>static int i;</code> would be containedExamples in theC BSS segment.include:
int i;
char a[12];
The heap area commonly begins at the end of the .bss and .data segments and grows to larger addresses from there. The heap area is managed by [[malloc]], calloc, realloc, and free, which may use the [[Sbrk|brk]] and [[sbrk]] system calls to adjust its size (note that the use of brk/sbrk and a single "heap area" is not required to fulfill the contract of malloc/calloc/realloc/free; they may also be implemented using [[mmap]]/munmap to reserve/unreserve potentially non-contiguous regions of virtual memory into the process' [[virtual address space]]). The heap area is shared by all threads, shared libraries, and dynamically loaded modules in a process. ▼
===Stack Heap ===
{{Main main|Manual Callmemory stackmanagement}}
▲The '''heap areasegment''' contains dynamically allocated memory, commonly begins at the end of the .bssBSS and .data segmentssegment and grows to larger addresses from there. The heap areaIt is managed by [[malloc]], calloc, realloc, and free, which may use the [[Sbrk|brk]] and [[sbrk]] system calls to adjust its size (note that the use of brk/sbrk and a single "heap area"segment is not required to fulfill the contract of malloc/calloc/realloc/free; they may also be implemented using [[mmap]]/munmap to reserve/unreserve potentially non-contiguous regions of virtual memory into the process' [[virtual address space]]). The heap areasegment is shared by all threads, shared libraries, and dynamically loaded modules in a process.
The stack area contains the program [[Stack (data structure)|stack]], a [[LIFO (computing)|LIFO]] structure, typically located in the higher parts of memory. A "stack pointer" register tracks the top of the stack; it is adjusted each time a value is "pushed" onto the stack. The set of values pushed for one function call is termed a "stack frame". A stack frame consists at minimum of a return address. [[Automatic variable]]s are also allocated on the stack. ▼
{{main|Call stack}}
▲The '''stack areasegment''' contains the program [[ Stack (datacall structure)|stack]], a [[LIFO (computing)|LIFO]] structure, typically located in the higher parts of memory. A "stack pointer" register tracks the top of the stack; it is adjusted each time a value is "pushed" onto the stack. The set of values pushed for one function call is termed a "stack frame". A stack frame consists at minimum of a return address. [[Automatic variable]]s are also allocated on the stack.
The stack areasegment traditionally adjoined the heap areasegment and they grew towards each other; when the stack pointer met the heap pointer, free memory was exhausted. With large address spaces and virtual memory techniques they tend to be placed more freely, but they still typically grow in a converging direction. On the standard PC [[x86 architecture]] the stack grows toward address zero, meaning that more recent items, deeper in the call chain, are at numerically lower addresses and closer to the heap. On some other architectures it grows the opposite direction.
==Interpreted languages==
|