Why most Delphi examples use FillChar() to initialize records?

前端 未结 8 2090
离开以前
离开以前 2020-12-13 06:32

I just wondered, why most Delphi examples use FillChar() to initialize records.

type
  TFoo = record
    i: Integer;
    s: string; // not safe in record, b         


        
8条回答
  •  情深已故
    2020-12-13 07:22

    FillChar is fine to make sure you don't get any garbage in a new, uninitialized structure (record, buffer, arrray...).
    It should not be used to "reset" the values without knowing what your are resetting.
    No more than just writing MyObject := nil and expecting to avoid a memory leak.
    In particulart all managed types are to be watched carefully.
    See the Finalize function.

    When you have the power to fiddle directly with the memory, there is always a way to shoot yourself in the foot.

提交回复
热议问题