Atmel CAVR-4 ユーザーズマニュアル

ページ / 323
CAVR-4
Part 2. Compiler reference
137
Data representation
This chapter describes the data types, pointers, and structure types supported 
by the AVR IAR C/C++ Compiler.
See the chapter Efficient coding for embedded applications for information about 
which data types and pointers provide the most efficient code for your 
application.
Alignment
Every C data object has an alignment that controls how the object can be stored in 
memory. Should an object have an alignment of, for example, four, it must be stored on 
an address that is divisible by four.
The reason for the concept of alignment is that some processors have hardware 
limitations for how the memory can be accessed.
Assume that a processor can read 4 bytes of memory using one instruction, but only 
when the memory read is placed on an address divisible by 4. Then, 4-byte objects, such 
as 
long
 integers, will have alignment 4.
Another processor might only be able to read 2 bytes at a time; in that environment, the 
alignment for a 4-byte 
long
 integer might be 2.
A structure type will inherit the alignment from its components.
All objects must have a size that is a multiple of the alignment. Otherwise, only the first 
element of an array would be placed in accordance with the alignment requirements. 
In the following example, the alignment of the structure is 4, under the assumption that 
long
 has alignment 4. Its size is 8, even though only 5 bytes are effectively used.
struct str {
  long a;
  char b;
};
In standard C, the size of an object can be determined by using the 
sizeof
 operator.
ALIGNMENT IN THE AVR IAR C/C++ COMPILER
The AVR microcontroller does not have any alignment restrictions.