[fpc-devel] Wrong docs: not initialized global variables
Maciej Izak
hnb.code at gmail.com
Sat Mar 24 23:17:11 CET 2018
2018-03-24 20:49 GMT+01:00 Ondrej Pokorny <lazarus at kluug.net>:
> Does it mean I can assume a local string variable is always (=whenever the
> routine is called) initialized to ''? I.e. that TestB always returns 'a'?
>
> function TestB: string;
> var B: string;
> begin
> B := B + 'a';
> Result := B;
> end;
>
> Until now I though that the above code is unsafe and I always initialized
> all local variables (also managed) before use. I assumed the initialization
> to empty string does not need to happen at every routine call.
>
Please note that string/dynamic array as Result is exception (it works like
*var* parameter). IMO this is the ugliest Pascal behavior ever:
=== example begin ===
function test1: string;
begin
Result := Result + 'a';
end;
function test2: TArray<byte>;
begin
SetLength(Result, Length(Result) + 1);
end;
begin
writeln(test1); // print a
writeln(test1); // print aa
writeln(test1); // print aaa
writeln(test1); // print aaaa
writeln(Length(test2)); // print 1
writeln(Length(test2)); // print 2
writeln(Length(test2)); // print 3
writeln(Length(test2)); // print 4
end.
=== example end ===
--
Best regards,
Maciej Izak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180324/eab096a4/attachment.html>
More information about the fpc-devel
mailing list