Aligning Your Data for Speed and Correctness
Jonathan 'Wolf' Rentzsch
Red Shed Software
jon@redshed.net
sizeof
)
Ptr
is a char*
Handle
is a char**
typedef struct { char a; long b; char c; } Struct;
typedef struct { char a; // 1 byte long b; // 4 bytes char c; // 1 byte } Struct; // 6 bytes, right?
Field Type | Field Name | Field Offset | Field Size | Field End |
---|---|---|---|---|
char |
a |
0 | 1 | 1 |
padding | 1 | 1 | 2 | |
long |
b |
2 | 4 | 6 |
char |
c |
6 | 1 | 7 |
padding | 7 | 1 | 8 | |
Total Size in Bytes: | 8 |
#pragma options align=power
:
Field Type | Field Name | Field Offset | Field Size | Field End |
---|---|---|---|---|
char |
a |
0 | 1 | 1 |
padding | 1 | 3 | 4 | |
long |
b |
4 | 4 | 8 |
char |
c |
8 | 1 | 9 |
padding | 9 | 3 | 12 | |
Total Size in Bytes: | 12 |
#pragma options align=reset
at the end of your structure declaration
NewPtr()
and NewHandle()
return sixteen-byte aligned addresses
malloc()
may or may not return aligned addresses
NewPtr()
?
MPAllocateAligned()
in Multiprocessing.h
has a bunch of options