locked
view address of variables in C#?????? RRS feed

  • Question

  • hi all!
    How do I view address of variables in C#?

    Tuesday, March 3, 2009 5:07 PM

Answers

  • This can be done, but you have to use unsafe code.  Beware that many variables will move around as the Garbage Collector does it's thing, so this code is truly "unsafe", which is why you'll have to enable the option to allow unsafe code in your project, and place this code in an unsafe bracket.  C# really wants you to know that you're doing something it doesn't like.

    Ahmed is correct, you'd better have a really good reason for doing this:

    public static void Main(string[] args)

    {

        Console.WriteLine("Creating an integer value with the value of 44");

        int value = 44;

        unsafe

        {

            int* memaddress = &value;

     

            // write out the location in memory.

            Console.WriteLine("Memory Location: {0}", (int)memaddress);

     

            // read from memory to validate.

            IntPtr ptr = new IntPtr((int)memaddress);

            int myCopiedValue = (int)Marshal.PtrToStructure(ptr, typeof(int));

     

            // write the copied value to be sure.

            Console.WriteLine("Copied Value: {0}", myCopiedValue);

        }

     

        Console.WriteLine("Press any key...");

     

        Console.ReadLine();

    } 


    David Morton - http://blog.davemorton.net/
    • Marked as answer by Bin-ze Zhao Thursday, March 5, 2009 7:28 AM
    Tuesday, March 3, 2009 5:30 PM
    Moderator

All replies

  • I don't believe this is possible - is there a reason you wish to do this?
    Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
    Tuesday, March 3, 2009 5:16 PM
  • This can be done, but you have to use unsafe code.  Beware that many variables will move around as the Garbage Collector does it's thing, so this code is truly "unsafe", which is why you'll have to enable the option to allow unsafe code in your project, and place this code in an unsafe bracket.  C# really wants you to know that you're doing something it doesn't like.

    Ahmed is correct, you'd better have a really good reason for doing this:

    public static void Main(string[] args)

    {

        Console.WriteLine("Creating an integer value with the value of 44");

        int value = 44;

        unsafe

        {

            int* memaddress = &value;

     

            // write out the location in memory.

            Console.WriteLine("Memory Location: {0}", (int)memaddress);

     

            // read from memory to validate.

            IntPtr ptr = new IntPtr((int)memaddress);

            int myCopiedValue = (int)Marshal.PtrToStructure(ptr, typeof(int));

     

            // write the copied value to be sure.

            Console.WriteLine("Copied Value: {0}", myCopiedValue);

        }

     

        Console.WriteLine("Press any key...");

     

        Console.ReadLine();

    } 


    David Morton - http://blog.davemorton.net/
    • Marked as answer by Bin-ze Zhao Thursday, March 5, 2009 7:28 AM
    Tuesday, March 3, 2009 5:30 PM
    Moderator
  • As David says, the garbage collector (GC) will move things around in memory so if you are talking about member variables of classes rather than local variables (as in David's example) you need to tell the GC not to move the object when you are using the address - this mechanism is known as pinning. The normal reason for needing to pin an object is because you want to pass a reference to it to native code (out of the control of the GC). Luckily the interop layer pins for you during the native call.

    However, if you just want to take the address of a member variable you need to manually pin using the fixed statement - e.g.

    class Foo
    {
        int myValue;

        public unsafe int GetValueAddress()
        {
            int val = 0;

            fixed( int* p = &myValue )
            {
                 val = (int)p;
            }

            return val;
        } 
    }
    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Tuesday, March 3, 2009 5:50 PM
  • But please don't use the results of that function for anything, as soon as you unpin (outside the fixed part of the code) the GC is free to move stuff around so there's no guarantee that 1 ms after calling that function the variable would still be at the same address.

    Tuesday, March 3, 2009 9:36 PM
  • Absolutely - but if you were, say, plotting a graph to show how the allocation works with GC that would be ok.

    Its a raw memory address that could be invalid to touch as soon as you leave the fixed block
    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Tuesday, March 3, 2009 9:40 PM