This is my first article on getting started


First, the program memory allocation

  • Stack: the stack is automatically allocated and freed by the compiler. It holds function parameter values, local variable values, etc. It operates in a similar way to a stack in a data structure.

  • Heap: Usually allocated by the programmer to release, if the programmer does not release, the program may end by the OS. Note that this is not the same as a heap in a data structure, but is allocated like a linked list.

  • Global 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 static variables are in another adjacent area. The system releases them after the program ends.

  • Literal constant area: this is where constant strings are placed and released by the system when the program ends.

  • Program code area: store the binary code of the function body.

Example code:

int a = 0; // Global initialization area

char *p1; // Global uninitialized area

int main(a)

{
  int b; / / stack
  char s[] = "abc"; / / stack
  char *p2; / / stack
  char *p3 = "123456"; // 123456\0 in constant area, p3 on stack.
  static int c =0;// The global (static) initialization area
  
  p1 = (char *)malloc(10);
  p2 = (char *)malloc(20); // The allocated 10 and 20 bytes are in the heap.
  strcpy(p1, "123456"); // 123456\0 is placed in the constant area, and the compiler may optimize it into one place with "123456" pointed to by p3.

  return 0;

}
Copy the code

Second, basic knowledge

1. Application method

  • Stack: Automatically allocated 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 apply, and specify the size, in C malloc function: such as p1 = (char *)malloc(10).

2. 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 message will be reported indicating stack overflow.

  • Pile: First of all should know the operating system has a record 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 the majority of systems, The size of this allocation is recorded at the first address in the memory space, so that the delete statement in the code can properly free 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 redundant part back into the free list.

3. Limits on application size

  • Stack: In Windows, a stack is a data structure that extends to a lower address and is a contiguously spaced area of memory. In WINDOWS, the size of the stack is 2M(or 1M, in short, a constant determined at compile time). If the requested space exceeds the remaining space of the stack, the message overflow will be displayed. As a result, the amount of space available from the stack is small.

  • Heap: A heap is a data structure that extends to a higher address and is a discontinuous area of memory. This is because the system is to use the list to store the free memory address, natural is discontinuous, and the list traversal direction is from the low address to the high address. The size of the heap is limited by the virtual memory available on the computer system. As you can see, the space obtained by the heap is more flexible and larger.

4. Comparison of application efficiency:

  • Stack: The system automatically allocates the stack. But programmers can’t control that.

  • Heap: Is the memory allocated by new. It is generally slow and prone to memory fragmentation, but is the most convenient to use.

5. Storage contents in the heap and stack

  • Stack: When a function is called, the address of the next instruction after the main function (the executable next to the function call statement) is first pushed on the stack, followed by the function’s arguments, 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 on the stack. At the end of this function call, the local variables go out of the stack first, then the arguments, and finally the top of the stack pointer points to the first stored address, which is the next instruction in the main function, from which the program continues to run.

  • 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.

Third, summary

The difference between stack and heap can be seen by the following analogy: the use of stack is like we go to a restaurant to eat, just order (send out application), pay, and eat (use), eat full go, do not have to pay attention to cutting vegetables, vegetables and other preparation work and wash dishes, brush the pot and other finishing work, his advantage is fast, but small freedom. Use pile elephant is oneself begin work do the dish that likes to eat, more troublesome, but accord with oneself taste more, and freedom is big.

Another is that a function call has a series of operations on the stack that preserve the field and pass parameters. The size of the stack is limited. The default value for VC is 2M. When you run out of stacks, you allocate a lot of arrays and recursive functions that are too deep in your program. It is important to know that when a function call returns it frees up all the stack space in that function. The stack is automatically managed by the compiler, so you don’t have to worry about it. The heap allocates memory dynamically, and you can allocate and use a large amount of memory. But bad use can produce memory leak. And frequent malloc and free produce memory fragmentation (a bit like disk fragmentation) because C allocates dynamic memory looking for a match. With stacks, there is no fragmentation. Accessing data on the stack is faster than accessing data on the heap through Pointers. Stack = stack = stack = heap = heap = heap = heap = heap = heap = heap = heap = heap = heap The stack is first in and then out, generally from the high address to the low address growth.