Atmel CAVR-4 Manual De Usuario

Descargar
Página de 323
CAVR-4
Part 2. Compiler reference
IAR language extensions
273
Example
struct str 
{
  char a;
  unsigned long b[];
};
struct str * GetAStr(int size)
{
  return malloc(sizeof(struct str) +
                sizeof(unsigned long) * size);
}
void UseStr(struct str * s)
{
  s->b[10] = 0;
}
The 
struct
 will inherit the alignment requirements from all elements, including the 
alignment of the incomplete array. The array itself will not be included in the size of the 
struct. However, the alignment requirements will ensure that the struct will end exactly 
at the beginning of the array; this is known as padding.
In the example, the alignment of 
struct
 
str
 will be 4 and the size is also 4. (Assuming 
a processor where the alignment of 
unsigned
 
long
 is 4.)
The memory layout of 
struct
 
str
 is described in the following figure.
Arrays of incomplete types
An array may have an incomplete 
struct
union
, or 
enum
 type as its element type. The 
types must be completed before the array is used (if it is), or by the end of the 
compilation unit (if it is not).
a
First long element of b
Second long element of b
...
pad 
byte
pad 
byte
pad 
byte