69,382
社区成员
发帖
与我相关
我的任务
分享
Data segment
From Wikipedia, the free encyclopedia
Jump to: navigation, search
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".
1. The Data segment contains constants 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.
2. 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.
3. The heap area begins at the end of the data 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.
4. 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.
#include <stdio.h>
#include <malloc.h> /* for using "ptrdiff_t" which defined in GLIBC */
#include <unistd.h>
#include <alloca.h> /* just for set a example */
extern void afunc(void);/* show the stack increament */
int bss_var; /* auto initialize to 0, it should be arranged to BSS */
int data_var = 42; /* initialize to NON-ZERO, it should be arranged to data segment. */
int main(int argc, char **argv) /* the arguments has no use */
{
char *p, *b , *nb;
int temp_var1;
int temp_var2;
int temp_array[100000];
int temp_var3;
printf("Text location :\n");
printf("\tAddress of main: %p\n", main);
printf("\tAddress of afunc: %p\n", afunc);
printf("In main Function:\n");
printf("\tAddress of temp_var1: %p\n", &temp_var1);
printf("\tAddress of temp_var2: %p\n", &temp_var2);
printf("\tAddress of temp_array: %p\n", temp_array);
printf("\tAddress of temp_array[1]: %p\n", &temp_array[1]);
printf("\tAddress of temp_array[99999]: %p\n", &temp_array[99999]);
printf("\tAddress of temp_var3: %p\n", &temp_var3);
printf("Stack location: \n");
afunc();
p = (char *) alloca(32);
if (p!= NULL)
{
printf("\tStart of alloca()'ed array: %p\n", p);
printf("\tEnd of alloca()'ed array: %p\n", p+31);
}//if
printf("Data location: \n");
printf("\tAddress of data_var: %p\n", &data_var);
printf("BSS location: \n");
printf("\tAddress of bss_var: %p\n", &bss_var);
b = sbrk((ptrdiff_t) 32); /* increament the address space. */
nb = sbrk((ptrdiff_t) 0);
printf("Heap location:\n");
printf("\tInitial end of heap: %p\n", b);
printf("\tNew end of heap: %p\n", nb);
b = sbrk((ptrdiff_t) -16); /* contract the arrdress space. */
nb = sbrk((ptrdiff_t) 0);
printf("\tFinal end of heap: %p\n", nb);
return 0;
}//main()
void afunc(void)
{
static int level = 0; /* recursion level */
int stack_var; /* auto variable on stack */
if (++level == 3)
{
return;
}//if
printf("\tStack level %d: address of stack_var: %p\n", level, &stack_var);
afunc();
}//afunc()
-------------------------------------------------------------------------
lee@Lee:~/MyProject/utility$ ./print_address
Text location :
Address of main: 0x8048484
Address of afunc: 0x80486b9
In main Function:
Address of temp_var1: 0xbfc41990
Address of temp_var2: 0xbfc4198c
Address of temp_array: 0xbfbdff08
Address of temp_array[1]: 0xbfbdff0c
Address of temp_array[99999]: 0xbfc41984
Address of temp_var3: 0xbfc41988
Stack location:
Stack level 1: address of stack_var: 0xbfbdfee4
Stack level 2: address of stack_var: 0xbfbdfeb4
Start of alloca()'ed array: 0xbfbdfed0
End of alloca()'ed array: 0xbfbdfeef
Data location:
Address of data_var: 0x804a020
BSS location:
Address of bss_var: 0x804a030
Heap location:
Initial end of heap: 0x87ae000
New end of heap: 0x87ae020
Final end of heap: 0x87ae010
lee@Lee:~/MyProject/temp$ cat ./test.c && size ./test
#include <stdio.h>
int array[10000] ;
int main(int argc, char* argv[])
{
printf("Array location:%p\n", array);
printf("array[0] = %d\n", array[0]);
return 0;
}
text data bss dec hex filename
951 264 40032 41247 a11f ./test
lee@Lee:~/MyProject/temp$ cat ./test.c && size ./test
/* 初始化BSS段也是在BSS段中。 大小还是一样 */
#include <stdio.h>
int array[10000] = {0};
int main(int argc, char* argv[])
{
printf("Array location:%p\n", array);
printf("array[0] = %d\n", array[0]);
return 0;
}
text data bss dec hex filename
951 264 40032 41247 a11f ./test
lee@Lee:~/MyProject/temp$ cat ./test.c && size ./test
/* 把数组改成10。 BSS段大小变化 */
#include <stdio.h>
int array[10] ;
int main(int argc, char* argv[])
{
printf("Array location:%p\n", array);
printf("array[0] = %d\n", array[0]);
return 0;
}
text data bss dec hex filename
951 264 72 1287 507 ./test