Thread: Tell adress of object from adress of member

  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,708
    you can use the offsetof macro, but it won't work for members that are references.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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,057
    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,895
    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