Code Geek
Code Geek

Reputation: 39

Can reference types be allocated on stack?

Consider following code:

class Point
{
   public int position;
}

here the int position will be allocated on heap even if it is a value type.

but my question is about the opposite scenario,consider following code:

struct Person
{
   public string name;
}

Here will string name be allocated on heap or stack?

TIA

Upvotes: 1

Views: 1823

Answers (3)

in your case - string allocates on heap.

but reference types can be allocated on stack.

for exemplar, arrays can be allocated as:

private static void Main(string[] args)
        {
            unsafe
            {
                const int size = 123;
                var mgmtArray = new long[size];
                mgmtArray[0] = 12345;

                void* refAddr = Unsafe.AsPointer(ref mgmtArray);

                long mgmtArrAddr = *(long*)refAddr;
                long mtAddr = *(long*)mgmtArrAddr;
                long arrSize = ((int*)mgmtArrAddr)[2];

                Console.WriteLine("Addr ref: {0:x}", (long)refAddr);
                Console.WriteLine("Mgmt array addr: {0:x}", mgmtArrAddr);
                Console.WriteLine("MT: {0:X}", mtAddr);
                Console.WriteLine($"{nameof(arrSize)}: {arrSize}");

                long* stackPtr = stackalloc long[size * 2];

                stackPtr[0] = 0; //sync block
                stackPtr[1] = mtAddr;
                ((int*)stackPtr)[4] = size;
                stackPtr++;

                long** stackPtrRef = &stackPtr;

                var pointer = new MgmtPointer<long[]>(Unsafe.Read<long[]>(stackPtrRef));

                long[] arrayOnStack = Unsafe.Read<long[]>(stackPtrRef);

                arrayOnStack[0] = 54321;

                Console.WriteLine("-----");

                Console.WriteLine($"In heap: {mgmtArray[0]}"); //12345
                Console.WriteLine($"In stack: {arrayOnStack[0]}"); //54321
                Console.WriteLine($"arrayOnStack len: {arrayOnStack.Length}");
            }
        }

for other reference types also, but with some changes.

Upvotes: 0

Roman Ambinder
Roman Ambinder

Reputation: 369

Reference types actual instance object is allocated on the heap and its' reference variable, that references it is allocated on the stack.

Just as a brain teaser: When we take a value type cast it to a reference type, for example and object (everything is an object in .net ) or and interface (reference type) it's implementing then value type is boxed, meaning copied to the heap and it's being referenced from the stack. If we cast the object back to value type , it's being unboxed back to a value type and copied entirely to the stack. Some people tend to wrongly consider this scenario as a reference type allocated on the heap.

Upvotes: 1

Esben Skov Pedersen
Esben Skov Pedersen

Reputation: 4517

No. The reference to the string will be allocated where ever Person is allocated. The string itself will be allocated on the heap. Infact everytime you say new T where T is a class the object will be allocated on the heap.

Upvotes: 1

Related Questions