Author Topic: Memory location  (Read 336 times)

LemonParty

  • Hero Member
  • *****
  • Posts: 657
Memory location
« on: August 13, 2026, 12:42:59 pm »
Assume we have this code:
Code: Pascal  [Select][+][-]
  1. function Func(A: Integer): Integer;
  2. const
  3.   CArr1: array [0..15] of Byte = ({...});
  4.   CArr2: array [0..15] of Byte = ({...});
  5.   CArr3: array [0..15] of AnsiChar = ({...});
  6. begin
  7.   {...}
  8. end;
Is it true that declared arrays will be located at the same place in memory? I mean addresses for this arrays will be for example $1000 for CArr1, $1010 for CArr2, $1020 for CArr3.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Thausand

  • Hero Member
  • *****
  • Posts: 613
Re: Memory location
« Reply #1 on: August 13, 2026, 01:05:41 pm »
Is it true that declared arrays will be located at the same place in memory? I mean addresses for this arrays will be for example $1000 for CArr1, $1010 for CArr2, $1020 for CArr3.
No.
A docile goblin always follow HERMES.md

Thaddy

  • Hero Member
  • *****
  • Posts: 19805
  • Glad to be alive.
Re: Memory location
« Reply #2 on: August 13, 2026, 04:01:56 pm »
Yes and no: you do not need a function for that:
Code: Pascal  [Select][+][-]
  1. program recordpackingagain;
  2. {$mode objfpc}
  3. type
  4.   TRec = bitpacked record
  5.     CArr1: array [0..15] of Byte;
  6.     CArr2: array [0..15] of Byte;
  7.     CArr3: array [0..15] of AnsiChar;
  8.   end;
  9. const
  10.   func:TRec = (
  11.                Carr1:(0,1,2,3,4,5,6,7,8,9,$A,$B,$C,$D,$E,$F);
  12.                Carr2:(0,1,2,3,4,5,6,7,8,9,$A,$B,$C,$D,$E,$F);
  13.                CArr3:('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  14.               );
  15.              
  16. begin
  17. end.
  18.  
This is guaranteed to be aligned.
Even if {$writeable const}/{$J+} is on.
If you subsequently use a global const of that record, all fields will always have the same address too.

Using a function is wrong because that aligns on the stack and can have different pointers on every call. (Although in practice these consts are global)
« Last Edit: August 13, 2026, 04:49:41 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

 

TinyPortal © 2005-2018