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 ...

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.