當HWND位於布爾變量之前時,使用它們的內存位置訪問變量字段v2
時出現錯誤值。如果在bool之後使用HWND,那麼我會得到正確的結果。使用實例變量(t
)我得到正確的值爲v1
和v2
,例如t->v1
和t->v2
。我正在使用Windows Server 2003.我有以下測試類。這隻能在64位操作系統中重現; 32位操作系統工作正常。通過內存位置訪問變量
#include "conio.h"
#include "stdio.h"
include "windows.h"
class Test
{
public :
Test()
{
v1=12345678;
v2=87654321;
}
HWND hWnd;
bool MsgHandled;
unsigned long v1;
unsigned long v2;
};
int _tmain(int argc, _TCHAR* argv[])
{
Test* t=new Test();
unsigned long sign1 = *(unsigned long*)((unsigned char*)t + sizeof(Test)-2*sizeof(unsigned long));
unsigned long sign2 = *(unsigned long*)((unsigned char*)t + sizeof(Test)-sizeof(unsigned long));
printf("\nTest size %d",sizeof(Test));
printf("\n t->v1 %d",t->v1);
printf("\n t->v2 %d",t->v2);
printf("\n v 1 %d",sign1);
printf("\n v 2 %d",sign2); // garbage value in 64 bit os
getch();
return 0;
}