相关疑难解决方法(0)

为什么大多数Delphi示例使用FillChar()来初始化记录?

我只是想知道为什么大多数Delphi示例使用FillChar()来初始化记录.

type
  TFoo = record
    i: Integer;
    s: string; // not safe in record, better use PChar instead
  end;

const
  EmptyFoo: TFoo = (i: 0; s: '');

procedure Test;
var
  Foo: TFoo;
  s2: string;
begin
  Foo := EmptyFoo; // initialize a record

  // Danger code starts
  FillChar(Foo, SizeOf(Foo), #0);
  s2 := Copy("Leak Test", 1, MaxInt); // The refcount of the string buffer = 1
  Foo.s = s2; // The refcount of s2 = 2
  FillChar(Foo, SizeOf(Foo), #0); // The refcount …
Run Code Online (Sandbox Code Playgroud)

delphi initialization record

28
推荐指数
3
解决办法
2万
查看次数

标签 统计

delphi ×1

initialization ×1

record ×1