Attribute for Packing and Alignment

From Software Engineers Wiki
Jump to: navigation, search

How to force packing of structure and alignment of structure?

Answer

GCC provides __attribute__ to specify additional attributes.

  • aligned (alignment): This attribute specifies a minimum alignment for the variable or structure field, measured in bytes.
  • packed: This attribute specifies that a variable or structure field should have the smallest possible alignment.

For example,

int x __attribute__ ((aligned (16))) = 0;

causes the compiler to allocate the global variable x on a 16-byte boundary.

struct foo { int x[2] __attribute__ ((aligned (16))); };

Above create a structure on 16 byte boundary.

Reference

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox