Resizable Arrays?

Denis Koroskin 2korden at gmail.com
Fri Feb 27 17:45:28 PST 2009


On Sat, 28 Feb 2009 04:27:52 +0300, Jason House <jason.james.house at gmail.com> wrote:

> BCS Wrote:
>
>> Reply to tim,
>>
>> > Theres nothing stopping you from having all your arrays static in D. I
>> > think your crazy :)
>> >
>>
>> I think the thought is that arrays can have dynamic length but can't be  
>> changed
>> after allocation.
>
> Right. Length is fixed until (re)allication.
>
>
>> starting with:
>>
>> int[] arr = new int[15];
>>
>> assigning to length
>>
>> arr.length = 30;
>>
>> would always become
>>
>> auto tmp = new int[30];
>> tmp[0..min(arr.length,$)] = arr[0..min(tmp.length,$)];
>> arr = tmp;
>
> Either that or a simple compile error...
>
> It's also possible a middle ground where only const(T)[] is fixed length  
> until (re)allocation. I'm hoping there will be good discussion on the  
> pros and cons of alternatives.

I might be wrong, but it seems that arrays are always reallocating memory when resize. I might be wrong but that's the behaviour I discovered in a recent test.




More information about the Digitalmars-d mailing list