Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C/C++ User's Guide - the "pack" pragma can be used to control the way in which structures are http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
The "pack" pragma can be used to control the way in which structures are
stored in memory.  By default, Watcom C/C++ aligns all structures and its
fields on a byte boundary.  There are 4 forms of the "pack" pragma.

The following form of the "pack" pragma can be used to change the alignment
of structures and their fields in memory.

+--------------------------------------------------------------------------+
|      #pragma pack ( n ) [;]                                              |
|                                                                          |
+--------------------------------------------------------------------------+

where
    description

n
    is 1, 2, 4, 8 or 16 and specifies the method of alignment.

The alignment of structure members is described in the following table.  If
the size of the member is 1, 2, 4, 8 or 16, the alignment is given for each
of the "zp" options.  If the member of the structure is an array or
structure, the alignment is described by the row "x".


                         zp1     zp2     zp4     zp8     zp16
     sizeof(member)  \---------------------------------------
             1       |   0       0       0       0       0
             2       |   0       2       2       2       2
             4       |   0       2       4       4       4
             8       |   0       2       4       8       8
             16      |   0       2       4       8       16
             x       |   aligned to largest member

An alignment of 0 means no alignment, 2 means word boundary, 4 means
doubleword boundary, etc.  If the largest member of structure "x" is 1 byte
then "x" is not aligned.  If the largest member of structure "x" is 2 bytes
then "x" is aligned according to row 2.  If the largest member of structure
"x" is 4 bytes then "x" is aligned according to row 4.  If the largest
member of structure "x" is 8 bytes then "x" is aligned according to row 8.
 If the largest member of structure "x" is 16 bytes then "x" is aligned
according to row 16.

If no value is specified in the "pack" pragma, a default value of 1 is used.
 Note that the default value can be changed with the "zp" Watcom C/C++
compiler command line option.

The following form of the "pack" pragma can be used to save the current
alignment amount on an internal stack.

+--------------------------------------------------------------------------+
|      #pragma pack ( push ) [;]                                           |
|                                                                          |
+--------------------------------------------------------------------------+
The following form of the "pack" pragma can be used to save the current
alignment amount on an internal stack and set the current alignment.

+--------------------------------------------------------------------------+
|      #pragma pack ( push, number ) [;]                                   |
|                                                                          |
+--------------------------------------------------------------------------+
The following form of the "pack" pragma can be used to restore the previous
alignment amount from an internal stack.

+--------------------------------------------------------------------------+
|      #pragma pack ( pop ) [;]                                            |
|                                                                          |
+--------------------------------------------------------------------------+

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson