Vojtěch Čihák
2018-09-04 13:53:03 UTC
Hello,
Â
even if there's closed issue https://bugs.freepascal.org/view.php?id=34169 I would like to ask if it can be reconsidered.
Â
The subject is that SetLength now gives warning: Variable "dynamic array" of a managed type does not seem to be initialized
in 3.3.1 and 3.1.1 while it doesn't give any warning in stable 3.0.4
Â
As a solution is recommended do declaration: var arr: array of widechar = ();
But it means that initialization is in fact done twice. See assembler of following code:
Â
procedure TForm1.Button1Click(Sender: TObject);
var DA: array of widechar = ();
begin
 SetLength(DA, 10);
 writeln(DA[0]);
end;Â
Â
unit1.pas:30Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â var DA: array of widechar = ();
00000000004696C9 488d1538536600      lea  0x665338(%rip),%rdx    # 0xacea08 <RTTI_$UNIT1_$$_def00000138>
00000000004696D0 488b3529536600      mov  0x665329(%rip),%rsi    # 0xacea00 <TC_$UNIT1$_$TFORM1_$_BUTTON1CLICK$TOBJECT_$$_defaultDA>
00000000004696D7 4889e7          mov  %rsp,%rdi
00000000004696DA e8d199fcff        callq 0x4330b0 <fpc_dynarray_assign>
unit1.pas:32Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SetLength(DA, 10);
00000000004696DF 48c74424680a000000    movq  $0xa,0x68(%rsp)
00000000004696E8 488d3519536600      lea  0x665319(%rip),%rsi    # 0xacea08 <RTTI_$UNIT1_$$_def00000138>
00000000004696EF 488d4c2468        lea  0x68(%rsp),%rcx
00000000004696F4 4889e7          mov  %rsp,%rdi
00000000004696F7 ba01000000        mov  $0x1,%edx
00000000004696FC e8df99fcff        callq 0x4330e0 <fpc_dynarray_setlength>
unit1.pas:33Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â writeln(DA[0]);
To avoid warning we now need four extra instructions incl. call to fpc_array_assign, which is obviously redundant.As an explanation is written:Â Setlength uses a var parameter. This is no different from other cases of var parameter usage.I disaggre here, because SetLength is not an usual procedure, it is fundamental part of compiler. Therefore it deserves some exception.That's why I ask for reconsidering and make it behave like in 3.0.4 or change declaration from "var" to "out" as it is done in getmem().Thanks, VojtÄch.Â
Â
even if there's closed issue https://bugs.freepascal.org/view.php?id=34169 I would like to ask if it can be reconsidered.
Â
The subject is that SetLength now gives warning: Variable "dynamic array" of a managed type does not seem to be initialized
in 3.3.1 and 3.1.1 while it doesn't give any warning in stable 3.0.4
Â
As a solution is recommended do declaration: var arr: array of widechar = ();
But it means that initialization is in fact done twice. See assembler of following code:
Â
procedure TForm1.Button1Click(Sender: TObject);
var DA: array of widechar = ();
begin
 SetLength(DA, 10);
 writeln(DA[0]);
end;Â
Â
unit1.pas:30Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â var DA: array of widechar = ();
00000000004696C9 488d1538536600      lea  0x665338(%rip),%rdx    # 0xacea08 <RTTI_$UNIT1_$$_def00000138>
00000000004696D0 488b3529536600      mov  0x665329(%rip),%rsi    # 0xacea00 <TC_$UNIT1$_$TFORM1_$_BUTTON1CLICK$TOBJECT_$$_defaultDA>
00000000004696D7 4889e7          mov  %rsp,%rdi
00000000004696DA e8d199fcff        callq 0x4330b0 <fpc_dynarray_assign>
unit1.pas:32Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SetLength(DA, 10);
00000000004696DF 48c74424680a000000    movq  $0xa,0x68(%rsp)
00000000004696E8 488d3519536600      lea  0x665319(%rip),%rsi    # 0xacea08 <RTTI_$UNIT1_$$_def00000138>
00000000004696EF 488d4c2468        lea  0x68(%rsp),%rcx
00000000004696F4 4889e7          mov  %rsp,%rdi
00000000004696F7 ba01000000        mov  $0x1,%edx
00000000004696FC e8df99fcff        callq 0x4330e0 <fpc_dynarray_setlength>
unit1.pas:33Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â writeln(DA[0]);
To avoid warning we now need four extra instructions incl. call to fpc_array_assign, which is obviously redundant.As an explanation is written:Â Setlength uses a var parameter. This is no different from other cases of var parameter usage.I disaggre here, because SetLength is not an usual procedure, it is fundamental part of compiler. Therefore it deserves some exception.That's why I ask for reconsidering and make it behave like in 3.0.4 or change declaration from "var" to "out" as it is done in getmem().Thanks, VojtÄch.Â