Data segment

This is an old revision of this page, as edited by Ghiraddje (talk | contribs) at 00:04, 2 May 2009 ("Terrorist incidents in 2000"?). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A data segment is one of the sections of a program in an object file or in memory, which contains the global variables that are initialized by the programmer. It has a fixed size, since all of the data in this section is set by the programmer before the program is loaded. However, it is not read-only, since the values of the variables can be altered at runtime. This is in contrast to the Rodata (constant, read-only data) section, as well as the code segment (also known as text segment).

In the PC architecture there are four basic read-write memory regions in a program: Stack, Data, BSS, and Heap. Sometimes the data, BSS, and heap areas are collectively referred to as the "data segment".

To resume, RAM area contains:

  • Data Segment (Data + BSS + Heap)
  • Stack

In detail:

  • The Data area contains global variables used by the program that are not initialized to zero. For instance the string defined by char s[] = "hello world"; in C would exist in the data part.
  • The BSS segment starts at the end of the data segment and contains all global variables that are initialized to zero. For instance a variable declared static int i; would be contained in the BSS segment.
  • The heap area begins at the end of the BSS segment segment and grows to larger addresses from there. The Heap area is managed by malloc, realloc, and free, which use the brk and sbrk system calls to adjust its size. The Heap area is shared by all shared libraries and dynamic load modules in a process.
  • The stack is a LIFO structure, typically located in the higher parts of memory. It usually "grows down" with every register, immediate value or stack frame being added to it. A stack frame consists at minimum of a return address.

See also