Datatype misalignment
-
Hello! I have a structure that contains several informations. This structure can be read directly from a file. In this file I have a "memory copy" of the structure, so I just have to make a fread (with the adress of the structure as first parameter) to import the complete structure. For compatibility, I NEED (!!) to have this structure with a packing alignment of 1 byte:
#pragma pack(1)
struct SOMESTRUCT
{
.... //Some fields here
char data[]; //Buffer that will hold data in a single block
}
#pragma pack() //restore to original byte alignment settingThis works fine when I read the structure in the file. The problem occurs when I try to "put" data in the data field:
SOMESTRUCT* temp = .....; //Initialized here from another function, works fine
int SomeValue = 3;
*((int*)temp->data)=SomeValue; //Exception thrown here: datatype misalignment !This works fine on the emulator but not on the remote device !! So, I really don't know what to do!! Thanks for help :) !
-
Hello! I have a structure that contains several informations. This structure can be read directly from a file. In this file I have a "memory copy" of the structure, so I just have to make a fread (with the adress of the structure as first parameter) to import the complete structure. For compatibility, I NEED (!!) to have this structure with a packing alignment of 1 byte:
#pragma pack(1)
struct SOMESTRUCT
{
.... //Some fields here
char data[]; //Buffer that will hold data in a single block
}
#pragma pack() //restore to original byte alignment settingThis works fine when I read the structure in the file. The problem occurs when I try to "put" data in the data field:
SOMESTRUCT* temp = .....; //Initialized here from another function, works fine
int SomeValue = 3;
*((int*)temp->data)=SomeValue; //Exception thrown here: datatype misalignment !This works fine on the emulator but not on the remote device !! So, I really don't know what to do!! Thanks for help :) !
cedric moonen wrote: This works fine on the emulator but not on the remote device !! If I remember correctly, datatypes like int needs to be placed on even (or was it divisble by 4) memory addresses on Windows CE. Its unclear to me if your char data[] will be placed at an illegal address for an int, but that might be it. A workaround would be using byte-wise copy, or perhaps re-order the struct. Jonas “Our solar system is Jupiter and a bunch of junk” - Charley Lineweaver 2002
-
cedric moonen wrote: This works fine on the emulator but not on the remote device !! If I remember correctly, datatypes like int needs to be placed on even (or was it divisble by 4) memory addresses on Windows CE. Its unclear to me if your char data[] will be placed at an illegal address for an int, but that might be it. A workaround would be using byte-wise copy, or perhaps re-order the struct. Jonas “Our solar system is Jupiter and a bunch of junk” - Charley Lineweaver 2002
Hi, thanks for suggestion. I replaced *((int*)temp->data)=SomeValue; by a memcpy and the bug disapeared !! Thanks a lot :) :)