Custom memory allocation in C# Part 7 — Stack allocation once again

This is the seventh part of the Custom memory allocation series. For your convenience you can find other parts in the table of contents in Part 1 — Allocating object on a stack

We know how to allocate object on a stack, however, we did this in .NET Framework. Can we do the same in .NET Core? Let’s see. I am using .NET Core 2.1.103 x64 on W10 Enterprise x64.


Since I am using an 64-bit SDK, I need to change the code. Below is the modified version:

It is basically the same code as in Part 1, however, instead of casting pointers to int I use ulong type. ALso, the syncblock is now 8 bytes before the metadata.


Let’s see the WinDBG session. We start the application, wait for first ReadLine and attach the debugger:

Nothing special here, let’s examine the stack:

We can see the 0badf00d markers. Let’s continue the application, and after one step check the stack again:

Yes, this looks correct. Let’s verify with the object metadata directly:

The MethodTable part is correct so the whole object is copied to the stack correctly. We can verify this differently:

So we dump the object directly from the stack and it works.


There was no TypedReference in .NET Core for a long time. Right now it is available so we can toy with memory and check what has been changed.