We may frequently see stack when we read programming books. But many times we may be confused about the different meanings of it. This term actually has three common meanings. Here we explain the three different meanings of Stack in programming.
1. Data structure
The first meaning of Stack defines a method for storing data. Its feature is LIFO9Last In First Out). In this data structure, data are accumulated level by level. The data last put in is added at the top of the stack. When using the data, the top one is first popped out from the stack.
There are usually some methods related to this data structure implementation:
push() : Put one item at the top of the stack
pop() : Remove one item from the top of the stack
top() : Return the top item of the stack
isEmpty() : Check whether the stack contains any item
2. Code execution
The second meaning is usually called call stack which is referring to the calling order of different instructions of a program. Take a Java program as example:
class Student{ int age; String name; public Student(int Age, String Name) { this.age = Age; setName(Name); } public void setName(String Name) { this.name = Name; } } public class Main{ public static void main(String[] args) { Student s; s = new Student(23,"Jonh"); } }
When above code snippet is executed, the first method is called is main(), an instance of Student will be created inside main method. When creating Student instance, the constructor of it is called. Within this constructor, the setName() method is called.
3. Memory location
The last one is the memory location to store data. When a program is running, a block of memory is allocated to store the different data objects created. Generally, there would be two kinds of memory space : stack and heap.
The main difference between stack and heap is stack is structural, each section is stored in order and the size of each block is known to the user. While heap can store data in any order. This is also why stack access speed is faster than heap.
The general rule is local variables are stored on stack while global variables are stored in heap. See below example:
public void Method1() { int i=4; int y=2; class1 cls1 = new class1(); }
There are three variables in above method: i,y and cls1. Among them, i and y are integers, the memory taken by them are known. And they are local variables and accessed only in the method itself. So these two will be stored on the stack. cls1 is also a local variable, but it refers to an object instance which has unknown memory size. The variable itself is on the stack but the object it points to is stored in the heap.
When the execution of Method1 is completed, the whole stack allocated to the method will be vanished. This means i, y and cls1 will be cleared. But the object referenced by cls1 will be still in the heap until the garbage collector runs later. So usually memory leaks happen in the heap.
Author : 阮一峰 Source : http://www.ruanyifeng.com/blog/2013/11/stack.html
thank you , I have learnt some knowledge