2

I'm working with an ARM processor Cortex-m4, STM32L476RG Board.

I have read about the padding might be the reason to a bigger size of the struct, than what actually inside the struct.

But, I would really like to get the exact size of 68 bytes to the struct instead of 72.

Anyone have an idea on how to get the right sizee?

Consider the following struct:

struct data
{
  double Latitude;            // 00-07
  double Longtitude;          // 08-15
  float HorizontalAcc;        // 16-19
  float Altitude;             // 20-23
  float AltitudeAcc;          // 24-27
  float SpeedInMS;            // 28-31
  float SpeedAcc;             // 32-35
  float Heading;              // 36-39
  float HeadingAcc;           // 40-43
  int Data;                   // 44-47
  unsigned long PacketNumber; // 48-51
  int64_t y;                  // 52-59
  int64_t x;                  // 60-67
}; // size 8 + 8 + 7*4 + 4 + 4 + 8 + 8 = 68

The thing is that I need to write these data bytewise into a binary file, and the padding ( after PacketNumber ) ruining it.. Is there anyway, to not use the padding or maybe to skip the padded bytes when writing to a file?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Noob14
  • 165
  • 3
  • 12

2 Answers2

0
#pragma pack(x) 

is the solution and fixed my problem. In this case with either 4 or 8 bytes, I chose #pragma pack(4).

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Noob14
  • 165
  • 3
  • 12
0

gcc users can also use non-standard __attribute__ ((packed)) attribute for remove padding from the wanted struct:

struct data
{
  double Latitude;            // 00-07
  ...
  unsigned long PacketNumber; // 48-51
  int64_t y;                  // 52-59
  int64_t x;                  // 60-67
} __attribute__ ((packed));
SKi
  • 8,007
  • 2
  • 26
  • 57