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 callingFillChar
.If the ordinal value of the
Fill
argument is out of the range of aByte
, 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 theSysUtils.AllocMem
function, which callsGetMem
and thenFillChar
to fill the newly allocated memory with all zeros.When allocating a new record or array that contains long strings, dynamic arrays, interfaces, or
Variant
s, you must initialize those elements. TheInitialize
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 callFillChar
to fill the new memory with all zeros, which is a correct initial value for strings, dynamic arrays, interfaces, andVariant
s. When you free the record, be sure to callFinalize
to free the memory associated with the strings, dynamic arrays, interfaces, andVariant
s.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.