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.37 Unaligned pointers in C and C++ code<br />

By default, <strong>the</strong> compiler expects conventional C and C++ pointers to point to naturally aligned<br />

words in memory because this enables <strong>the</strong> compiler to generate more efficient code.<br />

If you want to define a pointer that can point to a word at any address, you must specify <strong>the</strong><br />

__packed qualifier when defining <strong>the</strong> pointer. For example:<br />

__packed int *pi; // pointer to unaligned int<br />

When a pointer is declared as __packed, <strong>the</strong> compiler generates code that correctly accesses <strong>the</strong><br />

dereferenced value of <strong>the</strong> pointer, regardless of its alignment. The generated code consists of a<br />

sequence of byte accesses, or variable alignment-dependent shifting and masking instructions,<br />

ra<strong>the</strong>r than a simple LDR instruction. Consequently, declaring a pointer as __packed incurs a<br />

per<strong>for</strong>mance and code size penalty.<br />

5.37.1 See also<br />

Reference<br />

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

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

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

ID061811<br />

Non-Confidential

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

Saved successfully!

Ooh no, something went wrong!