class member offset error in vc 2003?
Hi, folks
I got a tricky problem here, from assembly code, I noticed that sometimes the offset of a member was wrong.
below is my code:
interface header file Port.h
#pragma once
#include <stdlib.h>
template <class T, class S>
struct Port
{
typedef int (T::*FuncPointer)(const S&);
FuncPointer functionPointer;
T* classPointer;
Port()
{
classPointer = NULL;
functionPointer = NULL;
}
};
class A header file - A.h
#pragma once
#include "port.h"
class B;
class A
{
Port<B, int> port1;
long x0, x1, x2;
public:
A();
};class A source file - a.cpp
#include "a.h"
A::A()
{
x0 = 0;
}
class B header file - B.h
#pragma once
#include "port.h"
class A;
class B
{
Port<A, int> port1;
long x0, x1, x2;
public:
B();
};
class B source file - b.cpp
#include "b.h"
B::B()
{
x0 = 0;
}
and my main.cpp
#include "b.h"
#include "a.h"
void main(int argc, char** argv)
{
A* aPtr = new A();
B* bPtr = new B();
int x = 0;
return;
}
compile and then I found that B's x0, x1, x2 were correct, but A's weren't.
checking assembly code and i found that
when executing A's contructor
x0's offset is 8, but assembly shows that it is updating to to offset 0x18.
also sizeof(port1) gives 8.
when executing B's contructor
sizeof(port1) is 0x18.
if I move A and B's constructor into header file, everything is ok.
if I add more Port member to class A or B, some of their size are 8 while others are 0x18.
anyone got a clue?
many thanks!
baum
Answers
According to another discussion -- http://social.msdn.microsoft.com/forums/en-US/vcgeneral/thread/2793a64f-ec09-495c-b995-4f5b98a26321/ -- try /vmg and /vmb compiler options or #pragma pointers_to_memebers.
- Marked As Answer bybaumchen Tuesday, November 03, 2009 5:16 PM
All Replies
Hi Baum,
I have tested this code in Visual Studio 2008, for I don’t have Visual Studio 2003 available. And the offset of x0,x1,x2 in A Class are all 4 byte ( long type is 4 byte) as same as those in B Class. I don’t know why it is not the same from Visual Studio 2003. And I would suggest you specify value to x1,x2 in constructor and you check again the offset of these value in Disassembly Windows.Please let me know the result.
And I highly suggest you upgrade your development IDE to newer version, such as Visual Studio 2008 or higher, and you will get better support.
Best Regards,
Nancy
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.According to another discussion -- http://social.msdn.microsoft.com/forums/en-US/vcgeneral/thread/2793a64f-ec09-495c-b995-4f5b98a26321/ -- try /vmg and /vmb compiler options or #pragma pointers_to_memebers.
- Marked As Answer bybaumchen Tuesday, November 03, 2009 5:16 PM
Nancy
sorry I don't have 2008, so I can only try on my 2003 ...
I tried adding a few lines in main.cpp
fprintf(stdout, "a: %x,%x,%x\n", aPtr->x0, aPtr->x1, aPtr->x2);
fprintf(stdout, "b: %x,%x,%x\n", bPtr->x0, bPtr->x1, bPtr->x2);
and I got
a: 0, fffffff, 0
b: 0, cdcdcdcd, cdcdcdcd
and, i think u mean size instead of offset by "the offset of x0,x1,x2 in A Class are all 4 byte"?
by the way, adding compiler option /vmg does fix the problem.
thanks!
baum- viorel, thanks a lot!
/vmg does fix the problem.
baum - Hi Viorel,
Thanks Viorel, that's very useful.
Best Regards,
Nancy
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


