C/C++ sizeof() struct in KDS GNU compiler

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C/C++ sizeof() struct in KDS GNU compiler

Jump to solution
641 Views
jmartinez1
Contributor II

Hi everyone,

 

I want to use the sizeof() operator in order to know the number of bytes in a struct with different kind of objects (please refer to my struct detalied below). I know sizeof() for 32 bits processors by default they use 4 bytes padding aligment. That means for a "char/uint8_t" variable, the numer of bytes returned by sizeof() is 4. To avoid this situation, I would like to use the #pragma pack directive when I define my struct, because sizeof() will return the exact number of bytes, but my doubts are:

 

1) If it is a good practice to use #pragma pack(push, 1) directive so as to force padding to 1 byte.

2) In terms of handle and processing speed of the RAM, does #pragma pack(push,1) improve these aspects in comparison with default padding (4 bytes)?

3) There are any other issue modifying the default padding?

4) Finally, must I use the #pragma pack(pop) at the end of the definition?

 

Thank you very much.

 

My struct is:

 

#pragma pack(push,1)

typedef struct
{
LDD_CAN_TFrame BufferEstructuraTx[MaxFramesCAN];
LDD_CAN_TFrame BufferEstructuraRx[MaxFramesCAN];
uint8_t BufferTx[MaxFramesCAN];
uint8_t BufferRx[MaxFramesCAN];
bool SemaphoreTramaRxPendentProcessar[MaxFramesCAN];
bool SemaphoreTramaTxPendentProcessar[MaxFramesCAN];
uint8_t Punter_BufferRX_WriteMissatge;
uint8_t Punter_BufferRX_ReadMissatge;
uint8_t Punter_BufferTx_WriteMissatge;
uint8_t NumTramesRXPendentsProcessar;
CAN_DRIVER_TDeviceData *DeviceData;
} BUFFER_CAN;

#pragma pack(pop)

 

OTHERS STRUCTS THAT I USE IN MY TYPEDEF

 

typedef struct {
LDD_CAN_TMessageID MessageID; /*!< Message ID */
LDD_CAN_TFrameType FrameType; /*!< Type of the frame DATA/REMOTE */
uint8_t *Data; /*!< Message data buffer */
uint8_t Length; /*!< Message length */
uint16_t TimeStamp; /*!< Message time stamp */
uint8_t LocPriority; /*!< Local Priority Tx Buffers */
} LDD_CAN_TFrame;

 

typedef struct {
CAN_MemMapPtr BaseAddr; /* Device register memory map base address */
bool EnUser; /* Enable/Disable CAN */
LDD_TEventMask AvailableEventsMask; /* Available events bit mask */
LDD_TEventMask EventMask; /* Enable/Disable events bit mask */
uint8_t MaxDataLen; /* Max number of data to be sent in one frame */
LDD_CAN_TMBIndex MaxBufferIndex; /* Number of message buffers */
LDD_CAN_TMBIndex BuffersNumber; /* Number of message buffers */
LDD_CAN_TBufferMask RxBufferMask; /* Bit mask for message buffers configured as receive */
LDD_CAN_TBufferMask TxBufferMask; /* Bit mask for message buffers configured as transmit */
LDD_CAN_TErrorMask ErrorMask; /* Variable for errors mask value */
LDD_CAN_TBufferMask IntFlagReg; /* Content of the buffer status */
LDD_CAN_TStats Stats; /* Communication statistics */
LDD_CAN_TBufferMask TransferPendingMask; /* Transfer request pending message buffer mask */
LDD_TUserData *UserData; /* RTOS device data structure */
} CAN_DRIVER_TDeviceData;

Labels (1)
0 Kudos
1 Solution
462 Views
bobpaddock
Senior Contributor III

Are you really after the functionality of offsetof() from stddef.h?

Learn a new trick with the offsetof() macro | Embedded 

Space can also be saved by reordering the structure from largest to smallest.

Such as putting the pointers first in the struct.

Push and Pop should be used together.

Depending on the specific CPU there could be performance penalties or hard-faults from packing (not likely on modern parts).

If overlaying a struct on physical hardware sometimes push/pop is the only way that can be done as the hardware does not have padding.




View solution in original post

0 Kudos
1 Reply
463 Views
bobpaddock
Senior Contributor III

Are you really after the functionality of offsetof() from stddef.h?

Learn a new trick with the offsetof() macro | Embedded 

Space can also be saved by reordering the structure from largest to smallest.

Such as putting the pointers first in the struct.

Push and Pop should be used together.

Depending on the specific CPU there could be performance penalties or hard-faults from packing (not likely on modern parts).

If overlaying a struct on physical hardware sometimes push/pop is the only way that can be done as the hardware does not have padding.




0 Kudos