Dynamic array void initialization
Tom
tom at nospam.com
Tue Mar 8 17:36:42 PST 2011
El 08/03/2011 21:42, Kai Meyer escribió:
> On 03/08/2011 02:34 PM, Tom wrote:
>> import std.stdio;
>>
>> struct S {
>> int i;
>> int j;
>> }
>>
>> int main(string[] args) {
>> S[] ss = void;
>> ss.length = 5;
>> foreach (ref s; ss)
>> s = S(1, 2);
>> return 0;
>> }
>>
>> Is the above code correct? (it doesn't work... it blows away or just
>> give and access violation error).
>>
>> I need to create a dynamic array of some struct, but don't want defer
>> contained elements initialization (for performance reasons).
>>
>> Tom;
>
> Any reason you don't just do this:
>
> S[] ss;
> ss.reserve(5)
> foreach(i; 0..i)
> ss ~= S(1, 2);
>
> I think that would not do default initialization on any of the elements,
> and it would ensure that the dynamic array is own "grown" once.
Nope, you're right. That'll work for me.
Thank you,
Tom;
More information about the Digitalmars-d-learn
mailing list