Is it bad practice to alter dynamic arrays that have references to them?

Steven Schveighoffer schveiguy at yahoo.com
Thu Aug 5 11:50:57 PDT 2010


On Thu, 05 Aug 2010 14:41:12 -0400, simendsjo <simen.endsjo at pandavre.com>  
wrote:

> On 05.08.2010 19:35, Steven Schveighoffer wrote:
>> On Thu, 05 Aug 2010 13:10:44 -0400, simendsjo
>> <simen.endsjo at pandavre.com> wrote:
> (...)
>>
>> One helpful function to note is the capacity function:
>>
>> http://digitalmars.com/d/2.0/phobos/object.html#capacity
>>
>> This gives you the capacity of the array, or the largest length it can
>> be set to without reallocating. If it's 0, that means any append will
>> reallocate. This can allow deterministic behavior when appending. Or you
>> can just dup the array if you want to ensure you don't mess up the
>> original copy.
>
> Ah, that's really good to know! So I can see if it will reallocate using
> bool willReallocate = array.capacity < (array.length +  
> numberOfItemsToAppend)
> ?

Yes, except the condition should be <=.  And that function is  
implementation independent (the value returned isn't, but it's guaranteed  
that willReallocate should reflect whether the runtime will reallocate)

>>>
>>> Let me know if it's not accepted to crosspost like this.
>>
>> I think there is no problem with that.
>
> Good, then I'll keep on doing that then.
>
> Thanks a lot for your answer!

No problem!

-Steve


More information about the Digitalmars-d-learn mailing list