C language of the use of stack of stack and heap of heap

  • 2020-04-02 02:20:19
  • OfStack

One, preparatory knowledge - the program memory allocation

The memory used by a C/C++ compiled program is divided into the following sections

1. Stack -- the compiler will automatically allocate and release the values of parameters of functions, local variables, etc. It operates like a stack in a data structure.
2, heap area (heap) - is generally allocated by the programmer to release, if the programmer does not release, the end of the program may be recovered by the OS. 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 in the same place, the initialized global variables and static variables in the same area, the uninitialized global variables and uninitialized static variables in the adjacent area. When the program is finished, it is released by the system.
4. Literal constant area - this is where constant strings are placed. When the program is finished, it is released by the system.
5. Program code area -- the binary code that stores the function body.

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201404/20140401164546.jpg? 201431164721 ">

Two, example procedures


//main.cpp
 int a=0;  //Global initialization area
 char *p1;  //Global uninitialized area
 main()
 {
  intb;The stack
  char s[]="abc";  //The stack
  char *p2;     //The stack
  char *p3="123456";  //1234560 In the constant region, p3 in The stack On. 
  static int c=0 ;   //Global (static) initialization area
  p1 = (char*)malloc(10);
  p2 = (char*)malloc(20);  //The areas allocated for 10 and 20 bytes are in the heap area.
  strcpy(p1,"123456");  //1234560 is placed in the constant area, and the compiler may optimize it to the same place as p3 "123456".
}

Theoretical knowledge of stacks and stacks
2.1 application method
The stack:
It is automatically assigned by the system. For example, declare a local variable int b in a function; The system automatically makes room in the stack for b

Heap:
  Requires the programmer to request and specify the size, using the malloc function in c
Such as p1 = (char *) malloc (10);
The new operator is used in C++
Such as p2 = (char *) malloc (10);
But notice that p1 and p2 themselves are in the stack.

2.2 response of the system after the 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.
Heap: first of all should know the operating system has a record of free memory address list, when the system is receiving program application, will traverse the list, find the first pile space is greater than the applied space node, then the node is removed from the idle node list, and assign the node space program, in addition, for most of the system, in the first address the distribution of memory space size, so, delete statements in your code to the right to release the memory space. In addition, since the size of the heap node found is not necessarily the same as the size of the application, the system will automatically put the extra part back into the free list.

2.3 application size limit
Stack: under Windows, a stack is a data structure that scales to a lower address and is a contiguous area of memory. In WINDOWS, the stack size is 2M (or 1M, a constant determined at compile time), and overflow will be prompted if the requested space exceeds the rest of the stack. Therefore, the amount of space available from the stack is small.
Heap: a heap is a data structure that extends to a high address and is a discrete area of memory. This is because the system USES the linked list to store the free memory address, which is naturally discontinuous, and the traversal direction of the linked list is from the low address to the high address. The size of the heap is limited by the amount of virtual memory available in the computer system. Thus, the heap gains more flexible and larger space.

2.4 comparison of application efficiency:
Stack: automatically allocated by the system, faster. But programmers can't control that.
Heap: memory allocated by new, usually slow and prone to memory fragmentation, but the most convenient to use. In addition, under WINDOWS, the best way to allocate memory is to use a Virtual Alloc, which is not in the heap or on the stack, but simply keeps a block of memory in the process's address space, even though it is the least convenient to use. But it's fast and the most flexible.

2.5 storage content in the heap and stack
Stack: when a function is called, the address of the next instruction in the main function (the next executable statement in the function call statement) is pushed first, followed by the parameters of the function, which in most C compilers are pushed from right to left, followed by local variables in the function. Note that static variables are not pushed. When the function call is over, the local variable is pushed out of the stack first, then the argument, and finally the top pointer on the stack points to the original address, which is the next instruction in the main function, and the program continues to run from that point.
Heap: the size of the heap is usually stored in one byte at the head of the heap. The details of the heap are arranged by the programmer.

2.6 comparison of access efficiency


char s1[]="aaaaaaaaaaaaaaa";
char *s2="bbbbbbbbbbbbbbbbb";
aaaaaaaaaaa Is assigned at runtime; while bbbbbbbbbbb Is determined at compile time; However, in later accesses, the array on the stack is more than the string that the pointer points to ( For example the heap ) Fast. Such as: 
#include
int main()
{
char a=1;
char c[]="1234567890";
char *p="1234567890";
a = c[1];
a = p[1];
return 1;
}

The corresponding assembly code


10:a=c[1];
004010678A4DF1movcl,byteptr[ebp-0Fh]
0040106A884DFCmovbyteptr[ebp-4],cl
11:a=p[1];
0040106D8B55ECmovedx,dwordptr[ebp-14h]
004010708A4201moval,byteptr[edx+1]
004010738845FCmovbyteptr[ebp-4],al

The first reads the elements of the string directly into the register cl, while the second reads the pointer values into the edx first, which is obviously slow to read the characters from the edx.
2.7 summary
The difference between stack and stack can be seen by the following analogy: the use of stack is like we go to a restaurant to eat, just order (to issue an application), pay, and eat (to use), eat when full, do not have to pay attention to the preparation work such as cutting, washing dishes, washing POTS and other finishing work, its advantage is fast, but less freedom. The use of heap is like doing the dishes you like to eat, more trouble, but more in line with their own taste, and freedom.


Related articles: