24.4.11 Packing Objects

The __pack specifier can be used to indicate that structures should not use memory gaps to align structure members, or that individual structure members should not be aligned.

Use the native keywords discussed in the Differences section to look up information on the semantics of this specifier.

Some compilers cannot pad structures with alignment gaps for some devices, and use of this specifier for such devices is ignored.

Example

The following shows a structure qualified using __pack, as well as a structure where one member has been explicitly packed.
struct DATAPOINT {
	unsigned char type;
	int value;
} __pack x_point;
struct LINETYPE {
	unsigned char type;
	__pack int start;
	long total;
} line;

Differences

The __pack specifier is a new CCI specifier that can be used with PIC devices and MPLAB XC8. This specifier has no observable effect since the device memory is byte addressable for all data objects. When targeting AVR devices, the MPLAB XC8 compiler has used the packed attribute.

The MPLAB XC16, XC-DSC, and XC32 compilers have used the packed attribute to indicate that a structure member was not aligned with a memory gap.

Migration to the CCI

No migration is required for the MPLAB XC8 compiler when building for PIC MCUs. When building for AVR MCUs or when building with any other compiler, change any occurrence of the packed attribute to the __packed specifier, for example, from:
struct DOT
{
	char a;
	int x[2] __attribute__ ((packed));
};
to
struct DOT
{
	char a;
	__pack int x[2];
};
Alternatively, you can pack the entire structure, if required.

Caveats

None.