We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Print the value of a object's memory address?

ChrisClement
ChrisClement asked
on
Medium Priority
338 Views
Last Modified: 2008-03-06
Does anyone know the C# equivalent of...

cout << &MyClass;

?

I need to print the memory address of an object in C#.  I've tried using unsafe code and the fixed statement which will let me set a primitive or unmanaged object to a pointer, but I still can't get the value of the memory address.  

Please help.  

Comment
Watch Question

Commented:
Not possible to get the memory address of an object in c#.  If even you did it wouldnt do you any good because the object can be moved around in memory by the garbage collector to keep the heap from fragmenting.

-gp
Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
How about:

        static void Main(string[] args)
        {
            int x = 20;
            int y = 30;

            unsafe
            {
                Console.WriteLine((int)&x);
                Console.WriteLine((int)&y);
            }

            Console.ReadLine();
        }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
ok but keep in mind that is not code running within the CLR.  it runs as native code so keep that in mind.
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
And if you are using pointers the you will need to use the "fixed" keyword in order to prevent the GC from invalidating your pointer.

Author

Commented:
But how would you do this with objects rather than primitives?

Author

Commented:
Nevermind.  I figured it out.  I kept getting an error when trying to get the address of a class.   The error was "cannot take the address or size of a variable of a managed type".  Did some checking and found that a struct would work just as well and it can be used in this way.

For example...

      class Class1
      {

            [STAThread]
            static void Main(string[] args)
            {
                  

                  unsafe
                  {
                        Class2 c = new Class2();
                        Class2 d = new Class2();
                        Console.WriteLine((int)&c);
                        Console.WriteLine((int)&d);
                  }

                  Console.ReadLine();
            }

      
            public struct Class2
            {
                  //example struct
            }
      }

Thanks for the help

Commented:
fyi, Structs are on the stack and not the heap.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.