Revision [17215]

This is an old revision of KeyPgField made by DkLwikki on 2014-08-30 09:42:03.

 

FIELD


Specifies field alignment.

Syntax:
KeyPgType Type|KeyPgUnion Union typename Field = { 1 | 2 | 4 }
...
End KeyPgType Type|KeyPgUnion Union

Description:
Field can be used to pack KeyPgType Types or KeyPgUnion Unions more tightly than the default layout. The most commonly used value is Field = 1, which causes the Type or Union to be packed as tightly as possible, without any padding bytes being added between the fields or at the end of the Type. Field can only be used to decrease field alignment, but it cannot be used to increase it. In order to add padding bytes, a Union with appropriate members could be used instead.

The default layout of Type and Union structures in FreeBASIC follows the GCC ABI. This allows for compatibility with other compilers or languages such as C.
By default, fields are aligned to their natural boundaries, which are:
  • A multiple of 1 for 1-byte data types
  • A multiple of 2 for 2-byte data types
  • A multiple of 4 for 4-byte data types
  • A multiple of 4 for 8-byte data types (32bit x86 DOS(DJGPP)/Linux/BSD)
  • A multiple of 8 for 8-byte data types (Win32/Win64, 32bit ARM Linux, 64bit x86_64/AArch64 Linux/BSD)
  • The largest natural boundary of the fields of Type/Union data types
  • Dynamic string descriptors are handled as Type structures with the data pointer field being the one with the largest natural alignment.
  • Fixed-length strings are aligned according to the alignment required for the character size.
  • Static arrays are aligned according to the alignment required for the element data type.

The compiler aligns fields by inserting padding bytes in front of them in order to move them to an offset that corresponds to their natural boundary, or to a multiple of the value given with Field, if it is smaller than the field's natural alignment. On the x86 architecture, such proper alignment is not required but can result in better performance when accessing the fields. Other architectures might actually require proper alignment.

In addition to field alignment, the whole structure's size is rounded up to a multiple of the largest natural alignment of its fields, by adding padding bytes at the end of the structure. This ensures that in an array of such structures, each individual one is properly aligned as required by the fields.


Examples:
Type bitmap_header Field = 1
    bfType          As UShort
    bfsize          As UInteger
    bfReserved1     As UShort
    bfReserved2     As UShort
    bfOffBits       As UInteger
    biSize          As UInteger
    biWidth         As UInteger
    biHeight        As UInteger
    biPlanes        As UShort
    biBitCount      As UShort
    biCompression   As UInteger
    biSizeImage     As UInteger
    biXPelsPerMeter As UInteger
    biYPelsPerMeter As UInteger
    biClrUsed       As UInteger
    biClrImportant  As UInteger
End Type

Dim bmp_header As bitmap_header

'Open up bmp.bmp and get its header data:
'Note: Will not work without a bmp.bmp to load . . .
Open "bmp.bmp" For Binary As #1

    Get #1, , bmp_header
   
Close #1

Print bmp_header.biWidth, bmp_header.biHeight

Sleep


Dialect Differences:
Differences from QB:

See also:
Back to User Defined Types
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode