Integer sizes on nano vs nano 33 iot

Having trouble passing structured data between devices over RFM69 radio. I've narrowed down the problem to the same data structure having different sizes on the 2 different devices. With the structure declaration below, DriveData is 5 bytes on a Nano BUT 6 bytes on the Nano 33 iot ! The size of a temp variable declared to be unit16_t on the Nano 33 iot is 2 bytes as expected, so I'm not sure where the extra byte comes from. If I declare BattV to be unit8_t, then DriveData is 4 bytes on both devices, as expected - and all is well. If instead I declare BattV as an int, then DriveData is 12 bytes on the Nano 33 iot, and 5 bytes on the Nano. I have come to understand that the sizes of the various dat types differs on unique hardware, but if int is 8 bytes on the iot, then why is DriveData 12 instead of 11 bytes? I have scaled my data to get by with a single byte in this case but this wont always be satisfactory. What is it that I don't know?

typedef struct {
  byte        DW;
  uint16_t    BattV;  
  byte       Hr;   
  byte       Min;  
} DWDataStructure;

DWDataStructure DriveData;

Hello

Search about struct alignment, padding, packing...

if you can’t find it, look for GCC struct attributes like __attribute___ ((packed)) and align

Nice! Got the most out of this source:
https://developer.arm.com/documentation/100748/0616/Writing-Optimized-Code/Packing-data-structures
Arranging my members largest to smallest and using the packed attribute I get the same size structure using the unit16_t declaration for my integers without needing to scale down to a single byte. So everybody is talking and passing the correct data! I appreciate the help.

typedef struct __attribute__((packed)) {
  uint16_t    BattV;  
  uint16_t    PnlV;
  byte       Hr;   
  byte       Min;
  bool       DW;  
} DWDataStructure;

DWDataStructure DriveData;

Great

(Note that in C++ the typedef keyword is no longer needed)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.