29.10.2014 Views

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Compiler</strong> Coding Practices<br />

5.35 Unaligned fields in structures<br />

For efficiency, fields in a structure are positioned on <strong>the</strong>ir natural size boundary. This means that<br />

<strong>the</strong> compiler often inserts padding between fields to ensure that <strong>the</strong>y are naturally aligned.<br />

When space is at a premium, <strong>the</strong> __packed qualifier can be used to create structures without<br />

padding between fields. Structures can be packed in <strong>the</strong> following ways:<br />

• The entire struct can be declared as __packed. For example:<br />

__packed struct mystruct<br />

{<br />

char c;<br />

short s;<br />

} // not recommended<br />

Each field of <strong>the</strong> structure inherits <strong>the</strong> __packed qualifier.<br />

Declaring an entire struct as __packed typically incurs a penalty both in code size and<br />

per<strong>for</strong>mance.<br />

• Individual non-aligned fields within <strong>the</strong> struct can be declared as __packed. For example:<br />

struct mystruct<br />

{<br />

char c;<br />

__packed short s; // recommended<br />

}<br />

This is <strong>the</strong> recommended approach to packing structures.<br />

Note<br />

The same principles apply to unions. You can declare ei<strong>the</strong>r an entire union as __packed, or use<br />

<strong>the</strong> __packed attribute to identify components of <strong>the</strong> union that are unaligned in memory.<br />

5.35.1 See also<br />

Concepts<br />

• Per<strong>for</strong>mance penalty associated with marking whole structures as packed on page 5-48<br />

• Detailed comparison of an unpacked struct, a __packed struct, and a struct with<br />

individually __packed fields on page 5-51.<br />

Reference<br />

<strong>Compiler</strong> Reference:<br />

• #pragma pack(n) on page 5-56<br />

• __packed on page 5-9.<br />

<strong>ARM</strong> DUI 0375C Copyright © 2007-2008, 2011 <strong>ARM</strong>. All rights reserved. 5-47<br />

ID061811<br />

Non-Confidential

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!