Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Bob Swart (aka Drs.Bob) Dr.Bob's Delphi Clinics Dr.Bob's Delphi Courseware Manuals
View Bob Swart's profile on LinkedIn Drs.Bob's Delphi Notes
These are the voyages using Delphi Enterprise (and Architect). Its mission: to explore strange, new worlds. To design and build new applications. To boldly go...
Title:

Unicode tip #5 - FillChar and Fill/ZeroMemory

Author: Bob Swart
Posted: 11/26/2008 11:47:36 AM (GMT+1)
Content:

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.

Back  


8 Comments

AuthorPostedComments
Ali 08/11/26 09:50:22Hi, Why not use SizeOf(Char) instead of SizeOf(Buffer[0]), or StringElementSize(Buffer)?
Michael 08/11/26 12:31:24SizeOf(Char) is possible as well but when changing the type of the array element into e.g. AnsiChar, the code will fail with SizeOf(Char), assuming I didn't change it accordingly. With SizeOf(Buffer[0]) you will always get the correct size of each array element. StringElementSize is new in D2009, if compatibility with older Delphi versions comes into play, the coding will fail. cu, Michael
Olaf Monien 08/11/26 16:34:04I wouldn't recommend using byte oriented operations on Char (or String) types. If you need a byte-buffer, then use "Byte" as data type: buffer: array[0..255] of Byte; I'd also be cautious with FillChar: If you look at the PurePascal implementation in System.pas, then you will notice that it wouldn't even compile (String type mismatch). To me FillChar looks as if it was only half way ported to unicode. (I didn't try to fully understand it's assembly implementation, but basically it copies the low byte of the given value to it's high byte ...)
Mark Johnson 11/07/01 17:21:25I've got a mandate to clean out memory when it is released, including forms. Any thoughts about how to zero out an entire TForm before I FreeAndNil the TForm variable?
@Mark Johnson 12/04/20 07:32:36Change the FreeMem() implementation to add a FillChar() within. You may have to patch the original routine, or add a dedicated MM over FastMM4.
Arrnaud 12/04/20 07:32:54@Mark Johnson Change the FreeMem() implementation to add a FillChar() within. You may have to patch the original routine, or add a dedicated MM over FastMM4.
patrik 13/08/29 11:34:23DRgoBm http://www.5EWalaK3zaEFzKWsK72EARLchwGWGEmy.com
seo service 13/09/06 06:34:13Bo5FEF Major thankies for the article.Really thank you! Much obliged.


New Comment (max. 2048 characters, no HTML):

Name:
Comment:



This webpage © 2005-2019 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.