Address of variable
How can i find the address of a variable in memory
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Joginder BangerRank #360 · 7K reputationPosted Feb 21, 2015, 4:19 PM EST
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();
}
}
Prasham SabadraRank #116 · 19.3K reputationPosted Feb 21, 2015, 6:59 PM EST