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.
varNote that we can also use the SizeOf(Buffer) in this case:
Buffer: array[0..255] of Char;
FillChar(Buffer, Length(Buffer) * SizeOf(Buffer), 0);
// using new function StringElementSize
FillChar(Buffer, Length(buffer) * StringElementSize(Buffer), 0);
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);ZeroMemory, also defined in the Windows unit, is another function which will clear a section of memory with 0.
FillChar(Destination^, Length, Fill);
procedure ZeroMemory(Destination: Pointer; Length: DWORD);As you can see, both FillMemory and ZeroMemory use the FillChar function.
FillChar(Destination^, Length, 0);
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.