Difference data segment, code segment, a stack segment, the BSS section

Memory allocation area 
under the 32-bit operating system: 1G kernel mode, 3G user mode

BSS group: generally refers to a procedure used to store global variables uninitialized, static variables (the default time is not the global variable is initialized to 0) of a memory area

Data segment: it refers generally to global and static variables used to store the initialization program

Code segment: it refers generally used to store program code and constants

Heap: usually refers to a dynamically allocated memory segments used to store the program process is running (dynamic allocation: malloc / new, ambulatory release: free / delete)

Stack: generally refers to the local variable used to store the user program temporarily created, the function parameter, the array (not local variables initialized default value as spam) which means that we function brackets "{}" variable defined in (but not including static variables declared, static means that the variable is stored in the data segment). Other than, when the function is called, its parameters will be pushed to initiate the process stack called, and until the end of the call, the return value will be stored back into the stack. Because of the advanced features of the stack, so the stack is particularly convenient to save / restore call site. In this sense, we can stack as a deposit, exchange data temporary memory area. It is assigned by the operating system, memory, application management and recovery by the OS.

Difference (heap) and stack (Stack) of the stack 
1, the mode of application

Stack: automatically assigned by the system. For example, in the function declaration of a local variable int b; b automatically open space in the stack

Heap: Programmers need to apply their own, and indicate the size (dynamic allocation: malloc / new, ambulatory release: free / delete)

2 in response to application of the system

Stack: As long as the remaining stack space is greater than the application space, the system will provide program memory, otherwise it will report an exception in the stack overflow.

Heap: you should know that the operating system has a list of free memory address of the record, when the system receives the application process, 
will traverse the linked list, find the first space is greater than the application heap space node, then the node from free node list to delete, and assign the node to the space program, in addition, for most systems, the first will address this memory space allocated record size this time, so, delete the statement in the code the right to release this memory space. Further, since the size of the size of the stack to find the node is not necessarily exactly equal to the application, the system will automatically back into the excess portion of the free list.

3, application size limit

Stack: In Windows, the stack is expanded to the low address data structure is a contiguous area of ​​memory. This means that the maximum capacity of the stack address and stack predetermined system is good, in WINDOWS, the stack size is 2M (some say 1M, in short, it is a compile-time constants determined), if when space applications exceeds the remaining space on the stack you will be prompted to overflow. Therefore, less space obtained from the stack.

Heap: heap is extended to the high-address data structure is not continuous memory area. This is because the system is a linked list of free memory address is stored, even nature is not 
continuous, 
and the list traversal direction is from low to higher addresses. The heap size is limited by the computer system effective virtual memory. Thus, the space available heap more flexible, is relatively large. 
(4) application efficiency comparison:

Stack: automatically assigned by the system faster. But the programmer can not control.

Heap: It is a new memory allocation, usually more slowly, and prone to memory fragmentation, but it most convenient to use. 
In addition, in WINDOWS, the best way is to use Virtual Alloc allocate memory, he was not in the heap, nor is it in the stack, but it retains a memory address space directly in the process, although it used the most inconvenient. But the speed, but also the most flexible.

The stored content (5) in the heap and stack

Stack: the function call, the first into the stack is the next instruction in the main function (executable statement at a function call statement) address and is a function of various parameters, in most compilers C , the argument is pushed onto the stack from right to left, then a function of the local variables. Note that the static variable is not pushed on. 
After the end of this function call, first-out stack of local variables, then the parameters of the last stack pointer points to the beginning address of the memory, i.e. the main function of the next instruction, the program continues to run from this point.

Heap: one byte is generally stored in the heap size of the head stack. The specific content of the heap arranged by the programmer.

(6) comparing the access efficiency

Where the size of the BSS segment recorded? 
Reprinted: http://blog.csdn.net/Virtual_Func/article/details/48529249

bss size segment, recorded in the segment table, the record is all uninitialized variables in total size, bss section only records in the segment table, but does not actually exist in this segment. 
The size of each uninitialized variable discharge in the symbol table

/***************************************************************

The Name File: 2.cpp 
Author: Bliss fleeting 
Function List: main () main function 
Created Time: 2018 Nian 01 Thursday, 18 October 15:14:25 
**************** ********************************************** /

#include <iostream>
using namespace std;

int a = 0;//初始化的全局变量:保存在数据段 char *p1;//未初始化的全局变量:保存在BSS段 int main() { int b;//未初始化的局部变量:保存在栈上 char s[] = "abc";//"abc"为字符串常量保存在常量区;数组保存在栈上, 并将常量区的"abc\0"复制到该数组中。这个数组可以随意修改而不会有任何隐患, 而"123"这个字符串依然会保留在静态区中。 char *p2;//p2保存在栈上 char *p3 = "123456";//p3保存在栈上,"123456\0"保存在data区的read-only部分 //注意:如果令p3[1] = 9; 则程序崩溃,指针可以访问但不允许改变常量区的内容 //声明了一个指针p3并指向"123456\0"在静态区中的地址,事实上,p3应该声明为 char const *,以免可以通过p3[i]='\n'这一类的语法去修改这个字符串的内容。如果这样 做了,在支持“常量区”的系统中可能会导致异常,在“合并相同字符串”的编译方法下会导致其它 地方的字符串常量古怪地发生变化。 static int c = 0;//初始化的静态局部变量:保存在数据区(数据段) p1 = (char *)malloc(sizeof(char) * 10);//分配的10字节区域保存在堆上 p2 = (char *)malloc(sizeof(char) * 20);//分配的20字节区域保存在堆上 strcpy(p1, "123456"); //"123456\0"放在常量区,编译器可能会将它与p3所指向"123456"优化成一个地方 return 0; }

Guess you like

Origin www.cnblogs.com/mingzhang/p/10965691.html