Datasheet
ARM Compiler Reference
3-14 Copyright © 1999-2001 ARM Limited. All rights reserved. ARM DUI 0067D
Example 3-2 Pointer to packed
__packed int *p
There are no packed array types. A packed array is an array of objects of
packed type. There is no padding in the array.
Note
On ARM processors, access to unaligned data can take up to seven
instructions and three work registers. Data accesses through packed
structures must be minimized to avoid increase in code size, and
performance loss.
The
__packed
qualifier is useful to map a structure to an external data
structure, or for accessing unaligned data, but it is generally not useful to
save data size because of the relatively high cost of access. The number
of unaligned accesses can be reduced by only packing fields in a structure
that requires packing.
When a packed object is accessed using a pointer, the compiler generates
code that works and that is independent of the pointer alignment
(Example 3-3).
Example 3-3 Packed structure
typedef __packed struct
{
char x; // all fields inherit the __packed qualifier
int y;
}X; // 5 byte structure, natural alignment = 1
int f(X *p)
{
return p->y; // does an unaligned read
}
typedef struct
{
short x;
char y;
__packed int z; // only pack this field
char a;
}Y; // 8 byte structure, natural alignment = 2
int g(Y *p)
{
return p->z + p->x; // only unaligned read for z
}