ARM Technical Support Knowledge Articles

PACK DIRECTIVE

Applies to: C166 C Compiler

Answer


Information in this article applies to:


QUESTION

What does the PACK directive do? Does it let me pack structure elements on byte or word boundaries?

ANSWER

Yes. That's exactly what the PACK directive does.

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

NOTE: The C166 compiler generates considerably more code to access byte-aligned words. For this reason, PACK(1) should be used only when necessary.

PACK(1) assumes that structure pointers point to word-aligned structures. If your application uses structure pointers to byte-aligned structures, you must use the BYTEALIGN directive in addition to PACK(1).

For example:

#pragma pack(1)   /* byte alignment */

struct s1  {
  int  i1;     // i1 has offset 0
  char c1;     // c1 has offset 2
  int  i2;     // i2 has offset 3
  char c2;     // c2 has offset 5
  int  i3;     // i3 has offset 6
  char z1;     // z1 has offset 8
};

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

struct s2  {
  int  i1;     // i1 has offset 0
  char c1;     // c1 has offset 2
  int  i2;     // i2 has offset 4
  char c2;     // c2 has offset 6
  int  i3;     // i3 has offset 8
  char z1;     // z1 has offset 10
};

SEE ALSO

Article last edited on: 2000-09-07 00:00:00

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential