Data Structure Alignment

C++ compilers automatcally add padding in structures to optimize performance. For example, a 4 byte integer should normally start on a 4 byte memory boundary (e.g. the binary address ends in a 00) so that multiple lookups to memory don't occur when the value is fetched (onde for the first part, another for a second part. See Data structure alignment for more information.

For the certain data structures that will be written to an external device, this packing may be undesirable, as it wastes space with no gain.

The solution is a pragma compiler command, which allows you to force the packing to be tight:

#pragma pack(1)     /* set alignment to 1 byte boundary */
struct MyPackedData
    char Data1;
    long Data2;
    char Data3;
#pragma options align=reset    /* restore original alignment */

-- JimSkon - 2011-04-05

Topic revision: r1 - 2011-04-05 - JimSkon
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback