www.digitalmars.com         C & C++   DMDScript  

D - how dynamic arrays work?

reply "Pavel Minayev" <evilone omen.ru> writes:
I wonder how dynamic arrays are implemented in D. Do
they reallocate themselves each time new element is
added? Or do they reserve memory in larger blocks,
so that several (how many?) consequent appends would
not require reallocation, like std::vector does?

I understand that it's implementation-defined, still
it would be useful to know at least how your compiler
works; I just want to decide if I can use dynamic
arrays to generate data dynamically in the memory as
is fast enough, or I have to make a wrapper class
that'd allocate memory in larger blokes.
Dec 21 2001
parent Russell Borogove <kaleja estarcion.com> writes:
Pavel Minayev wrote:
 
 I wonder how dynamic arrays are implemented in D. Do
 they reallocate themselves each time new element is
 added? Or do they reserve memory in larger blocks,
 so that several (how many?) consequent appends would
 not require reallocation, like std::vector does?
 
 I understand that it's implementation-defined, still
 it would be useful to know at least how your compiler
 works; I just want to decide if I can use dynamic
 arrays to generate data dynamically in the memory as
 is fast enough, or I have to make a wrapper class
 that'd allocate memory in larger blokes.
I think that Walter indicated a while back that darrays generally alloc in power-of-two steps up to a given size and then linearly after that, so go ahead and build your darrays one element at a time and let the compiler take care of you. -RB
Dec 21 2001