That's exactly why I prefer C over scripting langs. I want to place each bit in memory by myself
Allright. I'll spill it out then.
Reason is that with most of the compilers and architectures, we need to have a whole 32 bit memory block for storing a 32 bit value. If we now look at our structure, (let's see if I can use some ascii graphics to explain)
memory:
Now, we store 3 first 8 bit wide parameters:
As we see, in the first 32 bit wide block is still 8 unused bits. But because next parameter is int (32 bit wide), compiler want's to place it to begin from empty 32 bit block. Most of the compilers do so called 'padding' and leave the last 8 bits in first block unused.
and then the last char is stored in the beginning of the third block:
This is how struct is excpected to be filled, if we use default settings (no pragmas) (and as a side note, last 3 Bytes Eg. 24 bits are also allocated for the struct even though thy're not used).
Now, when we do our memcpy in the example, our memory is
First 3 parameters appear to be correct, but the integer is now
DDDDDDEE instead of DDDDDDDD. And last character is ... well, undefined.
So we can basically either send the parameters as
char p1
char p2
char p3
char unusedjunk
int p4
char p5
or change the order of parameters to be
char p1
char p2
char p3
char p5
int p4
(or use the pragma to avoid padding).
But anyways, Zyx was on the right tracks, and with a lil bit of googling the pb would have been solved
