Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 322
Zeb-ur-Rehman Avatar asked Apr 16 '15 05:04

Zeb-ur-Rehman


People also ask

How do you allocate memory for an array of structs?

The memory can be allocated using the malloc() function for an array of struct . This is called dynamic memory allocation. The malloc() (memory allocation) function is used to dynamically allocate a single block of memory with the specified size. This function returns a pointer of type void .

How is memory allocated to struct?

If we create an object of some structure, then the compiler allocates contiguous memory for the data members of the structure. The size of allocated memory is at least the sum of sizes of all data members. The compiler can use padding and in that case there will be unused space created between two data members.

What is memory allocation in array?

If an array is 3D or multidimensional array, then the method of allocating memory is either row major or column major order. Whichever is the method, memory allocated for the whole array is contiguous and its elements will occupy them in the order we choose – row major or column major.

What section of memory will you allocate the struct instances in?

Structs are allocated on the stack, if a local function variable, or on the heap as part of a class if a class member.


1 Answers

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.

like image 147
Martin Liversage Avatar answered Oct 07 '22 08:10

Martin Liversage