cancel
Showing results for 
Search instead for 
Did you mean: 

Pack alignment exception issue

Lyu.1
Associate III

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III

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."

View solution in original post

4 REPLIES 4
STOne-32
ST Employee

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

Pavel A.
Evangelist III

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."

Thank you for your answer, I understand.

Thank you for your answer, I understand.