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 "zp" option allows you to specify the alignment of members in a http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
The "zp" option allows you to specify the alignment of members in a
structure.  The default is "zp2" for the 16-bit compiler and "zp8" for
32-bit compiler.  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.

Note that packed structures are padded to ensure that consecutive
occurrences of the same structure in memory are aligned appropriately.  This
is illustrated when the following example is compiled with "zp4".  The
amount of padding is determined as follows.  If the largest member of
structure "s" is 1 byte then "s" is not aligned.  If the largest member of
structure "s" is 2 bytes then "s" is aligned according to row 2.  If the
largest member of structure "s" is 4 bytes then "s" is aligned according to
row 4.  If the largest member of structure "s" is 8 bytes then "s" is
aligned according to row 8.  At present, there are no scalar objects that
can have a size of 16 bytes.  If the largest member of structure "s" is an
array or structure then "s" is aligned according to row "x".  Padding is the
inclusion of slack bytes at the end of a structure in order to guarantee the
alignment of consecutive occurrences of the same structure in memory.

To understand why structure member alignment may be important, consider the
following example.

Example:

     #include <stdio.h>
     #include <stddef.h>

     typedef struct memo_el {
         char           date[9];
         struct memo_el *prev,*next;
         int            ref_number;
         char           sex;
     } memo;



     void main( )
     {
         printf( "Offset of %s is %d\n",
                 "date", offsetof( memo, date ) );
         printf( "Offset of %s is %d\n",
                 "prev", offsetof( memo, prev ) );
         printf( "Offset of %s is %d\n",
                 "next", offsetof( memo, next ) );
         printf( "Offset of %s is %d\n",
                 "ref_number", offsetof( memo, ref_number ) );
         printf( "Offset of %s is %d\n",
                 "sex", offsetof( memo, sex ) );
         printf( "Size of %s is %d\n",
                 "memo", sizeof( memo ) );
         printf( "Number of padding bytes is %d\n",
                 sizeof( memo )
                 - (offsetof( memo, sex ) + sizeof( char )) );
     }

In the above example, the default alignment "zp8" will cause the pointer and
integer items to be aligned on even addresses although the array "date" is 9
bytes in length.  The items are 2-byte aligned when sizeof(item) is 2 and
4-byte aligned when sizeof(item) is 4.  On computer systems that have a
16-bit (or 32-bit) bus, improved performance can be obtained when pointer,
integer and floating-point items are aligned on an even boundary.  This
could be done by careful rearrangement of the fields of the structure or it
can be forced by use of the "zp" option.


     16-bit output when compiled zp1:
     Offset of date is 0
     Offset of prev is 9
     Offset of next is 11
     Offset of ref_number is 13
     Offset of sex is 15
     Size of memo is 16
     Number of padding bytes is 0


     16-bit output when compiled zp4:
     Offset of date is 0
     Offset of prev is 10
     Offset of next is 12
     Offset of ref_number is 14
     Offset of sex is 16
     Size of memo is 18
     Number of padding bytes is 1


     32-bit output when compiled zp1:
     Offset of date is 0
     Offset of prev is 9
     Offset of next is 13
     Offset of ref_number is 17
     Offset of sex is 21
     Size of memo is 22
     Number of padding bytes is 0


     32-bit output when compiled zp4:
     Offset of date is 0
     Offset of prev is 12
     Offset of next is 16
     Offset of ref_number is 20
     Offset of sex is 24
     Size of memo is 28
     Number of padding bytes is 3

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