Content deleted Content added
mNo edit summary |
Make clear link is for Ruby |
||
(47 intermediate revisions by 40 users not shown) | |||
Line 1:
{{Short description|Storage segment}}
In [[computing]], a '''data segment''' (often denoted '''.data''') is a portion of an [[object file]] or the corresponding [[
The data segment is read
▲In [[computing]], a '''data segment''' (often denoted '''.data''') is a portion of an [[object file]] or the corresponding [[virtual address space]] of a program that contains initialized [[static variable]]s, that is, [[global variable]]s and [[static local variable]]s. The size of this segment is determined by the size of the values in the program's source code, and does not change at [[Run time (program lifecycle phase)|run time]].
Historically, to be able to support memory address spaces larger than the native size of the internal address register would allow, early CPUs implemented a system of segmentation whereby they would store a small set of indexes to use as offsets to certain areas. The [[Intel 8086]] family of CPUs provided four segments: the code segment, the data segment, the stack segment and the extra segment. Each segment was placed at a specific ___location in memory by the software being executed and all instructions that operated on the data within those segments were performed relative to the start of that segment. This allowed a 16-bit address register, which would normally
▲The data segment is read-write, since the values of variables can be altered at run time. This is in contrast to the ''read-only data segment'' (''{{visible anchor|rodata}} segment'' or ''.rodata''), which contains static constants rather than variables; it also contrasts to the [[code segment]], also known as the text segment, which is read-only on many architectures. Uninitialized data, both variables and constants, is instead in the [[BSS segment]].
▲Historically, to be able to support memory address spaces larger than the native size of the internal address register would allow, early CPUs implemented a system of segmentation whereby they would store a small set of indexes to use as offsets to certain areas. The [[Intel 8086]] family of CPUs provided four segments: the code segment, the data segment, the stack segment and the extra segment. Each segment was placed at a specific ___location in memory by the software being executed and all instructions that operated on the data within those segments were performed relative to the start of that segment. This allowed a 16-bit address register, which would normally provide 64KiB (65536 bytes) of memory space, to access a 1MiB (1048576 bytes) address space.
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]], [[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.▼
===Data===▼
[[File:Typical computer data memory arrangement.png|150px|thumb|This shows the typical layout of a simple computer's program memory with the text, various data, and stack and heap sections.]]▼
▲== Program memory ==
int val = 3;▼
▲A computer program memory can be largely categorized into two sections: read-only and read
char string[] = "Hello World";▼
{{main|Code segment}}
The '''code segment''', also known as '''text segment''', contains [[executable]] code and is generally read-only and fixed size.
===
▲[[File:
The [[BSS segment]], also known as ''uninitialized data'', is usually adjacent to the data segment. The BSS segment contains all global variables and 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 contained in the BSS segment.▼
The '''data segment''' contains initialized static variables, i.e. global variables and local static variables which have a defined value and can be modified. Examples in C include:
<syntaxhighlight lang="c">
===Heap===▼
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.▼
static int b = 2023; // Initialized static global variable
void foo (void) {
static int c = 2023; // Initialized static local variable
}
</syntaxhighlight>
===
{{
▲The
<syntaxhighlight lang="c">
static int i;
static char a[12];
</syntaxhighlight>
▲=== Heap ===
{{main|Manual memory management}}
▲The '''heap
=== Stack ===
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
The stack
==Interpreted languages==
Some interpreted languages offer a similar facility to the data segment, notably [[Perl]]<ref>[http://perldoc.perl.org/perldata.html#Special-Literals perldata: Special Literals]</ref> and [[Ruby (programming language)|Ruby]].<ref>Ruby:
==See also==
Line 46 ⟶ 56:
* [[Uninitialized variable]]
* [[Stack (abstract data type)]]
* [[Process control block]]
== References ==
{{reflist}}
== External links ==
* {{cite web
*[http://blog.ooz.ie/2008/09/0x03-notes-on-assembly-memory-from.html mem_sequence.c - sequentially lists memory regions in a process]▼
|title=C startup
|website = bravegnu.org}}
* {{cite web
▲
|archive-url = https://web.archive.org/web/20090202113414/http://blog.ooz.ie/2008/09/0x03-notes-on-assembly-memory-from.html
|url-status = dead
|archive-date = 2009-02-02
|title = mem_sequence.c - sequentially lists memory regions in a process}}
* {{cite book
|url = http://www.electroons.com/8051/ebooks/expert%20C%20programming.pdf
|title = Expert C Programming: Deep C Secrets
|first = Peter
|last = van der Linden
|publisher = Prentice Hall
|year = 1997
|pages = 119ff}}
{{DEFAULTSORT:Data Segment}}
|