Search code examples
cpreprocessorpragma

Meaning of #pragma pack(2)


What does

#pragma pack(2)

do? What does it mean?


Solution

  • It means that structs, unions or classes are aligned by 2 bytes. That means, the following struct will use 3 bytes in memory instead of 2:

    struct MyStruct
    {
        char Field1;
        char Field2;
    };
    

    The following will use 4 bytes:

    struct MyStruct
    {
        WORD Field1;
        WORD Field2;
    };
    

    More here: http://msdn.microsoft.com/en-us/library/2e70t5y1%28v=vs.80%29.aspx. Important: Read about the problems and use it only if you know what you are doing and you need it ;-)