The FillChar routine fills a string or buffer with bytes, which means that we need to multiply the length of the string with the size of the string elements in order to fill the complete size of the string.
var
Buffer: array[0..255] of Char;
begin
FillChar(Buffer, Length(Buffer) * SizeOf(Buffer[0]), 0);
// using new function StringElementSize
FillChar(Buffer, Length(buffer) * StringElementSize(Buffer), 0);
Note that we can also use the SizeOf(Buffer) in this case:
FillChar(Buffer, SizeOf(buffer), 0);
Note that FillChar fills the buffer with Byte values now, and no longer with Char values. A beter named function is FillMemory, defined in the Windows unit:
procedure FillMemory(Destination: Pointer; Length: DWORD; Fill: Byte);
begin
FillChar(Destination^, Length, Fill);
end;
ZeroMemory, also defined in the Windows unit, is another function which will clear a section of memory with 0.
procedure ZeroMemory(Destination: Pointer; Length: DWORD);
begin
FillChar(Destination^, Length, 0);
end;
As you can see, both FillMemory and ZeroMemory use the FillChar function.
This tip is the fifth in a series of Unicode tips taken from my Delphi 2009 Development Essentials book published earlier this week on Lulu.com.