Data segment: Difference between revisions

Content deleted Content added
m Add C examples + Fix highlight
Make clear link is for Ruby
 
(3 intermediate revisions by 3 users not shown)
Line 2:
In [[computing]], a '''data segment''' (often denoted '''.data''') is a portion of an [[object file]] or the corresponding [[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]].
 
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.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 be able to access 64 KB of memory space, to access 1 MB of memory space.
Line 20:
 
<syntaxhighlight lang="c">
int i = 3;
char a[] = "Hello World";
static int b = 2023; // Initialized static global variable
void foo (void) {
static int c = 2023; // Initialized static local variable
}
</syntaxhighlight>
 
Line 32:
The '''BSS segment''' contains uninitialized static data, both variables and constants, i.e. global variables and local static variables that are initialized to zero or do not have explicit initialization in source code. Examples in C include:
<syntaxhighlight lang="c">
static int i;
static char a[12];
</syntaxhighlight>
=== Heap ===
Line 46:
 
==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: ObjectEmbedded Data: [httphttps://docs.ruby-doclang.org/docsen/keywordsmaster/1.9/Objectglobals_rdoc.html#methodlabel-i-__END__ __END__Embedded+Data]</ref> In these languages, including the line <code>__DATA__</code> (Perl) or <code>__END__</code> (Ruby, old Perl) marks the end of the code segment and the start of the data segment. Only the contents prior to this line are executed, and the contents of the source file after this line are available as a file object: <code>PACKAGE::DATA</code> in Perl (e.g., <code>main::DATA</code>) and <code>DATA</code> in Ruby. This can be considered a form of [[here document]] (a file literal).
 
==See also==