Skip to content

Instantly share code, notes, and snippets.

@PathogenDavid
Created April 20, 2022 11:24
Show Gist options
  • Save PathogenDavid/71acd3797e665ea63ceabd74e32e1ffa to your computer and use it in GitHub Desktop.
Save PathogenDavid/71acd3797e665ea63ceabd74e32e1ffa to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.CompilerServices;
// See Pro .NET Memory page 328
// Primitive static fields are stored in the CLR's native heap along with the module, but user-defined structs are actually boxed and stored on the GC heap
public unsafe static class Program
{
public static int X = 100;
public static int Y = 101;
public static (int, int) Z = (200, 300);
public static void Main()
{
Console.WriteLine($"Address of X: {(IntPtr)Unsafe.AsPointer(ref X)}");
Console.WriteLine($"Address of Y: {(IntPtr)Unsafe.AsPointer(ref Y)}");
Console.WriteLine($"Address of Z: {(IntPtr)Unsafe.AsPointer(ref Z)}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment