8

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.

madhead
  • 31,729
  • 16
  • 153
  • 201
Zeb-ur-Rehman
  • 1,121
  • 2
  • 21
  • 37
  • This question is effectively a duplicate of [this question](http://stackoverflow.com/q/13049/1730559). Although you are talking about an array of reference or value types rather than individual variables, might as well be the same question though. – kjbartel Apr 16 '15 at 05:49
  • well thanks jason for your interest. I think my question is bit different then what you are suggesting because in my case i know what's the difference between Structs and Class, I'm interested to know how memory allocation is done in C# in case of Array of Structs and Array of Class Objects. Once again thanks for you help. – Zeb-ur-Rehman Apr 16 '15 at 09:12
  • The fact you are asking the question shows me that in fact you do not understand the difference between value and reference types in C# because the difference is memory allocation. As DrKoch shows, it's either an array of references to objects on the heap or an array of the structs (or other value type) as one contiguous block of memory. So whether it's in an array or just a variable it is exactly the same. Therefore this is a duplicate. – kjbartel Apr 16 '15 at 09:27

2 Answers2

28

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

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

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.

Community
  • 1
  • 1
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
7

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)

DrKoch
  • 9,556
  • 2
  • 34
  • 43