Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2851
  • Last Modified:

Clear memory...

Example code:

Var
  Buffer : Array of Byte
begin
 
  SetLength(Buffer, 32768);

  <=== Here is where i want to clear the buffer by setting it with a zero character - as fast as possible.

           I have tried FillChar(Buffer, Length(Buffer), #0) as well as ZeroMemory but it does not seem to work.

end;

Any help on this one?
0
mi6agent
Asked:
mi6agent
  • 2
1 Solution
 
Ivanov_GCommented:
procedure TForm1.Button1Click(Sender: TObject);
var
  Buffer     : array of Byte;
  counter    : Integer;
begin
  SetLength(Buffer, 32768);
  for counter := Low(Buffer) to High(Buffer) do
    Buffer[counter] := 0;
end;
0
 
Ivanov_GCommented:
and variant using ZeroMemory API

procedure TForm1.Button1Click(Sender: TObject);
var
  Buffer     : array of Byte;
begin
  SetLength(Buffer, 32768);
  ZeroMemory(@Buffer[0], sizeof(Buffer));
end;
0
 
mi6agentAuthor Commented:
Thanks Ivanov_G - both examples worked perfectly, now i'm spoilt for choice ;)
0
 
Wim ten BrinkSelf-employed developerCommented:
Ivanov_G gave a good and a bad example. You accepted the bad one since Borland might change it's implementation of dynamic arrays in future Delphi versions. The ZeroMemory option won't work in Delphi 8 for .NET, for example. His first solution where he walks through all elements and initializing them one by one should be considered the better solution.

It's just a warning since I don't know how Borland will handle dynamic arrays in Delphi 2005, the next Delphi version. It might break your code if you decide to upgrade after using ZeroMemory...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now