是的,您总是需要初始化函数的Result,即使它是托管类型(如string 和Variant)。编译器确实会生成一些代码来为您初始化 Variant 函数的未来返回值(至少我用于测试目的的 Delphi 2010 编译器会这样做),但编译器不保证您的 Result 已初始化;这只会使测试变得更加困难,因为您可能会遇到初始化结果的情况,基于此做出决定,但后来发现您的代码有错误,因为在某些情况下结果不是初始化。
根据我的调查,我注意到:
- 如果将结果分配给全局变量,则会使用已初始化的隐藏临时变量调用函数,从而产生 Result 被神奇地初始化的错觉。
- 如果您对同一个全局变量进行两次赋值,您将获得两个不同的隐藏临时变量,从而强化 Result 已初始化的错觉。
- 如果您对同一个全局变量进行两次赋值,但在调用之间不使用该全局变量,则编译器仅使用 1 个隐藏的临时变量,打破了之前的规则!
- 如果您的变量是调用过程的本地变量,则根本不使用中间隐藏的局部变量,因此结果没有初始化。
演示:
首先,这是一个返回 Variant 的函数收到 var Result:
Variant 隐藏参数的证明。以下两个编译成完全相同的汇编器,如下所示:
procedure RetVarProc(var V:Variant);
begin
V := 1;
end;
function RetVarFunc: Variant;
begin
Result := 1;
end;
// Generated assembler:
push ebx // needs to be saved
mov ebx, eax // EAX contains the address of the return Variant, copies that to EBX
mov eax, ebx // ... not a very smart compiler
mov edx, $00000001
mov cl, $01
call @VarFromInt
pop ebx
ret
接下来,有趣的是看看编译器是如何设置这两者的调用的。以下是调用具有var X:Variant 参数的过程时会发生的情况:
procedure Test;
var X: Variant;
begin
ProcThatTakesOneVarParameter(X);
end;
// compiles to:
lea eax, [ebp - $10]; // EAX gets the address of the local variable X
call ProcThatTakesOneVarParameter
如果我们将“X”设为全局变量,并调用返回 Variant 的函数,我们会得到以下代码:
var X: Variant;
procedure Test;
begin
X := FuncReturningVar;
end;
// compiles to:
lea eax, [ebp-$10] // EAX gets the address of a HIDDEN local variable.
call FuncReturningVar // Calls our function with the local variable as parameter
lea edx, [ebp-$10] // EDX gets the address of the same HIDDEN local variable.
mov eax, $00123445 // EAX is loaded with the address of the global variable X
call @VarCopy // This moves the result of FuncReturningVar into the global variable X
如果您查看此函数的序言,您会注意到用作调用FuncReturningVar 的临时参数的局部变量被初始化为零。如果函数不包含任何Result := 语句,X 将是“未初始化”。如果我们再次调用该函数,则会使用不同的临时隐藏变量!这里有一些示例代码可以看到:
var X: Variant; // global variable
procedure Test;
begin
X := FuncReturningVar;
WriteLn(X); // Make sure we use "X"
X := FuncReturningVar;
WriteLn(X); // Again, make sure we use "X"
end;
// compiles to:
lea eax, [ebp-$10] // first local temporary
call FuncReturningVar
lea edx, [ebp-$10]
mov eax, $00123456
call @VarCopy
// [call to WriteLn using the actual address of X removed]
lea eax, [ebp-$20] // a DIFFERENT local temporary, again, initialized to Unassigned
call FuncReturningVar
// [ same as before, removed for brevity ]
查看该代码时,您会认为返回 Variant 的函数的“结果”总是被调用方初始化为 Unassigned。不对。如果在前面的测试中我们将“X”变量设置为 LOCAL 变量(不是全局变量),编译器将不再使用两个单独的局部临时变量。所以我们有两种不同的情况,编译器生成不同的代码。换句话说,不要做任何假设,总是分配Result。
我对不同行为的猜测:如果可以在当前范围之外访问 Variant 变量,就像全局变量(或类字段)那样,编译器会生成使用线程安全的代码 @VarCopy功能。如果变量是函数的本地变量,则不存在多线程问题,因此编译器可以随意进行直接赋值(不再调用@VarCopy)。