Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when assign new struct to array in C#?

Say I have a C# struct:

struct Foo{
    int mA;
    public int A {get {return mA;}}
    int mB;
    public int B {get {return mB;}}

    public Foo(int a, int b)
    {
        mA = a;
        mB = b;
    }
}

And then I create and array of Foo's:

Foo[] foos = new Foo[10];

What happens when I do this?

foos[1] = new Foo(20, 10);

If Foo was a class, the Foo[] would hold a pointer to a Foo object on the heap, and that pointer would be changed to the new Foo object (the old one being left for recycling).

But since structs are value types, would the new Foo(20, 10) just physically overwrite the same memory location previously held by foos[1]?

like image 425
CodeFusionMobile Avatar asked Jul 06 '12 02:07

CodeFusionMobile


People also ask

Can you put a struct in an array?

Each element of the array can be int, char, float, double, or even a structure. We have seen that a structure allows elements of different data types to be grouped together under a single name. This structure can then be thought of as a new data type in itself. So, an array can comprise elements of this new data type.

Can you have an array of structs in C?

An array of structres in C can be defined as the collection of multiple structures variables where each variable contains information about different entities. The array of structures in C are used to store information about multiple entities of different data types.

Can you assign one struct to another in C?

Yes, you can assign one instance of a struct to another using a simple assignment statement. In the case of non-pointer or non pointer containing struct members, assignment means copy. In the case of pointer struct members, assignment means pointer will point to the same address of the other pointer.

Why use a struct over an array?

struct has other advantages over array which can make it more powerful. For example, its ability to encapsulate multiple data types. If you are passing this information between many functions, a structure is likely more practical (because there is no need to pass the size).


1 Answers

In practice the memory associated with the relevant array slot is populated by the values. Given your code a small example shows what goes on. Please see comments inline. This is for a release build.

static void Main(string[] args)
{
    Foo[] foos = new Foo[10];
    foos[1] = new Foo(127, 255);
    Console.ReadLine();
}

The code above is JIT compiled as follows

// Method setup
00280050 55              push    ebp
00280051 8bec            mov     ebp,esp
00280053 56              push    esi

// Create instance of Foo[]
00280054 b98a141d00      mov     ecx,1D148Ah
00280059 ba0a000000      mov     edx,0Ah
0028005e e8b121f4ff      call    CORINFO_HELP_NEWARR_1_VC (001c2214)
00280063 8bd0            mov     edx,eax

// Array range check 
00280065 837a0401        cmp     dword ptr [edx+4],1
00280069 7624            jbe     

// Assign foos[1] = new Foo(127, 255)  
0028006b 8d4210          lea     eax,[edx+10h]  <-- load location of foos[1] in eax
0028006e ba7f000000      mov     edx,7Fh        <-- load 127 in edx
00280073 beff000000      mov     esi,0FFh       <-- load 255 in esi
00280078 8910            mov     dword ptr [eax],edx    <-- move the value 127 to foos[1]
0028007a 897004          mov     dword ptr [eax+4],esi  <-- move the value 255 to foos[1] + offset

// This is just for the Console.ReadLine() part + rest of Main
0028007d e8d2436305      call    mscorlib_ni!System.Console.get_In() (058b4454)
00280082 8bc8            mov     ecx,eax
00280084 8b01            mov     eax,dword ptr [ecx]
00280086 8b402c          mov     eax,dword ptr [eax+2Ch]
00280089 ff501c          call    dword ptr [eax+1Ch]

// Epilog
0028008c 5e              pop     esi
0028008d 5d              pop     ebp
0028008e c3              ret

//Exception handling
0028008f e8f05e7f70      call    clr!JIT_RngChkFail (70a75f84)
00280094 cc              int     3

So in short, the code loads the constants in registers and then copies values of these registers to the memory associated with the relevant part of the array instance.

like image 141
Brian Rasmussen Avatar answered Oct 31 '22 22:10

Brian Rasmussen