The memory management stack stack

1, what is the stack
   stack is a data structure, C language is used to store a local variable stack. Invented stack is memory management.
2, the stack memory management features (small memory, automation)
   last out FILO first in last out stack
   first in first out FIFO first in first out queue
  stack feature is the entrance is exported, there is only one port, the other port is blocked in . It must be advanced after the out.
  Features queue is the inlet and outlet are, we must go from the entrance, from exports. So advanced to the need to go out, otherwise it will block behind.
3, the application stack Example: local variables
   in the C language is a local variable stack to achieve.
   We define a local variable in C (int a), the compiler is allocated some space (4 bytes) to our local variables (allocated on the stack when the
   stack pointer moves give space to the local variable a means is used, the local variable stack 4 bytes of memory and a memory address to our definition of a name associated with
   it), the corresponding operation is to stack the stack.
 Note: This stack pointer movement and memory allocation is automatic (stack yourself, we do not have to write code to manipulate).
    And then wait for when we exit the function, the local variable to perish. Corresponding to the operation is popped stack (the stack). When the stack pointer is moved to stack the stack space
    is released with the 4 bytes of a space associated. This action also stack automatically, do not write code employing intervention.
 Advantage of the stack: stack memory management, the benefits of convenient, distribution and final recovery programmers do not have to worry about, C language automatically.
 A detailed analysis: C language, the definition of local variables if not initialized, then the value is random, and why?
 Define a local variable, in fact, to provide a memory space and the name of the local variable bindings to the program on the stack by moving the stack pointer. Because this memory space
 on the stack, and the stack memory is used repeatedly (that is, no clear last run), so that when using the stack to achieve local variables defined, if not initialized,
 then the value of a variable is random of.
 C language is achieved by a local variable initialization it means small:
 int a = 15; // initializes a local variable is defined
 automatically C compiler converts this code into the line:
 int A; // definition of local variables
 a = 15 ; // ordinary assignment
4, stack constraints
 first, there is the stack size. So stack memory size is not set. If too afraid to overflow, too afraid to waste memory. (A bit like this shortcoming array)
 Second, stack overflow is a great harm, so we must avoid. Too much time to define local variables can not be defined or is too large. (For example:
 int A [10000]; recursive convergence must pay attention to solve the problem using recursion)

Guess you like

Origin www.cnblogs.com/jiangtongxue/p/11352133.html