Tell adress of object from adress of member

This is a discussion on Tell adress of object from adress of member within the C++ Programming forums, part of the General Programming Boards category; If o is an object and m is a member of the object, and you know the adress of m, ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Tell adress of object from adress of member

    If o is an object and m is a member of the object, and you know the adress of m, can you then tell the adress of o?
    Come on, you can do it! b( ~_')

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,662
    you can use the offsetof macro, but it won't work for members that are references.



    ITSA
    Socket Library!

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Nor will it work if the member is a function, of course.

    Furthermore, offsetof may only be used for PODs, just like the necessary pointer casts are valid only for PODs.

    POD (Plain Ol' Data) types are, succinctly put, those that can be written in C. (That's not quite accurate, but sufficiently so.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It CAN be done, but as explained, there are MANY restrictions, the most obvious covered above.

    Linux (and related) have a macro to get a base-pointer from a member of a function, like this:
    Code:
    #define container_of(ptr, type, member) ({                      \
            const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
            (type *)( (char *)__mptr - offsetof(type,member) );})
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The most portable way to do it would be if you had a pointer to a member function -- in which case you could just return this -- or make m itself have a member pointing to o. Or just have a pointer to o in the first place.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Basically, in any situation where you can assume that the pointer you actually does point to a member of a struct, and you find you need a pointer to that struct but don't have one, you're facing a design error. You should have provided a pointer to the struct to the component in question in the first place.
    In most situations, you can't assume that you've got a member.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator+ loses pointer in member object
    By ConceptOfZero in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2009, 11:48 AM
  2. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  3. Replies: 1
    Last Post: 04-17-2008, 07:45 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM

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