Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

This structure is used to make thousands of calculations in an algorithm like this:

for i:=1 to 900000 do
begin
  CleartheList(MyList);
  DotheMath(MyList);
  DotheChart(MyList);

I am looking for a fast way to initializate the values of my TListSignals to 0 and false.

Now I am using this :

procedure ClearListSignals(var ListSignals:TListSignals);
  i :Integer;
begin
  for i := 0 to MaxSignalRecords - 1 do
  with ListSignals[i] do
  begin
   signal1   :=0;
   signal2   :=0;
   signal3   :=0;
   signal4   :=0;
   signal5   :=0;
   signal6   :=0;
   bsignal1  :=false;
   bsignal2  :=false;
   bsignal3  :=false;
   bsignal4  :=false;
   bsignal5  :=false;
   bsignal6  :=false;

How can I improve the performance of the ClearListSignals procedure?

So if I was to read the value of any ListSignals[i].signal1 this would equal 0? Nice way to avoid the initial assignments. – Simon Apr 2, 2011 at 7:57 i suppose answer from ZENsan is more correct. need to use Default and no hack with direct memory access – Zam Dec 1, 2015 at 19:31 @SolarWind, if the record contains strings have values, ZeroMemory will cause a memory leak. You can call Finalize() on the record first to deallocate the strings before calling ZeroMemory. – dan-gph Jun 27, 2016 at 4:13 That seems to be the most elegant way. I think that if you have a constructor for a record it should zero the memory for you anyway, so that records work more like classes. – Mark Patterson Nov 28, 2018 at 3:07 FillChar is defined in System which makes it platform independant, ZeroMemory only works on Windows. – Jens Mühlenhoff Oct 23, 2012 at 11:05 +1 since it's a good idea. booleans will be aligned on 4 or 8 bytes boundaries, so it's like an awful waste of space, therefore speed in this case. – Arnaud Bouchez Apr 1, 2011 at 6:05 Really, @A.Bouchez? I thought Booleans were aligned on 1-byte boundaries. Are you saying the total size of the record in the question is 48 instead of 30? – Rob Kennedy Apr 1, 2011 at 6:24 Booleans will be aligned based on the compiler alignment switch, unless the record is packed. Also, RAM is pretty cheap these days so I would not worry too much about wasted space. – Misha Apr 1, 2011 at 6:40 @Rob: You're right: use sizeof(TSignalRecord) and you'll get... 32, i.e. there will be 1-byte aligned. And the whole record size is then with packed record, you'll get sizeof(TSignalRecord)=30. With set of 1..6 you'll have sizeof(TSignalRecord)=28. – Arnaud Bouchez Apr 1, 2011 at 6:41 The issue with wasted space is not the monetary cost of RAM but the speed implications that come with poor use of the cache. But it's not so relevant here because Boolean has an alignment of 1. – David Heffernan Apr 1, 2011 at 8:43

The SecureZeroMemory function fills a block of memory with zeros. It is designed to be a more secure version of ZeroMemory.

Use this function instead of ZeroMemory when you want to ensure that your data will be overwritten promptly, as the compiler can optimize a call to ZeroMemory by removing it entirely.

http://msdn.microsoft.com/en-us/library/aa366877%28v=vs.85%29.aspx

LE: here you have how to use it in Delphi if your version does not contain it:

Using SecureZeroMemory in Delphi

SecureZeroMemory as its name implies, is a security feature. It's not intended for general purpose use. Optimising compilers only remove the call to ZeroMemory if it detects that the memory is not reused later - implying there's no point in clearing it. However, it's possible that the memory could contain a password, but is not reused later. Then you still want to ensure the password is cleared from memory lest it appear in memory dumps, or be accessible by a hacking attack. In OP's case, there is absolutely no need to use SecureZeroMemory. Though it is useful to be aware of the function. – Disillusioned Apr 2, 2011 at 11:16

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.