Initialize(),Default()和FillChar()之间的区别 [英] Difference between Initialize(), Default() and FillChar()
问题描述
假设你有一个你想要初始化的本地记录:
type
TMyRec = record
Val1,Val2:整数;
结束
程序MyProc;
var
MyVar:TMyRec;
begin
// ...?
WriteLn(Val1,Val2);
结束
除了手动设置每个字段之外,还有几种方法可以实现。
-
使用初始化():
初始化(MyVar);
-
使用默认():
MyVar:= Default(TMyVar);
-
使用 FillChar :
FillChar(MyVar,SizeOf(MyVar),0);
-
定义一个空的常量,并将其分配给var
const cMyVar:TMyVar =();
...
MyVar:= cMyVar;
以上所有似乎都适用于这个例子。我想你甚至可以定义一个全局变量来初始化它。
但是有优先的方法吗?或者有某些情况,不建议使用上述任何一种,或者它根本不起作用?简单的说,简单的方法是使用原始的方式来初始化一个本地堆栈变量? : - )
不要使用 为本地变量初始化
。
初始化应该只能在Delphi代码中使用通过其他方式动态分配变量的新标准过程。
还有什么优化到您的记录的nop,因为它不包含任何托管类型。所以,我们可以把这个选项放开,把这个领域减少到三个竞争对手。
剩下的三个竞争者对未初始化的局部变量都有相同的效果。但是,在具有托管成员的局部变量上使用 FillChar
是有风险的。字符串,接口引用,变体等。如果被管理成员已被初始化,那么您将中断引用计数机制。但是,如果您确定该记录尚未初始化,则 FillChar
是安全有效的,尽管我的观点很丑陋。个人而言,我将拒绝 FillChar
。
留下 Default(T)
和常量赋值。在旧版本的Delphi中,您只能使用常量赋值。可以说,当你宣布常数时,不断的赋值与 FillChar
一样丑陋。所以,在现代的Delphi版本中,我会选择 Default()
,因为它更简洁,读取更好,在我看来。
正如在涉及类似原因的问题中进行了讨论,当您向变量分配 Default(T)
时,编译器会生成非常有效的代码。
Let's say you have a local record that you'd like to initialize:
type
TMyRec=record
Val1, Val2:Integer;
end;
procedure MyProc;
var
MyVar:TMyRec;
begin
// ... ?
WriteLn(Val1,Val2);
end;
Besides setting each field "manually", there are several ways to do it.
Use Initialize():
Initialize(MyVar);
Use Default():
MyVar := Default(TMyVar);
Use FillChar:
FillChar(MyVar,SizeOf(MyVar),0);
Define an empty constant, and assign that to the var
const cMyVar:TMyVar=(); ... MyVar := cMyVar;
The above all seem to work in situations like this example. I guess you could even define a global variable to get it initialized.
But is there a preferred method? Or are there certain situations where it's not recommended to use any of the above, or where it simply won't work?
To put it short, what's the definitive Right Waytm to initialize a local stack variable? :-)
Never use Initialize
for local variables.
Initialize should be used only in Delphi code where a variable is dynamically allocated by other means than the New standard procedure.
What's more it would be optimized to a nop with your record since it doesn't contain any managed types. So, we can throw this option away and reduce the field to three contenders.
The remaining three contenders all have the same effect for an uninitialized local variable. However, it's risky to use FillChar
on a local variable with managed members, e.g. strings, interface references, variants etc. If the managed members have been initialized then you will break the reference counting mechanism. However, if you are sure that the record has not been initialized, then FillChar
is safe and efficient, albeit rather ugly looking in my view. Personally, I would reject FillChar
for this role.
That leaves Default(T)
and the constant assignment. In older versions of Delphi you can only use the constant assignment. It's fair to say that the constant assignment is as ugly as FillChar
, by the time you've declared the constant. So, in modern Delphi versions I would opt for Default()
since it is more concise and reads better, in my view.
As was discussed in a question covering similar ground, the compiler produces very efficient code when you assign Default(T)
to a variable.
这篇关于Initialize(),Default()和FillChar()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!