简体   繁体   中英

Memory Allocation for an Array of Struct and Class Object

Last day I was reading C# reference and there I saw a statement. Kindly have a look at the following statement.

Context:

the use of a struct rather than a class for a Point can make a large difference in the number of memory allocations performed at run time. The program below creates and initializes an array of 100 points. With Point implemented as a class, 101 separate objects are instantiated—one for the array and one each for the 100 elements.

class Point
{
    public int x, y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
class Test
{
  static void Main() {
          Point[] points = new Point[100];
          for (int i = 0; i < 100; i++)
          points[i] = new Point(i, i*i);
      }
}

If Point is instead implemented as a struct, as in

struct Point
{
     public int x, y;
     public Point(int x, int y) {
         this.x = x;
         this.y = y;
     }
}

only one object is instantiated—the one for the array. The Point instances are allocated in-line within the array. This optimization can be misused. Using structs instead of classes can also make an application run slower or take up more memory, as passing a struct instance by value causes a copy of that struct to be created.

Question: Here my question is how memory allocation is done in case of Value Type and Reference Type?

Confusion: Why it is mentioned in Reference Guide that Only 1 Object will be intialized. As per my understanding for each object in Array a separate memory will be allocated.

Edit: Possible Duplicate This question is bit different from possible duplicate question as suggested by jason. My concern is about how memory is allocated in case of Value Type and Referenece Type solely while that question just explain the overview of Value Type and Reference Type.

Perhaps the difference between an array of a reference type and an array of a value type is easier to understand with an illustration:

Array of a reference type

引用类型的数组

Each Point as well as the array is allocated on the heap and the array stores references to each Point . In total you need N + 1 allocations where N is the number of points. You also need an extra indirection to access a field of a particular Point because you have to go through a reference.

Array of a value type

值类型的数组

Each Point is stored directly in the array. There is only one allocation on the heap. Accessing a field does not involve indirection. The memory address of the field can be computed directly from the memory address of the array, the index of the item in the array and the location of the field inside the value type.

An array with reference types will consist of an array of references. Each reference points to a memory area which contains the actual object:

array[0] == ref0 -> robj0
array[1] == ref1 -> robj1
...

So there is one memory allocation for the array of references (size: arraylength * sizeof(reference)) and a separate memory allocation for each object (sizeof(robj)).

An array with value types (like structs) will contain just the objects:

array[0] == vobj0
array[1] == vobj1
...

so there is jst one memory allocation with size arraylength * sizeof(vobj)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM