Objective
When learning to program, often see the word stack, its Chinese name is called "Stack".
Understanding this concept is essential for understanding the operation of the program. It is easy to confuse that the word actually has three meanings, which apply to different occasions and must be differentiated.
Meaning one: Data structure
The first meaning of a stack is the way a set of data is stored, characterized by LIFO, which is LIFO (last in, first out).
In this data structure, it is stacked up like a building block, and the data added to it is placed at the top level. When used, the topmost data is first used, which is called "LIFO."
This structure is matched by a number of specific methods, mainly for the following.
- Push: Add data at the top level.
- Pop: Returns and removes the topmost data.
- Top: Returns the value of the topmost data, but does not remove it.
- IsEmpty: Returns a Boolean value that indicates whether the current stack is an empty stack.
Meaning two: How the code runs
The second implication of the stack is the call stack, which indicates that a function or subroutine is stored like a stacked wood to implement layer calls.
The following is an example of a Java code (source).
ClassStudent{int Age;String Name;PublicStudent( int Age,String Name){This.Age=Age;setName( Name);}public voidsetName( String Name){This.Name=Name;}}public classMain{public static voidmain( String[]Args) {Student s;S= New Student( at,"Jonh");}}
When the above code is running, the main method is called first, and a student instance is generated, and then the student constructor is called. In the constructor, it is called to the SetName method.
These three calls are piled up like bricks, called "Call stacks." When the program runs, it always completes the topmost call first, then returns its value to the next layer call until the entire call stack is completed, returning the final result.
Meaning Three: Memory area
The third meaning of a stack is a memory area where data is stored. When the program runs, it needs memory space to hold the data. In general, the system divides two different memory spaces: one is called stack, and the other is heap.
The main difference is that the stack is structured, and each chunk is stored in a certain order, and the size of each chunk can be clearly known, the heap is unstructured, and the data can be stored arbitrarily. Therefore, the stack is addressed faster than the heap.
The other difference is that, in general, each thread allocates a stack, each process allocates a heap, that is, the stack is thread-exclusive, and the heap is thread-shared. In addition, when the stack is created, the size is deterministic, the data exceeds this size, and a stack overflow error occurs, and the heap size is indeterminate and needs to be increased continuously.
According to these differences, the data storage rules are: As long as the local, occupy space determined data, usually stored in the stack, or put in the heap inside. Take a look at the code below (source).
Method1() {int i=4; int y=2; = New Class1 (); }
The Method1 method of the above code contains three variables: I, Y, and CLS1. Where the value of I and Y is an integer, the memory footprint is deterministic, and is a local variable that is used only within the METHOD1 block and is not used outside the block. CLS1 is also a local variable, but the type is a pointer variable that points to an instance of an object. The size of the pointer variable occupies is deterministic, but the object instance is not able to know the amount of memory space it occupies with the current information.
These three variables and an object instance are stored in memory in the following way.
As you can see, I, Y, and cls1 are stored in the stack because they occupy memory space that is deterministic and are local variables themselves. However, the object instance that CLS1 points to is stored in the heap because its size is indeterminate. As a rule you can remember that all objects are stored in the heap.
The next question is, what happens when the Method1 method finishes running?
The answer is that the entire stack is emptied, and the three variables I, Y, and cls1 disappear because they are local variables, and once the chunks are running, there is no need to exist again. The object instance in the heap continues to exist until the system's garbage cleanup mechanism (garbage collector) reclaims the memory. Therefore, in general, memory leaks occur in the heap, where some memory space is no longer used, but for various reasons, it is not reclaimed by the system.
Three meanings of stack