2

I have:

class Foo {
  int a;
  int b;
  std::string s;
  char d;
};

Now, I want to know the offset of a, b, s, d given a Foo*

I.e. suppose I have:

Foo *foo = new Foo();
(char*) foo->b == (char*) foo + ?? ; // what expression should I put in ?
4
  • 12
    why do you want that?
    – Naveen
    Mar 12, 2010 at 6:44
  • 1
    And, when do you want that, compile-time or run-time?
    – Arun
    Mar 12, 2010 at 7:00
  • Did you mean to make the members private? (You shouldn't be asking for this to circumvent access specifiers.)
    – visitor
    Mar 12, 2010 at 9:57
  • stackoverflow.com/questions/1107672/… - see if this helps!
    – Narendra N
    Mar 12, 2010 at 11:19

2 Answers 2

8

I don't know exactly why you want the offset of a member to your struct, but an offset is something that allows to to get a pointer to a member given the address of a struct. (Note that the standard offsetof macro only works with POD-structs (which yours is not) so is not a suitable answer here.)

If this is what you want to do, I would suggest using a pointer-to-member as this is a more portable technique. e.g.

int main()
{
    int Foo::* pm = &Foo::b;

    // Pointer to Foo somewhere else in the program
    extern Foo* f;

    int* p = &(f->*pm);
}

Note that this will only work if b isn't private in Foo, or alternative you could form the pointer to member in a member function or friend of Foo.

2
  • 2
    I can't think of a compelling reason for the C++ standard crafters to restrict offsetof to POD types, but it seems to produce expected results with many compilers. So it may work, yet will be unportable.
    – Dan Olson
    Mar 12, 2010 at 9:24
  • 5
    @Dan, in fact C++0x allows application of it to non-PODs, as long as the type is a standard layout type. Mar 12, 2010 at 23:02
1

You should take into account that the offset inside structs are compiler-dependent. Different compilers can put padding zeroes or not not only at the end of the struct, but even in the middle of the struct (though I recognize this is more rare). I.e., the result of messing directly with offsets instead of accessing the members in the "standard way" is undefined behaviour.

The solution given by Charles Balley is the more sensible one. Anyway, I don't quite understand what is your ultimate goal with your question. Specifically,

Foo *foo = new Foo();
(char*) foo->b == (char*) foo + ?? ;

Does not makes sense to me at all. Do you want to assign to foo->b its own offset inside Foo?

Maybe what you are trying to do is to use b inside foo as a char*, in that case the question would be:

Foo *foo = new Foo();
char* ptr == (char*) foo + ?? ;

Maybe you sometimes want to treat b as an int, and sometimes as a char *, in that case, you could try to do the following change (assuming that you simply forgot to put public):

class Foo {
public:
    int a;
    union b {
        int intB;
        char * chrB;
    }
    int c;
    // ...
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.