Keil Logo

Technical Support

On-Line Manuals

Compiler User Guide

Preface Overview of the Compiler Getting Started with the Compiler Compiler Features Compiler Coding Practices Compiler Diagnostic Messages Using the Inline and Embedded Assemblers of the AR Compiler Command-line Options Language Extensions Compiler-specific Features Keywords and operators __align __ALIGNOF__ __alignof__ __asm __forceinline __global_reg __inline __int64 __INTADDR__ __irq __packed __pure __smc __softfp __svc __svc_indirect __svc_indirect_r7 __value_in_regs __weak __writeonly __declspec attributes __declspec(noinline) __declspec(noreturn) __declspec(nothrow) __declspec(notshared) __declspec(thread) Function attributes __attribute__((alias)) function attribute __attribute__((always_inline)) function attribute __attribute__((const)) function attribute __attribute__((deprecated)) function attribute __attribute__((destructor[(priority)])) function a __attribute__((format)) function attribute __attribute__((format_arg(string-index))) function __attribute__((malloc)) function attribute __attribute__((noinline)) function attribute __attribute__((no_instrument_function)) function a __attribute__((nomerge)) function attribute __attribute__((nonnull)) function attribute __attribute__((noreturn)) function attribute __attribute__((notailcall)) function attribute __attribute__((nothrow)) function attribute __attribute__((pcs("calling_convention"))) functio __attribute__((pure)) function attribute __attribute__((section("name"))) function attribut __attribute__((sentinel)) function attribute __attribute__((unused)) function attribute __attribute__((used)) function attribute __attribute__((visibility("visibility_type"))) fun __attribute__((warn_unused_result)) __attribute__((weak)) function attribute __attribute__((weakref("target"))) function attrib Type attributes __attribute__((bitband)) type attribute __attribute__((aligned)) type attribute __attribute__((packed)) type attribute Variable attributes __attribute__((alias)) variable attribute __attribute__((at(address))) variable attribute __attribute__((aligned)) variable attribute __attribute__((deprecated)) variable attribute __attribute__((noinline)) constant variable attrib __attribute__((packed)) variable attribute __attribute__((section("name"))) variable attribut __attribute__((unused)) variable attribute __attribute__((used)) variable attribute __attribute__((visibility("visibility_type"))) var __attribute__((weak)) variable attribute __attribute__((weakref("target"))) variable attrib __attribute__((zero_init)) variable attribute Pragmas #pragma anon_unions, #pragma no_anon_unions #pragma arm #pragma arm section [section_type_list] #pragma diag_default tag[,tag,...] #pragma diag_error tag[,tag,...] #pragma diag_remark tag[,tag,...] #pragma diag_suppress tag[,tag,...] #pragma diag_warning tag[, tag, ...] #pragma exceptions_unwind, #pragma no_exceptions_u #pragma GCC system_header #pragma hdrstop #pragma import symbol_name #pragma import(__use_full_stdio) #pragma import(__use_smaller_memcpy) #pragma inline, #pragma no_inline #pragma no_pch #pragma Onum #pragma once #pragma Ospace #pragma Otime #pragma pack(n) #pragma pop #pragma push #pragma softfp_linkage, #pragma no_softfp_linkage #pragma thumb #pragma unroll [(n)] #pragma unroll_completely #pragma weak symbol, #pragma weak symbol1 = symbol Instruction intrinsics __breakpoint intrinsic __cdp intrinsic __clrex intrinsic __clz intrinsic __current_pc intrinsic __current_sp intrinsic __disable_fiq intrinsic __disable_irq intrinsic __dmb intrinsic __dsb intrinsic __enable_fiq intrinsic __enable_irq intrinsic __fabs intrinsic __fabsf intrinsic __force_loads intrinsic __force_stores intrinsic __isb intrinsic __ldrex intrinsic __ldrexd intrinsic __ldrt intrinsic __memory_changed intrinsic __nop intrinsic __pld intrinsic __pli intrinsic __qadd intrinsic __qdbl intrinsic __qsub intrinsic __rbit intrinsic __rev intrinsic __return_address intrinsic __ror intrinsic __schedule_barrier intrinsic __semihost intrinsic __sev intrinsic __sqrt intrinsic __sqrtf intrinsic __ssat intrinsic __strex intrinsic __strexd intrinsic __strt intrinsic __swp intrinsic __usat intrinsic __wfe intrinsic __wfi intrinsic __yield intrinsic ARMv6 SIMD intrinsics ETSI basic operations C55x intrinsics VFP status intrinsic __vfp_status intrinsic Fused Multiply Add (FMA) intrinsics Named register variables GNU built-in functions Predefined macros Built-in function name variables C and C++ Implementation Details What is Semihosting? Via File Syntax Summary Table of GNU Language Extensions Standard C Implementation Definition Standard C++ Implementation Definition C and C++ Compiler Implementation Limits

__packed

9.12 __packed

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 unaligned access.

Usage

The __packed qualifier sets the alignment of any valid type to 1.
This means that:
  • There is no padding inserted to align the packed object.
  • Objects of packed type are read or written using unaligned accesses.
The __packed qualifier applies to all members of a structure or union when it is declared using __packed. There is no padding between members, or at the end of the structure. All substructures of a packed structure must be declared using __packed. Integral subfields of an unpacked structure can be packed individually.
Only packing fields in a structure that requires packing can reduce the number of unaligned accesses.

Note

On ARM processors that do not support unaligned access in hardware, for example, pre-ARMv6, access to unaligned data can be costly in terms of code size and execution speed. Data accesses through packed structures must be minimized to avoid increase in code size and performance loss.

Restrictions

The following restrictions apply to the use of __packed:
  • The __packed qualifier cannot be used on structures that were previously declared without __packed.
  • Unlike other type qualifiers you cannot have both a __packed and non-__packed version of the same structure type.
  • The __packed qualifier does not affect local variables of integral type.
  • A packed structure or union is not assignment-compatible with the corresponding unpacked structure. Because the structures have a different memory layout, the only way to assign a packed structure to an unpacked structure is by a field-by-field copy.
  • The effect of casting away __packed is undefined, except on char types. The effect of casting a nonpacked structure to a packed structure, or a packed structure to a nonpacked structure, is undefined. A pointer to an integral type that is not packed can be legally cast, explicitly or implicitly, to a pointer to a packed integral type.
  • There are no packed array types. A packed array is an array of objects of packed type. There is no padding in the array.

Errors

Taking the address of a field in a __packed structure or a __packed-qualified field yields a __packed-qualified pointer. The compiler produces a type error if you attempt to implicitly cast this pointer to a non-__packed pointer. This contrasts with its behavior for address-taken fields of a #pragma packed structure.

Examples

This example shows that a pointer can point to a packed type.
typedef __packed int* PpI;          /* pointer to a __packed int */
__packed int *p;                    /* pointer to a __packed int */
PpI p2;                             /* 'p2' has the same type as 'p' */
                                    /* __packed is a qualifier  */
                                    /* like 'const' or 'volatile' */
typedef int *PI;                    /* pointer to int */
__packed PI p3;                     /* a __packed pointer to a normal int */
                                    /*  -- not the same type as 'p' and 'p2' */
int *__packed p4;                   /* 'p4' has the same type as 'p3' */
This example shows that when a packed object is accessed using a pointer, the compiler generates code that works and that is independent of the pointer alignment.
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
}
Non-ConfidentialPDF file icon PDF versionARM DUI0375H
Copyright © 2007, 2008, 2011, 2012, 2014-2016 ARM. All rights reserved. 
  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.