Lazarus

Free Pascal => General => Topic started by: Kornelius on November 29, 2021, 11:46:42 am

Title: Problem with dynamic arrays [Solved]
Post by: Kornelius on November 29, 2021, 11:46:42 am
Hello verybody,

I work with lazarus v 2.0.12 on Windows10
both with 64bit

I have problems with dynamic arrays.
In my code it seems to me that fillchar does not work with dynamic arrays

Code: Pascal  [Select][+][-]
  1. {$DEFINE dyn}
  2.  
  3. procedure TSimpleFrm.FormShow(Sender: TObject);
  4.  
  5. var
  6.   ar1 : array of byte;
  7.   ar2 : array [0..99] of byte;
  8.  
  9. begin
  10. {$IFDEF dyn}
  11.   setLength(ar1, 100);
  12.   fillchar (ar1, SizeOf(ar1),1);
  13. {$ENDIF}
  14.   fillchar (ar2, SizeOf(ar2),1);
  15.  
  16. end;
  17.  
gives the error

Projekt simple has Exception-class ┬╗External: SIGSEGV┬ź on address 10000D2FF

but it works if dyn is undefined

You could circumvent this problem of course, but I have similar problems with

AES

DCP_rijndael.EncryptECB(InData,Block)
or
DCP_rijndael.EncryptCBC(InData,Block,Sizeof(Block));

Mainly I am amazed that you can call functions with dynamic arrays and normal arrays on the same way.

Do you have any idea?
Thank you very much for helping
Title: Re: Problem with dynamic arrays
Post by: Kornelius on November 29, 2021, 12:04:46 pm
to answer myself, I found a solution in

Code: Pascal  [Select][+][-]
  1. fillchar (ar1[0], Length(ar1),1);

and

Code: Pascal  [Select][+][-]
  1. DCP_rijndael.EncryptECB(InData[0],Block[0])

or
Code: Pascal  [Select][+][-]
  1. DCP_rijndael.EncryptCBC(InData[0],Block[0],Length(Block));
Title: Re: Problem with dynamic arrays [Solved]
Post by: PascalDragon on November 29, 2021, 01:10:28 pm
Your solution is correct, because dynamic arrays are essentially pointer variables. If you use only the variable itself then you modify or read the pointer to the array structure instead of the array itself.
TinyPortal © 2005-2018