Help... Due to the way the compiler lines up memory, i cannot assign member values using a pointer offset.
typedefs used:
BYTE is unsigned char(1 byte),
UINT is unsigned short (2 bytes)
coincedentally, if the struct size comes out as a even number of bytes, it works fine!Code:[.h]: typedef struct struct_System_Settings { BYTE byTestBYTE2; UINT nTestUINT1; BYTE byTestBYTE3; BYTE byTestBYTE4; UINT nTestUINT2; }SYSTEM_SETTINGS; SYSTEM_SETTINGS *pSystemSettings; // global pointer // sizeof(SYSTEM_SETTINGS) returns 8 when it is actually 7 bytes, due to memory alignment ! arr.... [.c]: // objective: plug values into the struct WITHOUT knowing the // member names (read from file, format <size in bytes><data>): for(;;) { // get data type (BYTE or UINT): if(fread(&byDataSize, 1, 1, pFile)==0) break; // end of file, complete // byDataSize: 1=BYTE, 2=UINT // get data: nData = 0; if(fread(&nData, byDataSize, 1, pFile)==0) break; // end of file, complete // copy to struct: if(byDataSize == 1) // BYTE memmove((BYTE*)pSystemSettings + x, &nData, 1); // (BYTE = 1 Byte ) else // UINT memmove((BYTE*)pSystemSettings + x, &nData, 2); // (UINT = 2 Bytes) x += byDataSize; // goto next member } // file read complete data read each time round the loop (all hex): byDataSize | nData | data type ---------------+--------------+------------ 01 | F1 | BYTE 02 | A1A1 | UINT <-- curruption here (nTestUINT1 is not = A1A1 !) 01 | F2 | BYTE 01 | F3 | BYTE 02 | A2A2 | UINT
Please help. There must be a way!



LinkBack URL
About LinkBacks


