使用带有默认参数和重载过程的字符串数组

zig*_*zig 5 delphi delphi-5

我有这个代码,有一个使用重载和默认参数的过程:

program Project2;
{$APPTYPE CONSOLE}
uses SysUtils;

procedure Foo; overload; // not actually needed to reproduce
begin
end;

procedure Foo(const a: array of string; b: Boolean=False); overload;
begin
  Writeln(Length(a));
end;

begin
  Foo(['1', '2', '3']); // => 1 ???
  Foo(['1', '2', '3'], False); // => 3 OK
  Readln;
end.
Run Code Online (Sandbox Code Playgroud)

输出是:

1
3
Run Code Online (Sandbox Code Playgroud)

请注意,第一次调用 Foo不提供默认值。为什么会这样?这个问题只与非常旧的编译器有关吗?

这仅在使用overload密钥时发生。

procedure Foo2(const a: array of string; b: Boolean=False);
begin
  Writeln(Length(a));
end;

Foo2(['1', '2', '3']);
Run Code Online (Sandbox Code Playgroud)

工作正常。

Dis*_*ned 4

概括

正如您所发现的并且 David 已帮助澄清的那样:这是 Delphi 5(可能还有那个时代的其他一些版本)中的一个错误。在特定条件下,编译器无法正确调用该过程。

它本质上是两个功能的冲突:

  • 开放数组允许调用者将未指定长度的固定数组传递到过程中。编译器在编译时确定长度并传递一个附加的隐藏参数(High索引),以便该方法可以正确确定数组中的元素数量。
  • 默认参数只是语法糖,允许调用者省略默认值。实现不受影响,但编译器会自动传递省略的参数,就像调用者传递了默认值一样。
  • 当过程被标记为过载时,就会出现该错误。编译器似乎“忘记”传递隐藏High索引,并在其位置传递默认值。

解决方法

我确信您已经在使用明显的解决方法,但为了完整起见,我将其包括在内。当我以前在 Delphi 5 中工作时,我们array of String用以下内容替换了 和 默认值的所有组合;(无论我们是否已经在使用overload)。

procedure Foo(const a: array of string; b: Boolean); overload; {Remove the default}
begin
  ...
end;
procedure Foo(const a: array of string); overload;
begin
  Foo(a, False); {And pass the default value via overload}
end;
Run Code Online (Sandbox Code Playgroud)

细节

您可以通过Foo在 CPU 窗口 ( Ctrl++ ) 中调试并检查汇编代码来准确观察编译器如何无法正确调用。AltC

您应该能够推断出该Foo过程的编译符合预期:

  • 开放数组的地址eax
  • 第二个参数(默认值)ecx
  • High数组的索引在edx

注意我使用Integerdefault 来获得更独特的默认值。

情况1

procedure Foo(const a: array of string; b: Integer = 7);
...
Foo(['a', 'b', 'c']);
{The last few lines of assembler for the above call}
lea eax,[ebp-$18] {Load effective address of array}
mov ecx,$00000007 {Implicitly set default value 7}
mov edx,$00000002 {The hidden High value of the open array}
call Foo
Run Code Online (Sandbox Code Playgroud)

案例2

procedure Foo(const a: array of string; b: Integer = 7); overload;
...
Foo(['a', 'b', 'c']);

lea eax,[ebp-$18]
{The second parameter is now uninitialised!}
mov edx,$00000007 {Instead the default is assigned to register for High(a)}
call Foo
Run Code Online (Sandbox Code Playgroud)

案例3

procedure Foo(const a: array of string; b: Integer = 7); overload;
...
Foo(['a', 'b', 'c'], 5);

lea eax,[ebp-$18]
mov ecx,$00000005 {The explicit argument for 2nd parameter}
mov edx,$00000002 {The hidden parameter is again correctly assigned}
call Foo
Run Code Online (Sandbox Code Playgroud)

额外的观察

1) 正如上面案例 2 所指出的,当 bug 出现时,ecx未初始化。下面应该演示效果:

procedure Foo(const a: array of string; b: Integer = 2); overload;
var
  I: Integer;
begin
  for I := Low(a) to High(a) do Write(a[I]);
  Writeln(b);
end;
...
Foo(['a', 'b', 'c'], 23); {Will write abc23}
Foo(['a', 'b', 'c']); {Will write abc, but the number probably won't be 2}
Run Code Online (Sandbox Code Playgroud)

2) 该错误不会在动态数组中出现。动态数组的长度是其内部结构的一部分,因此不能被忘记。