views:

95

answers:

3

I was (purely out of curiosity) trying to find out what the size of an actual reference is when an allocation is made on the stack.

After reading this I still don't know (this answers it only for value types or type definitions), and I still cannot seem to find it anywhere.

So basically imagine a class as follows

class A
{
    string a;
}

Now when an object of type A is instantiated, a reference to the string object would be stored on the stack, now what would the size of the allocation on the stack be?

Disclaimer: If I'm talking complete and utter nonsense please let me know :)

+4  A: 

Just like the size of pointers, presumably, the size would be that of a native int: 32-bits on 32-bit platforms and 64-bits on a 64-bit platform.

Snarfblam
Wow, classic example of overthinking the problem :)
Stephan
Pretty sure this is correct, but it is worth bearing in mind that some virtual machines do use compressed pointers that are smaller than the native integer size for performance / cache reasons - e.g. 32-bit CompressedOops on the 64-bit Hotspot JVM
mikera
+2  A: 

It will be the size of IntPtr, either 32 or 64 bits, depending upon your environment.

jscharf
+2  A: 

Now when an object of type A is instantiated, a reference to the string object would be stored on the stack, now what would the size of the allocation on the stack be?

The string reference would in fact be stored on the heap, not the stack, since A is a reference type.

Mattias S
The *reference* to the string will be stored on the stack, not the heap. The *actual object instance* itself will however be stored on the heap.
Stephan
No, all instance members of A are stored in the object data on the heap, including the string reference.
Mattias S