c and c++ stack distribution and its setting method

  • 2020-05-06 11:42:46
  • OfStack

The memory used by a program compiled by C/C++ is divided into the following sections 1, the stack area (stack) -- is automatically allocated by the compiler to release, stored function parameter name, local variable name, etc. It operates like a stack in a data structure.
2. Heap area (heap) -- is allocated and released by the programmer. If the programmer does not release the heap area, it may be recovered by OS at the end of the program. Note that it is not the same as the heap in a data structure, but is allocated in a way similar to a linked list.
3. Global area (static area) (static) -- the storage of global variables and static variables is put together, the initialized global variables and static variables are in one area, and the uninitialized global variables and uninitialized static variables are in the adjacent area. When the program is finished, it is released by the system.
4, text constant area - constant string is placed here, after the end of the program by the system release.
5. Program code area -- the binary code that stores the function body.


int a = 0;// Global initialization area 
char*p1;// Global uninitialized area 
main()
{
    intb;// The stack 
    chars[] = "abc";// The stack 
    char*p2;// The stack 
    char*p3 = "123456";//123456\0 In the constant region, p3 On the stack. 
    static int c =0;// Global (static) initialization area 
    p1 = (char*)malloc(10);
    p2 = (char*)malloc(20);// distributive 10 and 20 The region of the byte is in the heap area. 
}

strcpy (p1, "123456"); 123456\0 is placed in the constant area, and the compiler may optimize it into a place with the "123456" that p3 points to.


Related articles: