进程内存布局

一个由 C/C++编译的程序占用的内存(memory)分为以下几个部分:

1. 程序代码区(.text)  -      存放函数体的二进制代码  。

2. 文字常量区(.rodata)     -      常量字符串就是放在这里的,程序结束后由系统释放(rodata—read only data)。

3. 全局区/静态区(static)   -      全局变量 和 静态变量的存储是放在一块的。初始化的全局变量和静态变量在一块区域(.rwdata or .data),未初始化的全局变量和未初始化的静态变量在相邻的另一块区域(.bss), 程序结束后由系统释放。

*在 C++中,已经不再严格区分bss data了,它们共享一块内存区域

4. 堆区(heap)    -      一般由程序员分配释放(new/malloc/calloc delete/free),若程序员不释放,程序结束时可能由 OS 回收。

注意:它与数据结构中的堆是两回事,但分配方式倒类似于链表。

5. 栈区(stack)   -      由编译器自动分配释放,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。


(图出自:http://www.tenouk.com/Bufferoverflowc/Bufferoverflow1c.html



(图出自:APUE-2e, http://infohost.nmt.edu/~eweiss/222_book/222_book.html


 The computer program memory is organized into the following:

Code segment(text segment)
Data Segment 
– Data (rodata + rwdata)
– BSS
– Heap
Stack Segment


Data

The data area contains global and staticvariables used by the program that are initialized. This segment can be furtherclassified into initialized read-only (rodata) area and initialized read-writearea (rwdata).

BSS

The BSS segment also known as uninitialized datastarts at the end of the data segment and contains all uninitialized globalvariables and static variables that are initialized to zero by default. 

Heap

The heap area begins at the end of the BSSsegment and grows to larger addresses from there. The heap area is managed bymalloc/calloc/realloc/new and free/delete, which may use the brk and  sbrk system calls to adjust its size. The heaparea is shared by all shared libraries and dynamically loaded modules in aprocess.

Stack

The stack is a LIFO structure, typically locatedin the higher parts of memory. It usually “grows down” with everyregister, immediate value or stack frame being added to it. A stack frameconsists at minimum of a return address

例子程序

  1. //main.cpp  
  2. int a = 0;                      // 全局初始化区(data)  
  3. char *p1;                       // 全局未初始化区(bss)  
  4. int main()  
  5. {  
  6.     int b;                      // 栈区(stack)  
  7.     char s[] = “abc”;           // 栈区(stack)  
  8.     char *p2;                   // 栈区(stack)  
  9.     char *p3 = “123456”;        // p3 在栈区(stack);   “123456\0” 在常量区(rodata)  
  10.     static int c =0;            // 全局/静态 初始化区  (data)  
  11.     p1 = (char *)malloc(10);  
  12.     p2 = (char *)malloc(20);    // 分配得来的 10 和 20 字节的区域就在堆区 (heap)  
  13.     strcpy(p1, ”123456”);       // “123456\0” 放在常量区(rodata). 编译器可能会将它与 p3 所指向的”123456\0”优化成一个地方。  
  14.     return 0;  
  15. }   

一个由 C/C++编译的程序占用的内存(memory)分为以下几个部分:

1. 程序代码区(.text)  -      存放函数体的二进制代码  。

2. 文字常量区(.rodata)     -      常量字符串就是放在这里的,程序结束后由系统释放(rodata—read only data)。

3. 全局区/静态区(static)   -      全局变量 和 静态变量的存储是放在一块的。初始化的全局变量和静态变量在一块区域(.rwdata or .data),未初始化的全局变量和未初始化的静态变量在相邻的另一块区域(.bss), 程序结束后由系统释放。

*在 C++中,已经不再严格区分bss data了,它们共享一块内存区域

4. 堆区(heap)    -      一般由程序员分配释放(new/malloc/calloc delete/free),若程序员不释放,程序结束时可能由 OS 回收。

注意:它与数据结构中的堆是两回事,但分配方式倒类似于链表。

5. 栈区(stack)   -      由编译器自动分配释放,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。


(图出自:http://www.tenouk.com/Bufferoverflowc/Bufferoverflow1c.html



(图出自:APUE-2e, http://infohost.nmt.edu/~eweiss/222_book/222_book.html


 The computer program memory is organized into the following:

Code segment(text segment)
Data Segment 
– Data (rodata + rwdata)
– BSS
– Heap
Stack Segment


Data

The data area contains global and staticvariables used by the program that are initialized. This segment can be furtherclassified into initialized read-only (rodata) area and initialized read-writearea (rwdata).

BSS

The BSS segment also known as uninitialized datastarts at the end of the data segment and contains all uninitialized globalvariables and static variables that are initialized to zero by default. 

Heap

The heap area begins at the end of the BSSsegment and grows to larger addresses from there. The heap area is managed bymalloc/calloc/realloc/new and free/delete, which may use the brk and  sbrk system calls to adjust its size. The heaparea is shared by all shared libraries and dynamically loaded modules in aprocess.

Stack

The stack is a LIFO structure, typically locatedin the higher parts of memory. It usually “grows down” with everyregister, immediate value or stack frame being added to it. A stack frameconsists at minimum of a return address

例子程序

  1. //main.cpp  
  2. int a = 0;                      // 全局初始化区(data)  
  3. char *p1;                       // 全局未初始化区(bss)  
  4. int main()  
  5. {  
  6.     int b;                      // 栈区(stack)  
  7.     char s[] = “abc”;           // 栈区(stack)  
  8.     char *p2;                   // 栈区(stack)  
  9.     char *p3 = “123456”;        // p3 在栈区(stack);   “123456\0” 在常量区(rodata)  
  10.     static int c =0;            // 全局/静态 初始化区  (data)  
  11.     p1 = (char *)malloc(10);  
  12.     p2 = (char *)malloc(20);    // 分配得来的 10 和 20 字节的区域就在堆区 (heap)  
  13.     strcpy(p1, ”123456”);       // “123456\0” 放在常量区(rodata). 编译器可能会将它与 p3 所指向的”123456\0”优化成一个地方。  
  14.     return 0;  
  15. }   

猜你喜欢

转载自blog.csdn.net/vonmax007/article/details/77860119