Address of variable
How can i find the address of a variable in memory
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prasham SabadraPosted Feb 21, 2015, 11:59 PM
Joginder BangerPosted Feb 21, 2015, 9:19 PM
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
class Blittable {
int x;
}
class Program
{
public static unsafe void Main()
{
int i; object o = new Blittable();
int* ptr = &i;
IntPtr addr = (IntPtr)ptr;
Console.WriteLine(addr.ToString("x"));
GCHandle h = GCHandle.Alloc(o, GCHandleType.Pinned);
addr = h.AddrOfPinnedObject();
Console.WriteLine(addr.ToString("x"));
h.Free();
}
}