当前位置:网站首页>The memory four area model of C language program

The memory four area model of C language program

2022-06-12 03:48:00 Embedded workplace

Catalog

1、 Memory four area model flow of program

2、 Heap area (heap)

3、 The stack area (stack)

4、 Global area (global) or static( Static zone )

5、 Code section (code)

6、 Specific examples


1、 Memory four area model flow of program

  • The operating system puts the physical hard disk code load To the memory
  • The operating system puts c The code is divided into four areas

  • The operating system found main Function entry execution

2、 Heap area (heap)

Release is usually assigned by the programmer ( Dynamic memory release and application ), If programmers don't release , At the end of the program , May be recycled by the operating system .

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 table , 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 .

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 .      

It needs to be assigned by the programmer , Such as :

ch = (char *)malloc(20);      // The distribution of 20 The byte area is the heap area 

stay C++ in :

p = new char[10]; 

3、 The stack area (stack)

Release is automatically allocated by the compiler , Store function parameter values , Local variable value ;

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 .    

Stacks are allocated automatically by the system , Faster . But programmers can't control it . 

for example :

Declare the local variables in the function :int m; The system makes room for it in the stack .

4、 Global area (global) or static( Static zone )

Global variables and static variables are stored together , Initialized global variables and static variables are in the same area .

Uninitialized global variables and uninitialized static variables are in another adjacent area .

This area is recycled by the operating system after the program runs .

5、 Code section (code)

Store the binary code of the function body ;

6、 Specific examples

#include <stdio.h>
#include <stdlib.h>

int a = 4;  // Global initialization area 
char *ch;   // Global uninitialized area 

int main() {
    int m;    // Stack 
    char *p;  // Stack 

    char *p3 = "123";   // 123/0 In the constant area ,p3 On the stack 

    static int n = 12;  // Global initialization area 

    p = (char *)malloc(sizeof(char));  // The allocated byte area is the heap area 
    ch = (char *)malloc(20);      // The distribution of 20 The byte area is the heap area 

    return 0;
}
原网站

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