Thread: get address of object in .net

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838

    get address of object in .net

    i'm looking for a .net friendly to the equivalent c++

    Code:
    Foo* f;
    unsigned int faddress = (unsigned int)f;
    (IntPtr)gchandle.alloc(f) does not work. i need a consistent address for each instance; evidently this does not work (though it used to and now i have code written around this faulty paradigm)

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It sounds like you might be barking up the wrong tree. You can't rely on the framework to give you the same address every time. What problem are you actually trying to solve?
    If you understand what you're doing, you're not learning anything.

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    i just need a unique identifier for the object. the address is a very handy one in native code. if .net will spontaneously realloc objects under the hood, then i'll have to implement an index.

    thanks.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Any references to an object are guaranteed to always point to the object. Is this for state data or something? I'm really interested in when this need might arise.
    If you understand what you're doing, you're not learning anything.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    it's for dispatching messages on a distributed system.

    in C++, i just serialized the addresses of the objects sending and receiving messages, it worked wonderfully.

    this was more crucial in C++ when i needed an instance to invoke a method pointer, the real benefit of still doing this in .net is not having to have 1 master unique message code list, by doing a 2-stage lookup (instance, then messageTypeID), each user of this class can define their own message types without having to worry about syncing with what everyone else is doing.

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    is there some way that i can change that reference into a serializable identifier that is const for the lifetime of the object?

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You are comparing apples and oranges. In C++ you have pointers and in C# you have handles. The handles are not pointers themselves but are handles to the underlying pointers. These addresses can be moved around at will by the runtime and therefore grabbing their address without pinning them is bound to cause problems. You should not be thinking of C# in terms of pointers but rather references to objects. You can use various hash algorithms to do what you want.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    You can do this in C#:

    int* ptr = &obj;
    OR
    IntPtr p = (IntPtr)(int*)&obj;

    the function which contains this line of code must have unsafe modifier. C# datatype for pointers is IntPtr.
    The above code will only work if obj is a value type object like int, char, float or struct. For reference types (like string, StringBuilder, or class) it will not work. Reference type objects are maintained by CLR and you can not get their address.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    What exactly do you mean by serializable identifier? As far as i know a value type object can be boxed\unboxed as reference type but a reference type cannot be converted or assigned to a value type variable. And you cannot get the address of Reference type variables in C#.

    ---------------
    TradeM
    Retail POS Software - TradeMeters&#8482 is a Reliable Point of Sale Software Solution Retail pos software

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    If I am hearing you correctly, your original design is heavily flawed.


    You are dispatching messages ( remotely? ) and using the objects memory address as it's unique identifier? What happens when that object is de-allocated and a new one is allocated in the same address space, a not unlikely scenario. If you just need a unique identifier for created objects, it's time stamp is a much much much safer alternative and that can easily be implemented across platforms/languages.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-07-2010, 11:39 PM
  2. Hash function - Object ID/IP address to 2D co-ordinates
    By kerrymaid in forum C Programming
    Replies: 3
    Last Post: 09-28-2010, 11:00 AM
  3. How to retrieve an object's address?
    By Raven Arkadon in forum C# Programming
    Replies: 2
    Last Post: 05-01-2008, 02:47 AM
  4. Block address from word address
    By xddxogm3 in forum Tech Board
    Replies: 0
    Last Post: 04-25-2007, 09:02 PM