C # Notes on Stack and stack

Source: Internet
Author: User
C # Notes on Stack and stack

What are stacks and stacks?

In short, heap and stack reside in the memory, and their role is to help us execute code. In the. NET Framework environment, when code is executed, the heap and stack in the memory store the code and contain all the information required for code execution.

In this case, there is still some abstraction. So what exactly are saved in the heap and stack? In summary, there are four types of data:

1. Value Type data

2. reference type data

3. pointer

4. Commands

 

Next we will give a brief introduction to the above four types of data. We know that data types in C # are divided into two types: Value Type and reference type. Value-type data stores their own content (values) directly in one location in the memory; reference-type data stores the address pointing to another location in the memory in one location, the content (value) is stored in the location indicated by this address ).

Pointers that are not displayed in the. NET Framework environment are managed by CLR. The pointer itself is a memory address that points to another memory location. Its value is a memory address or is null ).

The instruction refers to the instruction for executing this method. When the method is executed, space must be allocated to it on the stack.

 

So how are the four types of data allocated for storage in the heap and stack? Or we can narrow down the scope of our concerns. How are values and reference data allocated?

 

Rules:

1. reference type data is always stored in the heap;

2. If the value type data is declared in the method body, it will be stored on the stack; if it is declared as a member of the reference type, it will be stored in the heap.

 

Based on the above two rules, let's take a look at the differences between the stack and stack.

In the memory, the stack is responsible for saving the code execution path (call path ). When our code starts to call a method, it first places an encoding instruction on the stack, then places the method parameter, and then when the code is executed to the position of the variable declared in the method body, these variables will be pushed to the top of the stack (note that this refers to the value type data, the first case ). As of now, the declared value type data in the method body is stored on the stack. When the method execution is complete and the result of the method is returned, all the memory space used by the method on the stack is cleared, and the program will automatically return to the position of the initial method call on the stack. This also tells us that the stack is self-maintained and the memory automatically maintains the stack. There is no garbage collection problem.

In the second case, when the code is executed to the location of the referenced data declared in the method body, the referenced data will be created on the stack, at the same time, a pointer to the heap is generated on the stack, which is stored on the stack. After the method is executed, the related information on the stack is cleared. However, the rest of the reference data parameters are stored in the stack, which is the cause of garbage collection. Note: Garbage collection is very performance-consuming, which is why we need to pay special attention to the use of stacks and stacks.

 

The above content is a basic and simple understanding of heap and stack in C #. We will continue to think deeply and explore heap and stack content in the future.

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.