Advertisement
sglienke

Size affects const passing

Aug 18th, 2016
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.95 KB | None | 0 0
  1. program SizeAffectsConstPassing;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. type
  6.   PSmallRec = ^TSmallRec;
  7.   TSmallRec = record
  8.     x: NativeInt;
  9.   end;
  10.  
  11.   PLargeRec = ^TLargeRec;
  12.   TLargeRec = record
  13.     x, y: NativeInt;
  14.   end;
  15.  
  16.   PSmallArray = ^TSmallArray;
  17.   TSmallArray = packed array[0..3] of Byte;
  18.  
  19. procedure TestSmallRecord(const rec: TSmallRec);
  20. begin
  21.   PSmallRec(@rec)^.x := rec.x * 10;
  22. end;
  23.  
  24. procedure TestLargeRecord(const rec: TLargeRec);
  25. begin
  26.   PLargeRec(@rec)^.x := rec.x * 10;
  27.   PLargeRec(@rec)^.y := rec.y * 10;
  28. end;
  29.  
  30. procedure TestSmallArray(const arr: TSmallArray);
  31. begin
  32.   PSmallArray(@arr)^[0] := arr[0] * 100;
  33. end;
  34.  
  35. var
  36.   s: TSmallRec;
  37.   l: TLargeRec;
  38.   a: TSmallArray = (1, 2, 3, 4);
  39. begin
  40.   s.x := 1;
  41.   l.x := 2;
  42.   l.y := 3;
  43.   TestSmallRecord(s);
  44.   TestLargeRecord(l);
  45.   TestSmallArray(a);
  46.   Writeln(s.x);
  47.   Writeln(l.x);
  48.   Writeln(l.y);
  49.   Writeln(a[0]);
  50.   Writeln(a[1]);
  51.   Writeln(a[2]);
  52.   Writeln(a[3]);
  53.   Readln;
  54. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement