I get wrong value when accessing variabel v2 using their memory location when HWND is before bool variable. If Ii use HWND after bool then I get correct result. Using instance variable (t) I get correct value for v1 and v2 such as t->v1 and t->v2. I am using Windows Server 2003. I have the following Test class. this is only reproducible in 64 bit OS; 32 bit OS work fine.
#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;
}