Discussion on storage of value type and reference type in heap and stack I

Source: Internet
Author: User

First, let's take a brief look at what stack is and what heap is ). "Stack" is actually a kind of data structure of the post-in-first-out (LIFO. In our. NET Framework, the CLR is responsible for management. Our programmers don't have to worry about garbage collection. Every thread has its own "stack ". The storage of "Heap" is fragmented and managed by Garbage Collector (GC). The Garbage collection part we focus on is Garbage collection on the "Heap; the second is that the whole process shares a "heap ".

 

 

Let's first remember two golden rules:

1. The reference type is always allocated to the "heap.

2. The value type is always assigned to the declared place:

A. Assign the member variables of the reference type to the "heap ".

B. Allocate the local variable as the method to the "stack ".

 

To really understand the two golden rules above, you also need to know how "stack" and "stack" work. The following method is used as an example:

  AddFive(= pValue + 

1. Method AddFive () is pushed into "stack"

2. The method parameter pValue is pushed to the "stack"

3. Then we need to allocate space for the result variable, which will be allocated to the "stack.

4. The final result is returned.

By pointing the stack pointer to the available memory address used by the AddFive () method, all the memory used by this method on the "stack" is cleared, and the program will automatically return to the initial method call position on the "stack.

 

 

Next let's take a look at the situation where the value type variables are allocated to the "heap.

   MyInt AddFive( result = = pValue + 

As before, the thread starts to execute functions, and function parameters are pushed into the thread stack.

Because MyInt is of reference type, it is allocated on the "heap" and referenced by a pointer located on the "stack.

After the AddFive () function is executed, the "stack" is also cleared.

 

Finally, only one MyInt class is left on the "Heap" (the "stack" has no pointer to this MyInt class )! In this case, the GC mechanism is required.

 

Now that you have some knowledge about "stack" and "stack", let's look at two examples.

  x =  y == 

You actually know the returned values, or 3. Why? This is because the value type uses the value itself. In fact, when int y = x is used, 3 is assigned a copy of "copy" to y, then, when y = 4, the "copy" operation will not be performed on the original 3.

Next, let's look at another example. MyInt is still the MyInt class used in the above example.

 = == 

In this Code, will x. MyValue still be 3? Is y. MyValue a "copy" of Operation 3? In fact, you only need to run this code to get the result. x. MyValue is modified to 4. This is because when we use the reference type, we are actually using the address pointing to these objects, rather than directly using the objects themselves.

 

Now, we will introduce you here. If you have any questions or errors, please give us some advice and exchange.

Related Article

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.