Three Meanings of stack

Source: Internet
Author: User

Understanding the stack is critical to understanding the execution of programs. Easy obfuscation is that the word actually has three meanings, which are applicable to different occasions and must be differentiated.

 

Meaning 1: Data Structure

The first meaning of stack is the storage method of a group of data, which is characterized by lifo, that is, last in, first out ).

In such a data structure, data is piled up layer by layer as a building block, followed? The data is placed on the top. When used, the first data in the top layer is used up. This is called "back-in-first-out ".

The following methods are applicable to such a structure:

(1) Push: add? Data

(2) Pop: return and remove the top-level data

(3) Top: returns the top-level data value, but does not remove it.

(4) isempty: returns a Boolean value indicating whether the current stack is empty.

 

Description 2: Code Execution Method

Another meaning of stack is "call stack", which means that functions or child routines are stored like stacked wood for layer-by-layer calls.

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 ()

{

Student s;

S = new student (23, "John ");

}

}

When the above code is executed, the main method is called first, and a student instance needs to be generated, So student constructor is called again. Call the setname method in the constructor.

Description 3: Memory Zone

The third meaning of stack is a memory area that stores data. During program execution, you need memory space to store data. Generally, the system divides two types of memory space: Stack and heap ).

 

The main difference between them is that stack is structured. Each block is stored in a certain order, so that you can understand the size of each block. heap is unstructured and data can be stored at will. Therefore, the addressing speed of stack is faster than that of heap.

 

There are other differences. Generally, each thread allocates a stack, and each process assigns a heap. That is to say, the stack is exclusively occupied by threads, and heap is shared by threads. In addition, the size of the stack is determined when it is created. If the data size exceeds this value, the stack overflow error occurs, while the heap size is uncertain. If necessary, can the stack be added continuously ?.

 

Based on these differences, the data storage rules are as follows: only partial data that occupies space is usually stored in the stack; otherwise, the data is stored in the heap.

 

Public void method ()

{

Int I = 4;

Int y = 2;

Class cls1 = new class1 ();

}

The method in the above Code includes three variables: I, Y, cls1., where the values of I and Y are integers, the memory occupied space is definite, and local variables, it is only between the method blocks and is not used outside the blocks. Cls1 is also a local variable, but its type is Pointer variable, pointing to an object instance. the size of the pointer variable is determined, but the size of the memory occupied by the object instance cannot be determined based on the current information.

 

Therefore, I, y, and cls1 are all stored in the stack, because they occupy a certain amount of memory space and are also local variables. However, the object instance pointed to by cls1 is stored in heap because of its uncertain size.

As a potential rule, you must remember that all objects are stored in heap.

 

The next question is, when the method execution is complete, what will happen?

The answer is that the entire stack is cleared, and the three variables I, y, and cls1 disappear. Because they are local variables, once the block is executed, it is not necessary to exist again. The object instance in heap continues to exist until Garbage Collector recycles the memory. Therefore, in general,Memory leakage occurs in heap.That is, some memory space is no longer used, but is not recycled by the system for various reasons.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Three Meanings of stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.