Alignment examples
Note: This topic does not apply to the Clang-based front
end of IBM® XL C/C++ for AIX®, which is
invoked by xlclang/xlclang++.
The following examples use these symbols to show padding and boundaries:
p
= padding
|
= halfword (2-byte) boundary
:
= byte boundary
Mac68K example
#pragma options align=mac68k
struct B {
char a;
double b;
};
#pragma options align=reset
The size of
B
is 10 bytes. The alignment of B
is 2 bytes. The
layout of B
is as follows: |a:p|b:b|b:b|b:b|b:b|
Packed example
#pragma options align=bit_packed
struct {
char a;
double b;
} B;
#pragma options align=reset
The size of B
is 9 bytes. The layout of B
is as follows:
|a:b|b:b|b:b|b:b|b:
Nested aggregate example
#pragma options align=mac68k
struct A {
char a;
#pragma options align=power
struct B {
int b;
char c;
} B1; // <-- B1 laid out using power alignment rules
#pragma options align=reset // <-- has no effect on A or B,
but on subsequent structs
char d;
};
#pragma options align=reset
The
size of A
is 12 bytes. The alignment of A
is 2 bytes. The layout
of A
is as follows: |a:p|b:b|b:b|c:p|p:p|d:p|
C++ derived class example
In 32-bit mode:
#pragma options align=natural
class A {
double _a;
} sa;
class C : public A {
public:
virtual void f() {}
private:
char* name;
} sc;
The size of
sc
is 24 bytes. The alignment of sc
is 8 bytes. The
layout of sc
is as follows:
|a:a|a:a|a:a|a:a|f:f|f:f|p:p|p:p|n:n|n:n|p:p|p:p|