Linux 程序内存空间布局
ux下,内存是如何被分配的?下图展示了一个典型的Linux C程序内存空间布局:要发现Linux下的内存问题,首先一定要知道在Lin
一个典型的Linux C程序内存空间由如下几部分组成:
- 代码段(.text)。这里存放的是CPU要执行的指令。代码段是可共享的,相同的代码在内存中只会 有一个拷贝,同时这个段是只读的,防止程序由于错误而修改自身的指令。
- 初始化数据段(.da
ta)。这里存放的是程序中需要明确赋初始值的变量,例如位于所有函数之外的全 局变量:int val=100。需要强调的是,以上两段都是位于程序的可执行文件中,内核在调用exec函数启动该程序时从源程序文件中读入。 - 未初始化数据段(.bss)。位于这一段中的数据,内核在执行该程序前,将其初始化为0或者 null。例如出现在任何函数之外的全局变量:int sum;
- 堆(Heap)。这个段用于在程序中进行动态内存申请,例如经常用到的malloc,new系列函数 就是从这个段中申请内存。
- 栈(Stack)。函数中的局部变量以及在函数调用过程中产生的临时变量都保存在此段中。
http://en.wikipedia.org/wiki/Data_segment#Program_memory
Program memory
The computer program memory is organized into the following:
- Data Segment (Data + BSS + Heap)
- Stack
- Code segment
[edit]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 by char 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 likeconst 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 segment
[edit]BSS:Block Started by Symbol
The BSS segment, also 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.
[edit]Heap
The heap area 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.
[edit]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 techniques 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.
[adad@localhost Ctest]$ cat memoryDis.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int a = 0; //全局初始化区
char *p1; //全局未初始化区
int main(int argc, char *argv[])
{
int b=1; // 栈
char s[] = "abc"; //栈
char *p2; //栈
char *p3 = "123456"; //"123456\0"在常量区,p3在栈上。
static int c =0; //全局(静态)初始化区
p1 = (char *)malloc(10);
p2 = (char *)malloc(20); //分配得来得10和20字节的区域就在堆区。
strcpy(p1, "123456"); //123456\0放在常量区,编译器可能会将它与p3所指向的"123456"优化成一个地方
printf("a :%p\n\
p1 :%p\n\
b :%p\n\
s :%p\n\
p2 :%p\n\
p3 :%p\n\
c :%p\n", &a, &p1, &b, s, &p2, &p3, &c);
system("pause");
return 0;
}
[zasasi@localhost Ctest]$ ./a.out
a :0x80497d8
p1 :0x80497e0
b :0xbfbbde0c
s :0xbfbbde08
p2 :0xbfbbde04
p3 :0xbfbbde00
c :0x80497dc
可以看出在数据段中,初始化的未初始化的变量是分类放的。
int a = 0; //全局初始化区 0x80497d8
static int c =0; //全局(静态)初始化区 0x80497dc
char *p1; //全局未初始化区 0x80497e0
Data Segment (Data + BSS + Heap)
这里解释是:数据段包含了堆区,那堆栈段只有栈区
Data:包含有全局变量(read-write area)、静态变量(read-write area)、常量(read-only area)
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.
BSS:包含有全局变量(uninitialized data)、静态变量(uninitialized data)
The BSS segment, also 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. http://en.wikipedia.org/wiki/.bss
转自:
http://blog.163.com/helloworld_zhouli/blog/static/2033711212012127114822565/