Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-10

fpc has trouble with array types

9 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andrew Pennebaker

But, but, the docs imply that this is the syntax for a function that returns an array of bytes.

fpc ios7crypt.pas
ios7crypt.pas(3,25) Error: Type identifier expected
ios7crypt.pas(3,25) Fatal: Syntax error, ";" expected but "ARRAY" found
Fatal: Compilation aborted
$ cat ios7crypt.pas
program IOS7Crypt;

function XlatPrime () : array of byte;
begin
XlatPrime := (
$64, $73, $66, $64, $3b, $6b, $66, $6f,
$41, $2c, $2e, $69, $79, $65, $77, $72,
$6b, $6c, $64, $4a, $4b, $44, $48, $53,
$55, $42, $73, $67, $76, $63, $61, $36,
$39, $38, $33, $34, $6e, $63, $78, $76,
$39, $38, $37, $33, $32, $35, $34, $6b,
$3b, $66, $67, $38, $37
);
end;

function Encrypt (hash : string) : string;
begin
Encrypt := 'abc';
end;
function Decrypt (hash : string) : string;
begin
Decrypt := 'abc';
end;
var
password : string;
hash : string;
begin
password := 'abc';
hash := Encrypt(password);
password := Decrypt(hash);
write('Password: ');
 writeln(password);
end.

Cheers,

Andrew Pennebaker
www.yellosoft.us

Reply by email

Felipe Monteiro de Carvalho
to Andrew Pennebaker: But, but, the docs imply that this is the syntax for a function that returns an array of…
Andrew Pennebaker wrote
function XlatPrime () : array of byte; begin XlatPrime := ( $64, $73, $66, $64, $3b, $6b, $66, $6f,

I think that a syntax similar to this is available only for constant initialization. Actually I remember it from constant initialization for records. Maybe it works for arrays too.

The usual way to set a dynamic array is:

function XlatPrime () : array of byte;
begin
  SetLength(XlatPrime, 20);
  XlatPrime[0] := $64;
  XlatPrime[1] := $73;
  etc

But you can also try this (maybe it works):

function XlatPrime () : array of byte;
const ConstXlatPrime: array[0..19] of Byte = (
 $64, $73, $66, $64, $3b, $6b, $66, $6f,....);
begin
  Result := ConstXlatPrime;
end;
Felipe Monteiro de Carvalho

Reply by email

Sven Barth
to Andrew Pennebaker: But, but, the docs imply that this is the syntax for a function that returns an array of…
Andrew Pennebaker wrote
But, but, the docs imply that this is the syntax for a function that returns an array of bytes.

[snip]

Show quoted text 12 lines
function XlatPrime () : array of byte; begin XlatPrime := ( $64, $73, $66, $64, $3b, $6b, $66, $6f, $41, $2c, $2e, $69, $79, $65, $77, $72, $6b, $6c, $64, $4a, $4b, $44, $48, $53, $55, $42, $73, $67, $76, $63, $61, $36, $39, $38, $33, $34, $6e, $63, $78, $76, $39, $38, $37, $33, $32, $35, $34, $6b, $3b, $66, $67, $38, $37 ); end;

Free Pascal (and Delphi and TP) does not support such syntax. I suggest you that you define your array as a const instead of a function and just use that.

E.g.

const
XlatPrime: array[0..52] of Byte = (
$64, $73, $66, $64, $3b, $6b, $66, $6f,
$41, $2c, $2e, $69, $79, $65, $77, $72,
$6b, $6c, $64, $4a, $4b, $44, $48, $53,
$55, $42, $73, $67, $76, $63, $61, $36,
$39, $38, $33, $34, $6e, $63, $78, $76,
$39, $38, $37, $33, $32, $35, $34, $6b,
$3b, $66, $67, $38, $37

   );

It is necessary to specify the element count correctly.

Note: In trunk you should be able to do the following:

type
   TByteArray = array of Byte;

function XlatPrime(): TByteArray;
begin

   XlatPrime := TByteArray.Create(
     $64, $73, $66, $64, $3b, $6b, $66, $6f,
     $41, $2c, $2e, $69, $79, $65, $77, $72,
     $6b, $6c, $64, $4a, $4b, $44, $48, $53,
     $55, $42, $73, $67, $76, $63, $61, $36,
     $39, $38, $33, $34, $6e, $63, $78, $76,
     $39, $38, $37, $33, $32, $35, $34, $6b,
     $3b, $66, $67, $38, $37
   );

end;

This feature was introduced because of Delphi compatibilty (and I still have the opinion that it would be nice if that would be extended for "unnamed" array types as well...)

Regards,
Sven

Reply by email

Sven Barth
to Sven Barth
Sven Barth wrote 15 lines
type TByteArray = array of Byte; function XlatPrime(): TByteArray; begin XlatPrime := TByteArray.Create( $64, $73, $66, $64, $3b, $6b, $66, $6f, $41, $2c, $2e, $69, $79, $65, $77, $72, $6b, $6c, $64, $4a, $4b, $44, $48, $53, $55, $42, $73, $67, $76, $63, $61, $36, $39, $38, $33, $34, $6e, $63, $78, $76, $39, $38, $37, $33, $32, $35, $34, $6b, $3b, $66, $67, $38, $37 ); end;

Before I forget it: If you use this solution (not the const one) or you use "SetLength", you need to free the array using "SetLength(0)" or "YourArrayVariable := Nil", otherwise you'll have a memory leak. You must not do this if you use the const solution.

Regards,
Sven

Reply by email

Vincent Snijders
to Sven Barth

2011/10/18 Sven Barth :

Show quoted text 22 lines
Am 18.10.2011 11:12, schrieb Sven Barth: > > type > TByteArray = array of Byte; > > function XlatPrime(): TByteArray; > begin > XlatPrime := TByteArray.Create( > $64, $73, $66, $64, $3b, $6b, $66, $6f, > $41, $2c, $2e, $69, $79, $65, $77, $72, > $6b, $6c, $64, $4a, $4b, $44, $48, $53, > $55, $42, $73, $67, $76, $63, $61, $36, > $39, $38, $33, $34, $6e, $63, $78, $76, > $39, $38, $37, $33, $32, $35, $34, $6b, > $3b, $66, $67, $38, $37 > ); > end; Before I forget it: If you use this solution (not the const one) or you use "SetLength", you need to free the array using "SetLength(0)" or "YourArrayVariable := Nil", otherwise you'll have a memory leak. You must not do this if you use the const solution.

That is unexpected. Until now, SetLength was used for ansistrings and dynamic arrays, which didn't have this requirement (unless you messed with the internals using Move).

Vincent

Reply by email

Sven Barth
to Vincent Snijders
Vincent Snijders wrote 27 lines
2011/10/18 Sven Barth: > Am 18.10.2011 11:12, schrieb Sven Barth: >> >> type >> TByteArray = array of Byte; >> >> function XlatPrime(): TByteArray; >> begin >> XlatPrime := TByteArray.Create( >> $64, $73, $66, $64, $3b, $6b, $66, $6f, >> $41, $2c, $2e, $69, $79, $65, $77, $72, >> $6b, $6c, $64, $4a, $4b, $44, $48, $53, >> $55, $42, $73, $67, $76, $63, $61, $36, >> $39, $38, $33, $34, $6e, $63, $78, $76, >> $39, $38, $37, $33, $32, $35, $34, $6b, >> $3b, $66, $67, $38, $37 >> ); >> end; > > Before I forget it: If you use this solution (not the const one) or you use > "SetLength", you need to free the array using "SetLength(0)" or > "YourArrayVariable := Nil", otherwise you'll have a memory leak. You must > not do this if you use the const solution. That is unexpected. Until now, SetLength was used for ansistrings and dynamic arrays, which didn't have this requirement (unless you messed with the internals using Move).

Could be that I missed the reference counting of arrays. At least I always try to "free" my arrays by hand ^^

Regards,
Sven

Reply by email

Sven Barth
to Sven Barth
Sven Barth wrote 33 lines
Am 18.10.2011 11:19, schrieb Vincent Snijders: > 2011/10/18 Sven Barth: >> Am 18.10.2011 11:12, schrieb Sven Barth: >>> >>> type >>> TByteArray = array of Byte; >>> >>> function XlatPrime(): TByteArray; >>> begin >>> XlatPrime := TByteArray.Create( >>> $64, $73, $66, $64, $3b, $6b, $66, $6f, >>> $41, $2c, $2e, $69, $79, $65, $77, $72, >>> $6b, $6c, $64, $4a, $4b, $44, $48, $53, >>> $55, $42, $73, $67, $76, $63, $61, $36, >>> $39, $38, $33, $34, $6e, $63, $78, $76, >>> $39, $38, $37, $33, $32, $35, $34, $6b, >>> $3b, $66, $67, $38, $37 >>> ); >>> end; >> >> Before I forget it: If you use this solution (not the const one) or >> you use >> "SetLength", you need to free the array using "SetLength(0)" or >> "YourArrayVariable := Nil", otherwise you'll have a memory leak. You >> must >> not do this if you use the const solution. > > That is unexpected. Until now, SetLength was used for ansistrings and > dynamic arrays, which didn't have this requirement (unless you messed > with the internals using Move). Could be that I missed the reference counting of arrays. At least I always try to "free" my arrays by hand ^^

Yes, seems like I missed the ref counting. See
http://www.freepascal.org/docs-html/ref/refsu15.html#x39-430003.3.1

====
As remarked earlier, dynamic arrays are reference counted: if in one of the previous examples A goes out of scope and B does not, then the array is not yet disposed of: the reference count of A (and B) is decreased with 1. As soon as the reference count reaches zero the memory, allocated for the contents of the array, is disposed of. ====

Regards,
Sven

Reply by email

Andrew Pennebaker
to Sven Barth

RRUZ<http://stackoverflow.com/questions/7802319/whats-the-syntax-for-literal-arrays-in-free-pascal>has the answer:

const
XLAT_SIZE = 53;
xlat : Array[0..XLAT_SIZE-1] of Integer = (
$64, $73, $66, $64, $3b, $6b, $66, $6f,
$41, $2c, $2e, $69, $79, $65, $77, $72,
$6b, $6c, $64, $4a, $4b, $44, $48, $53,
$55, $42, $73, $67, $76, $63, $61, $36,
$39, $38, $33, $34, $6e, $63, $78, $76,
$39, $38, $37, $33, $32, $35, $34, $6b,
$3b, $66, $67, $38, $37
);

Works for me!

Cheers,

Andrew Pennebaker
www.yellosoft.us

Sven Barth wrote 62 lines
Am 18.10.2011 11:33, schrieb Sven Barth: Am 18.10.2011 11:19, schrieb Vincent Snijders: > >> 2011/10/18 Sven Barth< >> >: >> >>> Am 18.10.2011 11:12, schrieb Sven Barth: >>> >>>> >>>> type >>>> TByteArray = array of Byte; >>>> >>>> function XlatPrime(): TByteArray; >>>> begin >>>> XlatPrime := TByteArray.Create( >>>> $64, $73, $66, $64, $3b, $6b, $66, $6f, >>>> $41, $2c, $2e, $69, $79, $65, $77, $72, >>>> $6b, $6c, $64, $4a, $4b, $44, $48, $53, >>>> $55, $42, $73, $67, $76, $63, $61, $36, >>>> $39, $38, $33, $34, $6e, $63, $78, $76, >>>> $39, $38, $37, $33, $32, $35, $34, $6b, >>>> $3b, $66, $67, $38, $37 >>>> ); >>>> end; >>>> >>> >>> Before I forget it: If you use this solution (not the const one) or >>> you use >>> "SetLength", you need to free the array using "SetLength(0)" or >>> "YourArrayVariable := Nil", otherwise you'll have a memory leak. You >>> must >>> not do this if you use the const solution. >>> >> >> That is unexpected. Until now, SetLength was used for ansistrings and >> dynamic arrays, which didn't have this requirement (unless you messed >> with the internals using Move). >> > > Could be that I missed the reference counting of arrays. At least I > always try to "free" my arrays by hand ^^ > Yes, seems like I missed the ref counting. See http://www.freepascal.org/* *docs-html/ref/refsu15.html#**x39-430003.3.1<http://www.freepascal.org/docs-html/ref/refsu15.html#x39-430003.3.1> ==== As remarked earlier, dynamic arrays are reference counted: if in one of the previous examples A goes out of scope and B does not, then the array is not yet disposed of: the reference count of A (and B) is decreased with 1. As soon as the reference count reaches zero the memory, allocated for the contents of the array, is disposed of. ==== Regards, Sven ______________________________**_________________ fpc-pascal maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>

Reply by email