Shortest way to allocate an array and initialize it with a specific value.

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 11 04:42:19 PDT 2015


On Thursday, 11 June 2015 at 07:57:47 UTC, Per Nordlöw wrote:
> On Wednesday, 10 June 2015 at 22:03:52 UTC, Ali Çehreli wrote:
>> Another option:
>>
>> void main()
>> {
>>    auto a2 = new ubyte[5];
>
> But this causes an extra zero-initialization of a2.
>
>>    a2[] = 0xAA;    // <-- Assign to "all elements"
>
> Is
>
>     auto a2 = value.repeat(size).array;
>
> better in this regard?

Yes, it uses `uninitializedArray()` if the length is known (which 
it is for `repeat()`):
https://github.com/D-Programming-Language/phobos/blob/master/std/array.d#L111


More information about the Digitalmars-d-learn mailing list