I have a board with a 32-bit ARM CPU as the main processor and a 8-bit AVR microcontroller as a slave device connected to it.
I am copying a struct (the slave converts it into a byte stream and the master receives and stores it in the memory location of a struct with identical definition).
Both the master and the slave print the values of the struct members on their own consoles. However the values do not match.
I have identified that the problem is occurring because the actual memory allocation of the struct in the master is changed a little to align with 16-bit boundaries. i.e. data types smaller than 16 bits are upgraded to 16 bits.
There are some 8 bit integers (u8) in the struct which are actually being allocated 2 bytes in memory in the Master whereas in the slave they are allocated just 1 byte as expected.
Unsurprisingly, sizeof(STRUCT_NAME)
yields different results in each system.
I have figured out a way around this by inserting filler bytes in the incoming data where necessary.
Is there any standard way to handle struct serialization in situations like this?
I'm using GCC for the ARM part.