How to retrieve an object's address?

This is a discussion on How to retrieve an object's address? within the C# Programming forums, part of the General Programming Boards category; Hi, is there a way to retrieve the address of an object? (Or some id that is unique to the ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    How to retrieve an object's address?

    Hi,

    is there a way to retrieve the address of an object? (Or some id that is unique to the object)

    I want to define the identity of an Edge based on the addresses of the Nodes it refers to.
    But because GetHashCode doesnt guarantee uniqueness, this solution is unsafe:

    Code:
       public int CompareTo(Edge other)
                {
     	            int result = from.GetHashCode() - other.from.GetHashCode();
                    if(result != 0)
                        return result;
    
                    return to.GetHashCode() - other.to.GetHashCode();
                }
    signature under construction

  2. #2
    Registered User
    Join Date
    Apr 2008
    Location
    USA
    Posts
    22

    Question

    MSDN online or MSDN Express Edition.

    There are two ways of getting an id for and object and the hash method is a generic method and the one that I had seen most frequently used. There is another that is quite similar to the methods utilized in C++ to get an objects signature, but I can't recall the way of going about to get it. I have seen references to the method in the MSDN Express edition and I personally recommend getting it (MSDN Express Ed.) if you do not have it already.

    A couple possible leads that might get you started are: comparing objects through equality, dynamically creating and/or destroying objects, and look up .Net assembly files and IL Dasm to access your assemblies to search for object ID references.

    A really good book I am reading right now that addresses these types of Intermediate to Advanced Professional Programming level topics is Pro C# 2008, and the .Net 3.5 Platform published by Apress Books. Excellent author and seriously deep treatment of C# and the .Net Framework from a hacker oriented/reverse engineering point of view geared toward getting a certification.
    Last edited by TheRaven; 04-29-2008 at 11:22 PM.

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    I had this exact problem once.

    The short answer is you can but it wouldn't work. C# allows objects to be moved around in memory, so the same actual object might occupy different memory locations at different times.

    My own method was to simply count nodes:


    Code:
    public class Node {
    
          private static int mCount = 0;
    
          private int ID;
    
          public Node()
          {
                ID = Interlocked.Increment(mCount);
          }
    The Interlocked.Increment in place of just "++mCount" is for thread safety.

    This one obviously doesn't reuse old IDs, and it has a hard limit of 2^32 possible ID values before you wrap all the way back to ID #1 again. That wasn't an issue for me, maybe it won't matter for you either.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 03:12 AM
  2. I am very new . . . :(
    By Eternalglory47 in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2008, 12:29 PM
  3. Replies: 10
    Last Post: 09-04-2008, 02:27 PM
  4. address out of bounds in sockets program
    By newbie_socketsp in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-05-2008, 07:41 AM
  5. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 06:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21