I do have troubles with the alignment of data which I read
from a file. The data in the file are:
aaaa bbbb bbbb cccc cccc dddd dddd ...
I tried to access this data with the structure below:
struct xy
{
unsigned short int ui;
int si;
};
what I got was:
in ui: aaaa, O.K.
in si: cccc bbbb !
it seems, that int starts always at a multiple of 4;
1.) is that correct?
2.) what can I do, to interpret the data correctly?
1. No.
2. Always assume that the compiler is throwing dice when it comes to the physical layout of classes, and never memcpy() classes. Instead, write a constructor that takes a pointer to the data and initializes the members appropriately.
many thanks for your reply!
The layout of the file, I mentioned in my initial posting, exists already and can't be changed. It is:
2 Bytes unsigned short int,
4 Bytes signed int,
4 Bytes signed int,
...
How can I access the 2nd item;
1.) is it possible by means of a structure?
2.) do I have to advance a pointer according to the length of the particular elements and assign the values by casting?
xy::xy(uchar *data,ulong *offset){
//(assuming data is in little endian)
this->ui=data[*offset]|(data[*offset+1]<<8);
(*offset)+=2;
this->si=0;
for (short a=0;a<4;a++)
this->si|=data[(*offset)++]<<(a*8);
}