Keil Logo

PACK Compiler Directive

Abbreviation

None.

Arguments

A decimal number (1, 2, or 4) enclosed in parentheses.

Default

PACK(4)

µVision

Options — CA — Misc Controls.

Description

The PACK directive causes the compiler to generate byte-aligned or half-word-aligned structures. This is useful when exchanging data with other systems where no alignment is required.

Note

  • The compiler generates considerably more code to access byte-aligned or half-word-aligned objects. For this reason, PACK(1) and PACK(2) should be used only when absolutely necessary.
Example
#pragma pack(1)     /* byte alignment */

struct s1  {
  int  i1;     // i1 has offset 0
  char c1;     // c1 has offset 4
  int  i2;     // i2 has offset 5
  char c2;     // c2 has offset 9
  int  i3;     // i3 has offset 10
  char z1;     // z1 has offset 14
};

#pragma pack()    /* reset to default alignment */

struct s2  {
  int  i1;     // i1 has offset 0
  char c1;     // c1 has offset 4
  int  i2;     // i2 has offset 8
  char c2;     // c2 has offset 12
  int  i3;     // i3 has offset 16
  char z1;     // z1 has offset 20
};

Related Knowledgebase Articles

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.