Recent

Author Topic: data alignement  (Read 5745 times)

airpas

  • Full Member
  • ***
  • Posts: 179
data alignement
« on: October 12, 2013, 11:09:47 pm »
hi every one
i am really confused about {$A} preprocessor .
the documentation said : The {$ALIGN directive can be used to select the data alignment strategy of the compiler for records.

ok take a look at this
Code: [Select]
{$A4}
type
     test = record
     x,y : char;
     end;

begin
 writeln(sizeof(test)); // expect 8 , but its 2
end.

so how dose the Alignment of data works ?


thanks

Dick, from the internet

  • Full Member
  • ***
  • Posts: 198
Re: data alignement
« Reply #1 on: October 13, 2013, 02:24:45 am »
leledumbo gives a good explanation here:
 http://forum.lazarus.freepascal.org/index.php/topic,21799.msg128017.html#msg128017

  regards,
      geno

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11471
  • Debugger - SynEdit - and more
    • wiki
Re: data alignement
« Reply #2 on: October 13, 2013, 02:50:19 am »
from the doc
Quote
, it has the same effect as the {$PACKRECORDS} directive

http://lazarus-ccr.sourceforge.net/fpcdoc/prog/progsu58.html#x65-630001.1.58

Quote
This means that the elements of a record which have size greater than n will be aligned on n byte boundaries.

char has a size of 1, so it is not aligned

airpas

  • Full Member
  • ***
  • Posts: 179
Re: data alignement
« Reply #3 on: October 13, 2013, 11:40:27 am »
thanks
so it is not possible to align a variable to be for example 16-byte boundary ?

i mean some thing similar to gcc like : int x __attribute__ ((aligned (16)))

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: data alignement
« Reply #4 on: October 13, 2013, 03:33:04 pm »
Quote
so it is not possible to align a variable to be for example 16-byte boundary ?
It is possible, but only for sizes bigger than the specified alignment property. Your C example also shows that (the size is still 4, not 16).

airpas

  • Full Member
  • ***
  • Posts: 179
Re: data alignement
« Reply #5 on: October 13, 2013, 03:55:10 pm »
thanks Leledumbo
the c example align the adress of x , so if you inc the adress you will find that it incremented by 16byte
to align the variable it self , it should be some thing  like : int x __attribute__ ((vector_size (16)))


Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: data alignement
« Reply #6 on: October 13, 2013, 04:36:04 pm »
Actually, I kinda seldom use alignment. Last time I use it is when I developed my FPOS kernel, since some addresses need to be aligned on 4K boundary.

eny

  • Hero Member
  • *****
  • Posts: 1647
Re: data alignement
« Reply #7 on: October 14, 2013, 06:36:21 pm »
If you absolutely need some kind of alignment you can always use something like this:
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain1.Button1Click(Sender: TObject);
  2. type
  3.   TAlignmentSize = array[1..21] of byte;
  4.   TMyData = char;
  5.  
  6.   TTestAlignment = packed record
  7.     case boolean of
  8.       false: (_dummy_: TAlignmentSize);
  9.       true : (data   : TMyData);
  10.   end;
  11.  
  12.   TAlignedArray = array[1..2] of TTestAlignment;
  13.  
  14. var
  15.   Aligned: TAlignedArray;
  16.  
  17. begin
  18.   ShowMessage(format('Size (expecting 42) : %d', [sizeof(Aligned)]));
  19. end;
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

airpas

  • Full Member
  • ***
  • Posts: 179
Re: data alignement
« Reply #8 on: October 14, 2013, 09:40:19 pm »
thanks eny , clever idea

 

TinyPortal © 2005-2018