Results 1 to 4 of 4

Thread: Question about byte alignment...

  1. #1

    Question about byte alignment...

    Hey all,

    Consider the following record:

    Code:
      TMyRecord = record
        ID:      Word;             //2 bytes
        recSize: Integer;          //4 bytes
        Str:     String;           //4 bytes
        b:       Boolean;          //1 bytes
        a:       char;             //2 bytes
      end;
    In delphi 2010, when I enable {$A8}, sizeOf(TMyRecord) will return 16. Which strikes me because I thought each field would take 8bytes (or will be padded, until it takes 8 bytes).

    It would correspond to the following memory layout:
    Code:
    WWxxxxxx
    IIIIxxxx
    SSSSxxxx
    Bxxxxxxx
    CCxxxxxx
    However, this is not the case. How exactly are the fields packed together when I use 8-byte alignment?

    Thanks
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  2. #2
    why not using packed record instead

  3. #3
    why not using packed record instead?
    I know about packed records, but it's not an answer to my question. I just want to know how exactly byte alignment works.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  4. #4
    I thought this was strange too, but I found this information here: http://www.delphibasics.co.uk/RTL.asp?Name=$Align&ExpandCode1=Yes

    With $Align On (default), complex data types, such as records have their elements aligned to 2, 4 or 8 byte boundaries, as appropriate to the data type. For example, a Word field would be aligned to a 4 byte boundary.
    Which means that A4 and A8 will only differ if the record contains 8 byte members such as Double or Int64.
    ZGameEditor - Develop 64kb games for Windows.
    Thrust for Vectrex - ROM-file and 6809 source code.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •