copy and array length vs capacity. (Doc suggestion?)

Jon D via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 21 19:19:54 PST 2015


On Sunday, 22 November 2015 at 00:31:53 UTC, Jonathan M Davis 
wrote:
>
> Honestly, arrays suck as output ranges. They don't get appended 
> to; they get filled, and for better or worse, the documentation 
> for copy is probably assuming that you know that. If you want 
> your array to be appended to when using it as an output range, 
> then you need to use std.array.Appender.
>
Hi Jonathan, thanks for the reply and the info about 
std.array.Appender. I was actually using copy to fill an array, 
not append. However, I also wanted to preallocate the space. And, 
since I'm mainly trying to understand the language, I was also 
trying to figure out the difference between these two forms of 
creating a dynamic array with an initial size:

    auto x = new int[](n);
    int[] y;  y.reserve(n);

The obvious difference is that first initializes n values, the 
second form does not. I'm still unclear if there are other 
material differences, or when one might be preferred over the 
other :) It's was in this context the behavior of copy surprised 
me, that it wouldn't operate on the second form without first 
filling in the elements. If this seems unclear, I can provide a 
slightly longer sample showing what I was doing.

--Jon




More information about the Digitalmars-d-learn mailing list