www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: dynamic array memory allocation

davidl Wrote:

 It's useful. Seperating the concept of capacity and length should be  
 considered carefully
 
 I find myself wondering what actually happens when I create a dynamic  
 array and concatenate items onto it.  I think I read in a post that  
 memory will be over-allocated at times to avoid re-allocating.

 I think it would help out a lot to have an ability to specify  
 over-allocation.  Something like
 uint a = [];
 a.length = 3:10

 Where the array length is 3, but you are guaranteed to have memory  
 allocation for 10, so you can be guaranteed that concatenation up to ten  
 will not need to allocate memory.  This could help in the situation  
 where there is concatenation in a loop, and the programmer over-sizes  
 the array before the loop and re-sizes after the loop.

-- 使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/

I can immediately give an example for the usefulness of this from my field: I'm doing individual-based simulations with many individuals (I'm a theoretical biologist). Typically these individuals reproduce frequently which often involves filling a new array with an unknown number of new objects (if you have discrete generations) or alternatively adding (an unknown number of) objects to an existing array. In C++ being able to do .reserve(large_enough_number) can improve performance considerably. Martin
May 16 2007