Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the stack entry for a reference type contain?

Tags:

c#

.net

memory

I'm refreshing my memory on how reference and value types work in .NET. I understand that the entry on the stack for a reference type contains a pointer to a memory location on the heap. What I can't seem to find details about is what else the stack entry contains. So, given the following:

Customer customer;
customer = new Customer();

After the first line of code an entry on the stack containing a null pointer will exist. Does that entry also contain the identifying name "customer"? Does it contain type information?

Have I fundamentally misunderstood something?

like image 293
Pseudonymous Avatar asked Jun 21 '16 12:06

Pseudonymous


People also ask

Is a stack a reference type?

Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM . For reference types, the reference is stored on the stack while the object it refers to is stored on the heap. For value types, the object itself is stored on the stack.

What is an example of a reference type?

Examples of reference types include: strings, arrays, objects of classes, etc.

What is a reference type in C#?

A reference type variable contains a reference to data stored in memory, also known as the heap. The heap is most often used for data that has a longer life. You can have more than one variable point to the same referenced data. Objects are an example of a reference type.

What is reference type and value type in C++?

Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable.


1 Answers

Only the pointer to the object is stored, nothing else. Not on the stack, it is stored in a processor register. Only if the method is large and the register is better used for other purposes will it be stored on the stack. Making that the decision is the job of the optimizer. Which is in general fairly unlikely to happen since you'll probably be using properties and methods of the Customer class so keeping the pointer in a register is efficient.

This is otherwise the basic reason why a NullReferenceException can tell you nothing useful about what reference is null. Associating the pointer value back to a named "customer" variable can only be done by a debugger. It needs the PDB file to know the name of the variable, that name is not present in the metadata of the assembly. Which is the basic reason why you cannot use Reflection to discover local variable names and values.

And this is also why debugging only works well when you use it on the Debug build of your project. That disables the optimizer and gets the pointer value to always be stored back to a stack slot, allowing the debugger to reliably read it back. At a significant cost, the basic reason you should only ever deploy the Release build of your project.

like image 95
Hans Passant Avatar answered Oct 27 '22 00:10

Hans Passant