Skip to main content
Explorer
March 29, 2024
Solved

Pack alignment exception issue

  • March 29, 2024
  • 2 replies
  • 1588 views

Hi,Master:

platform: STM32G070CB

keil ide: 

1.jpg

arm Compiler:version 5 (c99 mode)

Question:

 

#pragma pack(8)
typedef struct{
 uint32_t a;
 uint8_t b;
 uint32_t c;
 uint32_t d;
 uint32_t e;
} x_t;
#pragma pack()

x_t a[10] = {0};
int sz = sizeof(a[0]);

 

sz is 20, Why not 24?

Thank you very much!

    This topic has been closed for replies.
    Best answer by Pavel A.

    The fine manual has the answer.

    https://developer.arm.com/documentation/dui0375/g/Compiler-specific-Features/-pragma-pack-n-

    "This pragma aligns members of a structure to the minimum of n and their natural alignment."

    2 replies

    Technical Moderator
    April 6, 2024

    Dear @Lyu.1 ,

     

     Can you please show on the debugger how are packed on memory sz ?  

    Here is how it works for ARM/Keil for adding padding or not for each variable inside the structure? By default it should be 8 packed . 
    May be also to know the optimization level used as well , so we can discuss with Keil colleagues .

    https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/-pragma-pack-n-

    Ciao,

    STOne-32

    Lyu.1Author
    Explorer
    April 9, 2024

    Thank you for your answer, I understand.

    Pavel A.Answer
    Super User
    April 6, 2024

    The fine manual has the answer.

    https://developer.arm.com/documentation/dui0375/g/Compiler-specific-Features/-pragma-pack-n-

    "This pragma aligns members of a structure to the minimum of n and their natural alignment."

    Lyu.1Author
    Explorer
    April 9, 2024

    Thank you for your answer, I understand.