Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Print the value of a object's memory address?

Posted on 2006-03-24
7
Medium Priority
?
314 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.  

0
Comment
Question by:ChrisClement
  • 3
  • 2
  • 2
7 Comments
 
LVL 13

Expert Comment

by:devsolns
ID: 16284868
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
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 16284951
How about:

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

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

            Console.ReadLine();
        }
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16285007
ok but keep in mind that is not code running within the CLR.  it runs as native code so keep that in mind.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16285012
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.
0
 

Author Comment

by:ChrisClement
ID: 16286796
But how would you do this with objects rather than primitives?
0
 

Author Comment

by:ChrisClement
ID: 16286810
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
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16291188
fyi, Structs are on the stack and not the heap.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question