Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLR - where are reference addresses stored?

Tags:

c#

clr

I am new to programming and this is creating a lot of confusion for me.

Suppose we have the following statement:

Int32 i = 1;

i's content is stored on memory, which will be four bytes : 00000000 00000000 00000000 00000001

How does CLR access this memory location later? Does CLR store the address to this memory block somewhere?

like image 896
user657697 Avatar asked Jan 18 '23 09:01

user657697


1 Answers

System.Int32 is a value type, no references used.

In fact, a local variable may never be in memory at all, if the compiler can find a CPU register to hold it during its entire life.

If it is in memory, its address will be found by adding an offset to the stack pointer (ESP) or to the address of the reference-typed (class in C#) object that contains it.

In the code generated from the JIT, variables of value types are indistinguishable from variables used by native code (there's no object header or anything like that).

like image 122
Ben Voigt Avatar answered Jan 20 '23 22:01

Ben Voigt