Reputation: 181
I.e. what is the name of the Value Type data structure stored on the stack, that references an object on the heap?
I.e. an object reference is System.What(if anything)?
I know it's not a pointer, or is it?
I know it's "like" a pointer.
I know it is "owned by the garbage collector".
I SUPERSTITIOUSLY believe that when you instantiate an object (i.e. a reference type), that the IL gives the instruction to the CLR to allocate the "stack reference/heap value pair" in memory (e.g.
...
.locals init ([0] int32 i1,
[1] object o1,
[2] int64 l1)
IL_0000: nop
IL_0001: ldc.i4.4
IL_0002: stloc.0
IL_0003: newobj instance void [mscorlib]System.Object::.ctor()
IL_0008: stloc.1
IL_0009: ldloc.0
IL_000a: box [mscorlib]System.Int32
...
) , and that the actual reference is inaccessible at the language or IL level (except for copying/assigning to a new reference or manipulating the referenced object), and that the object reference's allocation is created and managed by the CLR/CLI and the CLR injects an OBJECTREF DWORD value into the stack (or similar).
Please set me straight and put an end to this for all book authors and Google searches.
Thanks!
Upvotes: 5
Views: 210
Reputation: 81660
Value Type created on the stack does not have a reference to any object on the heap - there is no corresponding object on the heap. This will be only created when value type is boxed - in which case still there will be no relationship between these two.
That is why when you change the value of the boxed reference type, original variable stays the same. Whenever you box, a new object is created.
Here is a simple method:
private void TestBoxing()
{
int i = 52;
object io = i;
io = "something else";
}
And here is the IL code generated:
.method private hidebysig instance void TestBoxing() cil managed
{
// Code size 18 (0x12)
.maxstack 1
.locals init ([0] int32 i,
[1] object io)
IL_0000: nop
IL_0001: ldc.i4.s 52
IL_0003: stloc.0
IL_0004: ldloc.0
IL_0005: box [mscorlib]System.Int32
IL_000a: stloc.1
IL_000b: ldstr "something else"
IL_0010: stloc.1
IL_0011: ret
} // end of method Program::TestBoxing
As you can see, this .locals init ([0] int32 i, [1] object io)
is nothing but initiating local variables, one int and one object.
I was asking what is the specific type of the reference on the stack which refers to the object on the heap
It is plain object reference, similar to all object references which refers to a point in memory which has a 4 byte type pointer and 4 byte sync block and then rest is as much space as the object requires, in our case int needs 4 bytes so 12 bytes.
Well, I do not think it has a name, it is CLR's internal pointer (32bit or 64bit depending on the runtime) which has no use outside CLR and is not exposed. As you know and said, it is managed by GC since the value will be changing after objects move on the heap after garbage collection.
Upvotes: 4
Reputation: 2387
It's a pointer, sized to the native word length (ie, 32-bit pointer on x86, 64-bit pointer on x64). It doesn't have a CTS-exposed 'type' in the sense that I think you mean.
Upvotes: 2