__packed

Blog | rml

struct __packed s { … }This attribute tells GCC that a type or variable should be packed into memory, using the minimum amount of space possible, potentially disregarding alignment requirements. If specified on a struct or union, all variables therein are so packed. If specified on just a specific variable, only that type is packed. As an example, a structure with a char followed by an int would most likely find the integer aligned to a memory address not immediately following the char (say, three bytes later). The compiler does this by inserting three bytes of unused packing between the two variables. A packed structure lacks this packing, potentially consuming less memory but failing to meet architecture alignment requirements.

It should also be noted that non aligned data access on some architectures (e.g. ppc) can totally cripple performance. We’re talking orders of magnatude here. IBM has a good article on their developer site.

http://www-128.ibm.com/developerworks/power/library/pa-dalign/

Apple has some good tools for this too – and docs if i remember correctly.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.