Name

FillChar Procedure

Syntax

procedure FillChar(var Buffer; Count: Integer; const Fill);

Description

FillChar fills a variable with Count bytes, copying Fill as many times as needed. Fill can be a Byte-sized ordinal value. FillChar is not a real procedure.

Tips and Tricks

  • Note that Buffer is not a pointer. Do not pass the address of a variable, but pass the variable itself. If you dynamically allocate memory, be sure to dereference the pointer when calling FillChar.

  • If the ordinal value of the Fill argument is out of the range of a Byte, Delphi silently uses only the least significant byte as the fill byte. Delphi does not report an error, even if you have overflow checking enabled.

  • The most common use for FillChar is to fill a buffer with zeros. You can also call the SysUtils.AllocMem function, which calls GetMem and then FillChar to fill the newly allocated memory with all zeros.

  • When allocating a new record or array that contains long strings, dynamic arrays, interfaces, or Variants, you must initialize those elements. The Initialize procedure is usually the best way to do this, but it does not initialize any other elements of the array or record. Instead, you can call FillChar to fill the new memory with all zeros, which is a correct initial value for strings, dynamic arrays, interfaces, and Variants. When you free the record, be sure to call Finalize to free the memory associated with the strings, dynamic arrays, interfaces, and Variants.

  • If Count < 0, FillChar does nothing.

Example

// Create a dynamic array of Count integers, initialized to zero.
type
  TIntArray = array of Integer;
function MakeZeroArray(Count: Integer): TIntArray;
begin
  SetLength(Result, Count);
  if Count > 0 then
    FillChar(Result[0], Count*SizeOf(Integer), 0);
end;

See Also

GetMem Procedure, Initialize Procedure, New Procedure, StringOfChar Function
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset