2.7.4. Type Attributes

The tiarmclang compiler supports the application of type attributes to enum, struct, or union declarations or definitions. Type attributes can also be applied to a type that is defined via typedef declarations.

The following type attributes are supported by the tiarmclang compiler:

2.7.4.1. aligned

The aligned type attribute indicates a minimum byte boundary alignment for variables of the specified type. This attribute is especially useful for overriding the default compiler-imposed constraint on a particular data object, especially when a more restrictive alignment requirement is warranted.

Syntax

<type specification> __attribute__((aligned(alignment)));

  • alignment - the minimum alignment for the indicated type, specified in bytes. The alignment value must be an integer power of two. The compiler will impose the maximum of the default alignment for the type and the specified alignment on data objects of the type.

Examples

  • An aligned attribute applied to a typedef:

    typedef short a_short_type __attribute__((aligned(4)));
    

    Use of the a_short_type in C source code will force the definition of any data objects of that type to be placed on a 4-byte boundary.

  • An aligned attribute applied to a struct type:

    struct myS {
      char  m1;
      int   m2
      int   m3
      char  m4;
      short m5;
    } __attribute__((aligned(8)));
    

    In this case, the myS struct is aligned to an 8-byte boundary instead of what the compiler would impose by default (4-byte boundary).

  • An aligned attribute applied to a union within a struct:

    #include <stdio.h>
    
    typedef struct {
      char m1;
      union {
        short m2_u_m1;
        int   m2_u_m2;
        char  m2_u_m3;
      } m2_u __attribute__((aligned(16)));
    } myS;
    
    myS myS_obj;
    
    int main() {
      printf("address of myS_obj: 0x%08lx\n", (unsigned long)&myS_obj);
      printf("address of m2_u_m1: 0x%08lx\n",
             (unsigned long)&myS_obj.m2_u.m2_u_m1);
      return 0;
    }
    

    When compiled and linked and run, the output reveals that the target memory location where the first member of the m2_u union resides in memory is on a 16-byte boundary relative to the start of the struct myS_obj:

    %> tiarmclang -mcpu=cortex-m0 aligned_type_attr.c -o a.out -Wl,-llnk.cmd,-ma.map
    %> load470 -q a.out
    address of myS_obj: 0x2000a1e0
    address of m2_u_m1: 0x2000a1f0
    

    Note that the myS type alignment is also 16-bytes since the alignment of the struct is determined by the struct member with the most restrictive alignment constraint (m2_u in this case).

2.7.4.2. packed

The packed type attribute can be applied to struct or union types to indicate that each member of a given struct or union is placed on a 1-byte boundary.

Syntax

<type specification> __attribute__((packed));

Examples

Consider the following C code in which a packed attribute is applied to a struct type:

struct __attribute__((packed)) {
  char m1;
  int  m2;
} packed_struct = { 10, 20 };

In this case, the size of packed_struct is 5 bytes, whereas without the packed attribute the int type member m2 would have been aligned to a 4-byte boundary causing the size of the struct to be 8 bytes.