Packing and unpacking stop and stop

Source: Internet
Author: User

Packing and unpacking stop and stop

The previous article briefly described the relationship between the value type and the reference type, so we can't help but talk about The binning and packing of the Value Type:

Packing a value type is equivalent to packing it for someone to see. "You see, I'm a reference type now ", as the name implies, unpacking means loading and unloading the package "actually I am a value type ".

When packaging value types into references using the packing mechanism, he needs to dress up a lot:

1. first, you need to give him a space (allocate memory in the hosting heap). How big is it? You need to store the cosmetics required for packaging (the amount of memory required for each segment ), in addition, there must be an expert designer (two additional members, type object pointers and synchronized block indexes );

2. If the space is ready, he will start to move his own things and objects (only type fields are copied to the newly allocated heap memory );

3. everything is ready, but in an accident, he was replaced by someone impersonating him. The person impersonating him knows the location of the room (the object address is returned, change the address is the object reference ).

The above explanation may be a little too far-fetched. It is clear to give an example:

 1 struct Point{public int x,y;}
2 public sealed class Program{ 3 public static void Main() 4 { 5 ArrayList a = new ArrayList(); 6 Point p; 7 p.x=1;p.y=1; 8 a.Add(p); 9 } 10 }

The simplest code is that the Add () method parameter is of the Object type, so a packing operation is required during execution: the fields in Point value type instance p are copied to the newly allocated Point object. The address of the boxed Point object is returned and passed to the Add method, it must be noted that the Point object is a reference type in the heap, and the Point value type Variable p can be reused. a does not know anything about it, therefore, the lifetime of the boxed Value Type exceeds that of the unboxed value type.

-------------------------------------------------------------------------------

Next we will talk about the unpacking:

Next to the code above, if Point p = (Point) a [0]; when the code is executed, it is split once:

A person knows the address of the room, and he wants to get everything back, and things may have changed (unpacking is not a simple reverse packing process, and its cost is lower than packing, unpacking is the process of getting a pointer. The Pointer Points to the original value type (data field) contained in an object, and the Pointer Points to the unpacked part of the boxed instance, do not copy any bytes in the memory );

Note the following when unpacking: 1. if the variable "reference to a boxed instance" is null, an exception is thrown (no room or object exists)

2. If the referenced object is not a boxed instance of the required value type, an exception will be thrown (for example, James, Li silai, can the other party send an exception );

 

--------------------------------------------------------------------------------

Let's make a brainstorm:

1 public static void Main(){2         int v=5;3         Object o = v;4         v=123;5         Console.WriteLine(v+","+(int)o);6 }

Q: How many times have the above Code been packed? Hope to open...

Three times, one time is easy to understand. The remaining two times can be understood by comparing the Console. WriteLine parameter.View Code

 

For some content, refer to "clr c #".

   

 

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.