11.07.2015 Views

MSP430 IAR C/C++ Compiler reference guide - Rice University

MSP430 IAR C/C++ Compiler reference guide - Rice University

MSP430 IAR C/C++ Compiler reference guide - Rice University

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

Efficient coding for embedded applicationsThere are two reasons why this can be considered a problem:● Network communication protocols are usually specified in terms of data types withno padding in between● There is a need to save data memory.For information about alignment requirements, see Alignment, page 169.There are two ways to solve the problem:● Use the #pragma pack directive. This is an easy way to remove the problem withthe drawback that each access to an unaligned element in the structure will use morecode.● Write your own customized functions for packing and unpacking structures. This isa more portable way, which will not produce any more code apart from yourfunctions. The drawback is the need for two views on the structure data—packedand unpacked.For further details about the #pragma pack directive, see pack, page 209.ANONYMOUS STRUCTS AND UNIONSWhen a structure or union is declared without a name, it becomes anonymous. The effectis that its members will only be seen in the surrounding scope.Anonymous structures are part of the <strong>C++</strong> language; however, they are not part of the Cstandard. In the <strong>MSP430</strong> <strong>IAR</strong> C/<strong>C++</strong> <strong>Compiler</strong> they can be used in C if languageextensions are enabled.In the <strong>IAR</strong> Embedded Workbench IDE, language extensions are enabled by default.Use the -e compiler option to enable language extensions. See -e, page 150, foradditional information.ExampleIn the following example, the members in the anonymous union can be accessed, infunction f, without explicitly specifying the union name:struct s{char tag;union{long l;float f;};} st;Part 1. Using the compiler115

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

Saved successfully!

Ooh no, something went wrong!