当前位置:网站首页>About stack area, heap area, global area, text constant area and program code area

About stack area, heap area, global area, text constant area and program code area

2022-07-04 22:30:00 InfoQ

One by C/C++ The memory occupied by the compiled program is divided into the following parts

The stack area (
stack
)
Automatically assigned by the compiler 、 Release , Stores the parameter values of the function 、 The value of a local variable, etc . It operates like a stack in a data structure .
Heap area (
heap
)
Release is usually assigned by the programmer , If programmers don't release , At the end of the program, the OS Recycling . Notice that it's not the same as the heap in the data structure , The distribution method is similar to the linked list
Global area ( Static zone )(
static
)
Global and static variables are stored in one block , Released by the system at the end of the program .

  • Initialized global and static variables in one area ,
  • Uninitialized global variables and uninitialized static variables are in another adjacent area .
Text constant area
That's where constant strings are put , Released by the system at the end of the program
Program code area
Store the binary code of the function body .

int a = 0; // Global initialization area
char *p; // Global uninitialized area
int main(int argc, char * argv[]) {
 int b; // Stack
 char *p1; // Stack
 char s[] = "abc"; // Stack
 char *p2 = "123456"; //"123456" In the constant area ,p2 On the stack
 static int c = 0; // Global static area , Initialization area
 p = (char *)malloc(10); // It's distributed 10 and 20 The byte area is in the heap area
 p1 = (char *)malloc(20); // It's distributed 10 and 20 The byte area is in the heap area
 strcpy(p1, "123456"); //123456 In the constant area , The compiler may associate it with p1 Points to 123456 Optimize into one place
}


Theoretical knowledge of heap and stack

Application method
  • Stacks are allocated automatically by the system , Faster , It's a continuous memory area , Size is 2MB. for example , Declare a local variable in a function  
    int b
    ;  The system automatically adds
    b
    Open up space
  • Heap requires programmers to apply , And indicate the size , Relatively slow . use
    malloc
    alloc
    new
    calloc
    realloc
    Wait for the function that allocates memory to get on the heap . in addition , Heap is a data structure extended to high address , It's a discontinuous memory area , The size of the heap is limited by the virtual memory of the computer . Therefore, the acquisition and use of heap space is more flexible , Large available space . for example

p1 = (char *)malloc(10);
p2 = (char *)malloc(10);

But notice  p1、p2 Itself is in the stack .
System response after application
  • Stack : As long as the remaining space of the stack is larger than the requested space , The system will provide memory for the program , Otherwise, an exception will be reported indicating that the stack overflows .
  • Pile up : First of all, you should know that the operating system has a list of free memory addresses , When the system receives an application for a program , Will traverse the list , Find the first heap node whose space is larger than the applied space , Then delete the node from the free node list , And allocate the space of the node to the program , in addition , For most systems , The size of this allocation will be recorded at the first address in this memory space , such , In code
    delete
    Statement can correctly release the memory space . in addition , Because the size of the heap node found is not necessarily exactly the size of the request , The system will automatically put the extra part back into the free list .
Request size limit
  • Stack : stay Windows Next , Stacks are data structures that extend to low addresses , It's a continuous area of memory . The address at the top of the stack and the maximum capacity of the stack are predetermined by the system , stay Windows Next , The size of the stack is 2M( Some say it is 1M, All in all, it's a constant determined at compile time ), If the requested space exceeds the remaining space of the stack , Will prompt
    overflow
    . therefore , The space available from the stack is small .
  • Pile up : Heap is a data structure extended to high address , It's a discontinuous memory area . This is because the system uses linked list to store the free memory address , Nature is not continuous , The traversal direction of the list is from low address to high address . The size of the heap is limited by the virtual memory available in the computer system . thus it can be seen , The heap gets more flexible space , It's bigger .
Comparison of application efficiency
  • Stacks are allocated automatically by the system , Faster . But programmers can't control it .
  • The heap is made up of
    new
    Allocated memory , Generally speaking, it's slow , And it's prone to memory fragmentation , But it's the most convenient .  in addition , stay Windows Next , The best way is
    VirtualAlloc
    Allocate memory , He's not piling up , It's not in the stack, it's in the address space of the process , Although it's the most inconvenient to use . But fast , And the most flexible .
Heap and stack storage
  • Stack : On function call , The first one on the stack is the next instruction in the main function ( The next executable statement of a function call statement ) The address of , And then the parameters of the function , In most of C In the compiler , Parameters are pushed from right to left , And then the local variables in the function . Note that static variables are not pushed . When this function call ends , Local variables first out of stack , And then the parameters , Finally, the top of the stack pointer points to the address of the first storage , That is, the next instruction in the main function , The program continues to run from this point .
  • Pile up : Generally, a byte is used to store the size of the heap in the head of the heap . The specific contents of the heap are arranged by the programmer .
Access efficiency comparison
char s1[] = "aaaaaaaaaaaaaaa";
char *s2 = "bbbbbbbbbbbbbbbbb";
 
aaaaaaaaaaa It's assigned at runtime ;
and bbbbbbbbbbb It's determined at compile time ;

however , In later access , The array on the stack is more than the string the pointer points to ( For example, pile ) fast .

such as :

#include
void main(){
 char a = 1;
 char c[] = "1234567890";
 char *p ="1234567890";
 a = c[1];
 a = p[1];
 return;
}

The corresponding assembly code

10: a = c[1];
00401067 8A 4D F1 mov cl,byte ptr [ebp-0Fh]
0040106A 88 4D FC mov byte ptr [ebp-4],
cl11: a = p[1];
0040106D 8B 55 EC mov edx,dword ptr [ebp-14h]00401070 8A 42 01 mov al,
byte ptr [edx+1]00401073 88 45 FC mov byte ptr [ebp-4],al

The first is to read the elements in the string directly to the register when reading
cl
in , And the second is to read the pointer value to
edx
in , According to
edx
Read character , Obviously slow .
Summary  ( The difference between heap and stack can be seen in the following metaphor )
  • Using a stack is like eating in a restaurant , Just order ( Issue an application )、 pay 、 And eating ( Use ), When you are full, go , Don't pay attention to cutting vegetables 、 Prepare for dishes and dishes 、 Brush the pot and finish the work , His advantage is fast , But the freedom is small .
  • Using heaps is like making your own favorite dishes , More trouble , But more in line with their own taste , And there's a lot of freedom .
contrast
  • Stack is the function provided by the system , It's fast and efficient , The disadvantage is that there are limits , Data inflexibility ; Heap is a function provided by function library , It is characterized by flexibility and convenience , Data has a wide range of applications , But the efficiency has been reduced to a certain extent .
  • Stack is the data structure of the system , For the process / Threads are unique ; Heap is the internal data structure of function library , Not necessarily the only one . Memory of different heap cannot operate with each other . Stack space is divided into static allocation and dynamic allocation . Static allocation is done by the compiler , Like automatic variables (auto) The distribution of . Dynamic allocation is made by
    alloca
    Function completion . Dynamic allocation of the stack does not require release ( It's automatic ), There is no release function . For the sake of portable programs , Dynamic allocation of stacks is not encouraged ! The allocation of heap space is always dynamic , Although all the data space will be released back to the system at the end of the program , But precise application memory / Free memory matching is good   The basic elements of the program .
  • Generally speaking, stack (
    stack
    ) It often refers to the stack , First in, then out ,  It's a memory area . Local variables used to store programs , Temporary variable , The parameters of the function , Return address, etc . The allocation and release of variables in this area are automatically carried out by the system . No user involvement is required . And in the pile (
    heap
    , fifo )  The space on is allocated by users  , And the user is responsible for releasing .
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042200173391.html