views:

188

answers:

2

I've discovered while trying to use a luaBind-style class binding system that passing pointers to member variables doesn't seem to work right when compiling as a 64 bit app. Specifically:

class Foo {
    int a;
    int b;
};

With the above class getting &Foo::b in 32 bit will return (as expected) 0x00000004. The same call in 64 bit returns 0xCCCCCCCC00000004, which is 32 bits of correct and 32 bits of WTF.

My first thought is that this is a bug in the compiler (I'm using Visual Studio 2005 SP1 on Vista 64 Business), but that seems like a pretty big bug for a IDE that proudly claims 64 bit compatibility. Googling turns up nothing (but I may be using the wrong term. Member Variable Pointer? Anyone care to correct me?), so I'm curious if this is just me or a more widespread issue.

+2  A: 

Pointers to members aren't necessarily as simple as you might expect. Consider:

#include <iostream>

struct Foo {
    int a;
};


struct Bar {
    int b;
};

struct Baz : public Bar, public Foo {
    int c;
};

struct Quux : public Bar {
  int d;
};

int main() {
  Baz x;
  x.a = 10;
  int (Foo::*ptr) = &Foo::a;
  using namespace std;

  cout << x.*ptr << endl;
  return 0;
}

You really shouldn't "expect" any particular bit pattern in a pointer to member. They may point to a thunk, they may be an offset, who knows. Also in the 64bit case, are you sure the pointer to member is 64 bits when you printed it? Pointers to member don't have to be the same size as other pointers (and often aren't).

This article may also be englightening, all though it deals mostly with pointers to member functions.

Logan Capaldo
+4  A: 

Try printing sizeof(&Foo::b).

IIRC, in 64-bit VS2005 a pointer to member could occupy 4 or 12 bytes (depending on virtual/multiple inheritance), but IDE always displays 8 bytes (which (IMHO) is a bug in IDE ).

Employed Russian
Agreed, debugger bug.
Hans Passant
Wonder of wonders. sizeof gives me 4! :) So this is a debugger issue indeed. Go figure! Thanks.
Toji