Packing and unpacking

Source: Internet
Author: User

Packing and unpacking
Packing: The value type is "lighter" than the reference type because they are not allocated as objects in the managed heap, are not garbage collected, or referenced through pointers. However, many times you need to obtain a reference of the value type. For example, if you want to create an ArrayList object to accommodate a group of point structures, the Code is as follows: public sealed class Program {public static void Main () {ArrayList a = new ArrayList (); Point p; // assign a Point (Not allocated in the heap) for (int I = 0; I <10; I ++) {p. x = p. y = I; // initialize a member of the value type. add (p); // binning the pair value type, adding the reference to the ArrayList }}} each iteration initializes a ponit Value Type field, and store the point in the ArrayList. But think about what is stored in ArrayList? Is it the Point structure, the address of the Point structure, or something completely different? To know the correct answer, you must study the Add method of ArrayList to understand the type of its parameter. In this example, the Add method prototype is as follows: public virtual int Add (Object value); you can see that Add obtains an Object parameter, that is, add gets a reference to an object on the managed stack as a parameter. However, the previous Code passes p, that is, a Point, which is a value type. To make the code work correctly, the Point value type must be converted to a real, hosted object in the heap, and must be referenced to this object. To convert a value type to a reference type, use the packing mechanism. The following describes what happens when a value-type instance is packed: 1. allocate memory in the managed heap. The amount of memory allocated is the amount of memory required for each field of the value type, plus two additional members for all the objects in the managed heap (type object pointers and synchronized block indexes) the amount of memory required is 2. The Value Type field is copied to the newly allocated heap memory 3, and the object address is returned. Now this object is an object reference; the value type is reference type C # The Compiler detects that the above Code is to pass the value type to the method requiring the reference type, so the automatically generated code is used to pack the object. Therefore, when running, the fields that currently exist in Point value type instance p are copied to the newly allocated Point object. The address of the packed Point Object (now a reference type) is returned and passed to the Add method. Point objects exist in the heap until they are recycled. Point Value Type Variable p can be reused because ArrayList does not know anything about it. In this case, the lifetime of the boxed type exceeds the lifetime of the boxed value type. Unbox: assume that you need to use the following code to obtain the first element of ArrayList: Point p = (Point) a [0]; it gets the reference contained in element 0 of ArrayList, view to put it in Point value type instance p. Therefore, all fields in the packed Point object must be copied to the value type variable, which is on the thread stack. CLR completes replication in two steps. The first step is to obtain the address of each Point field in the packed Point object. This process is called unpacking. The second part is to copy the value contained in the field from the heap to the value type instance based on the stack. The unpacking process is not directly reversed. The code for unpacking is much lower than that for packing. Unpacking is actually the process of getting a pointer, which points to the original value type contained in an object. In fact, the Pointer Points to the unpacked part of the boxed instance. Therefore, unlike packing, tea fragrance does not require any bytes to be copied in the memory. After knowing the important difference, we should also know that the key point is that field replication is followed. If the variable containing "reference to the boxed value type" is null, an NullReferenceException is thrown. 2, if the referenced object is not a boxed instance of the required value type, an InvalidCastException is thrown. The second exception means that the Code may work in a different way: public static void Main () {Int32 x = 5; Object o = x; // bind x. o references the boxed Object Int16 y = (Int16) o; // throw an InvalidCastException} logically, the boxed Int32 referenced by o can be obtained and forcibly converted to int16. however, when the object is unpacked, it can only be converted to a value type not originally boxed-Int32 in this example. The following is the correct syntax: public static void Main () {Int32 x = 5; Object o = x; // install x Box, o references the boxed object Int16 y = (Int16) (Int32) o; // first unpack the box to the correct type, and then transform} in the following code: public static void Main () {Point p; p. x = p. y = 1; Object o = p; // bind p. o references the boxed instance. // change the x field of the Point to 2 p = (Point) o; // unpack o and copy the field from the boxed instance to the stack Variable p. x = 2; // change the status of the stack variable o = p; // pack p, o reference a new boxed instance} the only purpose of the last three lines of code is to change the x field of the Point from 1 to 2. to solve this problem, you need to first unpack the box, copy the field once, change the field (on the stack), and finally run the boxing (create a new boxed instance on the managed stack ). From this we can see the impact of packing and unpacking on application performance. Question: public static void Main () {Int32 v = 5; Object o = v; v = 123; Console. writeLine (v + "," + (Int32) o);} How Many Times have the above Code been boxed?

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.