How to remove all items in an associative array?

Oskar Linde oskar.lindeREM at OVEgmail.com
Fri Oct 19 03:37:21 PDT 2007


Matti Niemenmaa wrote:
> Gregor Richards wrote:
>> Unless the performance is a huge hit, it would probably be wiser to use
>> code that isn't dependent on the current implementation of AAs:
>>
>> foreach (k; aa.keys.dup) {
>>     aa.remove(k);
>> }
> 
> I believe aa.keys is a duplicate already, meaning there is no need for the .dup.

You are both right. I updated the DocComments page.

Actually, the proper way to efficiently clear an aa might be to 
explicitly clear or delete the referred aaA*[] array. Leaving the GC a 
potentially quite large array full of pointers to dead objects might be 
a source of memory leaks in an imprecise GC implementation. The same 
potential memory leak exists in _aaRehash() today. Deleting, clearing or 
even mark-as-not-containing-pointers the old array when 
rehashing/resizing the AA should, I believe, improve the situation for 
the GC.

-- 
Oskar



More information about the Digitalmars-d mailing list