Data segment
Encyclopedia
A data segment is a portion of virtual address space of a program, which contains the global variable
s and static variable
s that are initialized by the programmer
. This portion has a fixed size for each program depending upon the quantity of contents, since all of the data here is set by the programmer before the program is loaded.
Note that, data segment is not read-only, since the values of the variables can be altered at run time. This is in contrast to the Rodata (constant, read-only data) section, as well as the code segment
(also known as text segment).
The PC architecture
supports few basic read-write memory regions in a program namely: Stack, Data and Code.
A Heap is another region of address space of a program which is managed by operating system in terms how the program uses it of which it is a part of.
Ex:
also colloquially known as uninitialized data starts at the end of the data segment and 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 declared
begins at the end of the BSS segment
and grows to larger addresses from there. The Heap area is managed by malloc
, realloc, and free, which may use the 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/realloc/free; they may also be implemented using mmap to reserve potentially non-contiguous regions of virtual memory into the process' virtual address space
). The Heap area is shared by all shared libraries and dynamically loaded modules in a process.
they may be placed almost anywhere, but they still typically grow opposite
directions.)
The stack area contains the program stack
, a LIFO structure, typically located in the higher parts of memory. On the standard PC x86 computer architecture it grows toward address zero; on some other architectures it grows the opposite direction. 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.
Global variable
In computer programming, a global variable is a variable that is accessible in every scope . Interaction mechanisms with global variables are called global environment mechanisms...
s and static variable
Static variable
In computer programming, a static variable is a variable that has been allocated statically — whose lifetime extends across the entire run of the program...
s that are initialized by the programmer
Programmer
A programmer, computer programmer or coder is someone who writes computer software. The term computer programmer can refer to a specialist in one area of computer programming or to a generalist who writes code for many kinds of software. One who practices or professes a formal approach to...
. This portion has a fixed size for each program depending upon the quantity of contents, since all of the data here is set by the programmer before the program is loaded.
Note that, data segment is not read-only, since the values of the variables can be altered at run time. This is in contrast to the Rodata (constant, read-only data) section, as well as the code segment
Code segment
In computing, a code segment, also known as a text segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions....
(also known as text segment).
The PC architecture
IBM PC compatible
IBM PC compatible computers are those generally similar to the original IBM PC, XT, and AT. Such computers used to be referred to as PC clones, or IBM clones since they almost exactly duplicated all the significant features of the PC architecture, facilitated by various manufacturers' ability to...
supports few basic read-write memory regions in a program namely: Stack, Data and Code.
A Heap is another region of address space of a program which is managed by operating system in terms how the program uses it of which it is a part of.
Program memory
The computer program memory is organized into the following:- Data Segment (Data + BSS + Heap)
- Stack
- Code segmentCode segmentIn computing, a code segment, also known as a text segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions....
Data
The data area contains global and static variables used by the program that are initialized. This segment can be further classified into initialized read-only area and initialized read-write area. For instance the string defined bychar s[] = "hello world"
in C and a C statement like int debug=1
outside the "main" would be stored in initialized read-write area. And a C statement like const char* string = "hello world"
makes the string literal "hello world"
to be stored in initialized read-only area and the character pointer variable string
in initialized read-write area.Ex:
static int i = 10
will be stored in data segment and global int i = 10
will be stored in data segmentBSS
The BSS segment.bss
In computer programming, the name .bss or bss is used by many compilers and linkers for a part of the data segment containing statically-allocated variables represented solely by zero-valued bits initially...
also colloquially known as uninitialized data starts at the end of the data segment and 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 declared
static int i;
would be contained in the BSS segment.Heap
The heap areaHeap (data structure)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if B is a child node of A, then key ≥ key. This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap...
begins at the end of the BSS segment
.bss
In computer programming, the name .bss or bss is used by many compilers and linkers for a part of the data segment containing statically-allocated variables represented solely by zero-valued bits initially...
and grows to larger addresses from there. The Heap area is managed by malloc
Malloc
C dynamic memory allocation refers to performing dynamic memory allocation in the C via a group of functions in the C standard library, namely malloc, realloc, calloc and free....
, realloc, and free, which may use the brk and sbrk system calls to adjust its size (note that the use of brk/sbrk
Sbrk
brk and sbrk are basic memory management system calls used in Unix and Unix-like operating systems to control the amount of memory allocated to the data segment of the process. These calls are typically made from a higher-level memory management library such as malloc...
and a single "heap area" is not required to fulfill the contract of malloc/realloc/free; they may also be implemented using mmap to reserve potentially non-contiguous regions of virtual memory into the process' virtual address space
Virtual address space
Virtual address space is a memory mapping mechanism available in modern operating systems such as OpenVMS, UNIX, Linux, and Windows NT...
). The Heap area is shared by all shared libraries and dynamically loaded modules in a process.
Stack
The stack area traditionally adjoined the heap area and grew the opposite direction; when the stack pointer met the heap pointer, free memory was exhausted. (With modern large address spaces and virtual memory techniquesthey may be placed almost anywhere, but they still typically grow opposite
directions.)
The stack area contains the program stack
Stack (data structure)
In computer science, a stack is a last in, first out abstract data type and linear data structure. A stack can have any abstract data type as an element, but is characterized by only three fundamental operations: push, pop and stack top. The push operation adds a new item to the top of the stack,...
, a LIFO structure, typically located in the higher parts of memory. On the standard PC x86 computer architecture it grows toward address zero; on some other architectures it grows the opposite direction. 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.
See also
- Segmentation (memory)Segmentation (memory)Memory segmentation is the division of computer memory into segments or sections. Segments or sections are also used in object files of compiled programs when they are linked together into a program image, or when the image is loaded into memory...
- Linker (computing)
- Code segmentCode segmentIn computing, a code segment, also known as a text segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions....
- .bss.bssIn computer programming, the name .bss or bss is used by many compilers and linkers for a part of the data segment containing statically-allocated variables represented solely by zero-valued bits initially...
- Uninitialized variableUninitialized variableIn computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. As such it is a programming error and a common source of bugs in software....