Structions in Windows
-
I have a series of structures overlayed with a buffer (fields and buffer of a message). This is in shared memory Can someone tell me why the first method I use to set fields in a message do not work correctly. Setting the time tag causes the year data to be wrong - data seems to be 8 bytes wide, even if you cast it to unsigned short.
// Each bit in the 64 byte message is defined in the structure (even spares etc) typedef struct{ unsigned long Msg001_02_01_spare1_w01 : 2; unsigned long Msg001_02_01_msg_code_valid : 1; unsigned long Msg001_02_01_msg_status : 1; unsigned long Msg001_02_01_spare2_w01 : 4; unsigned long Msg001_02_01_spare3_w01 : 8; unsigned short Msg001_02_02_time_time_tag; unsigned long Msg001_02_03_year_1000_s : 4; unsigned long Msg001_02_03_year_100_s : 4; unsigned long Msg001_02_03_year_10_s : 4; unsigned long Msg001_02_03_year_1_s : 4; unsigned long Msg001_02_04_day_10_s : 4; unsigned long Msg001_02_04_day_1_s : 4; unsigned long Msg001_02_04_month_10_s : 4; unsigned long Msg001_02_04_month_1_s : 4; unsigned long Msg001_02_05_hour_10s : 4; unsigned long Msg001_02_05_hour_1s : 4; unsigned long Msg001_02_05_minute_10s : 4; unsigned long Msg001_02_05_minute_1s : 4; unsigned long Msg001_02_06_second_10s : 4; unsigned long Msg001_02_06_second_1s : 4; unsigned long Msg001_02_06_second_0_1s : 4; unsigned long Msg001_02_06_second_0_01s : 4; }Msg001_02; struct TSharedMemory { // Form a union between the message structure and a buffer union { Msg001_02 fields; // Fields in message unsigned short buffer[32]; // Message buffer use in the Send/Recv etc }Msg001_02; etc ... }; TSharedMemory *g_pRigMem; // Pointer to shared memory object etc ..
If I use the following code to set the time tag it affects the year data:-g_pRigMem->Msg001_02.fields.Msg001_02_02_time_time_tag = (unsigned short)timeMilseconds;
Even the following does the same:-g_pRigMem->Msg001_02.buffer[1] = (unsigned short)timeMilseconds;
also the following code seems to do the same for fields in the next word?g_pRigMem->Msg001_02.fields.Msg001_02_03_year_1000_s = year1000s; g_pRigMem->Msg001_02.fields.Msg001_02_03_year_100_s = year100s; g_pRigMem->Msg001_02.fields.Msg001_02_03_year_10_s = year10s; g_pRigMem->Msg001_02.fields.Msg001_02_03_year_1_s = year1s;
If however I set the void* to the location of the data in the shared memory and use the following code it works OK -
I have a series of structures overlayed with a buffer (fields and buffer of a message). This is in shared memory Can someone tell me why the first method I use to set fields in a message do not work correctly. Setting the time tag causes the year data to be wrong - data seems to be 8 bytes wide, even if you cast it to unsigned short.
// Each bit in the 64 byte message is defined in the structure (even spares etc) typedef struct{ unsigned long Msg001_02_01_spare1_w01 : 2; unsigned long Msg001_02_01_msg_code_valid : 1; unsigned long Msg001_02_01_msg_status : 1; unsigned long Msg001_02_01_spare2_w01 : 4; unsigned long Msg001_02_01_spare3_w01 : 8; unsigned short Msg001_02_02_time_time_tag; unsigned long Msg001_02_03_year_1000_s : 4; unsigned long Msg001_02_03_year_100_s : 4; unsigned long Msg001_02_03_year_10_s : 4; unsigned long Msg001_02_03_year_1_s : 4; unsigned long Msg001_02_04_day_10_s : 4; unsigned long Msg001_02_04_day_1_s : 4; unsigned long Msg001_02_04_month_10_s : 4; unsigned long Msg001_02_04_month_1_s : 4; unsigned long Msg001_02_05_hour_10s : 4; unsigned long Msg001_02_05_hour_1s : 4; unsigned long Msg001_02_05_minute_10s : 4; unsigned long Msg001_02_05_minute_1s : 4; unsigned long Msg001_02_06_second_10s : 4; unsigned long Msg001_02_06_second_1s : 4; unsigned long Msg001_02_06_second_0_1s : 4; unsigned long Msg001_02_06_second_0_01s : 4; }Msg001_02; struct TSharedMemory { // Form a union between the message structure and a buffer union { Msg001_02 fields; // Fields in message unsigned short buffer[32]; // Message buffer use in the Send/Recv etc }Msg001_02; etc ... }; TSharedMemory *g_pRigMem; // Pointer to shared memory object etc ..
If I use the following code to set the time tag it affects the year data:-g_pRigMem->Msg001_02.fields.Msg001_02_02_time_time_tag = (unsigned short)timeMilseconds;
Even the following does the same:-g_pRigMem->Msg001_02.buffer[1] = (unsigned short)timeMilseconds;
also the following code seems to do the same for fields in the next word?g_pRigMem->Msg001_02.fields.Msg001_02_03_year_1000_s = year1000s; g_pRigMem->Msg001_02.fields.Msg001_02_03_year_100_s = year100s; g_pRigMem->Msg001_02.fields.Msg001_02_03_year_10_s = year10s; g_pRigMem->Msg001_02.fields.Msg001_02_03_year_1_s = year1s;
If however I set the void* to the location of the data in the shared memory and use the following code it works OKGrahamfff wrote: unsigned long Msg001_02_01_spare3_w01 : 8; unsigned short Msg001_02_02_time_time_tag; unsigned long Msg001_02_03_year_1000_s : 4; I would make the time tag the same format as the rest
unsigned long Msg001_02_02_time_time_tag : 16;
Maybe also pack the structure and union to 1 byte boundaries using
#pragma pack(1)
. You could be running into alignment problems.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
Honoured as one of The Most Helpful Members of 2004