Non-Initialized Dynamic Arrays Construction
TheFlyingFiddle
theflyingfiddle at gmail.com
Mon Feb 24 07:43:20 PST 2014
On Monday, 24 February 2014 at 13:08:52 UTC, Mengu wrote:
> On Monday, 24 February 2014 at 11:29:39 UTC, bearophile wrote:
>> TheFlyingFiddle:
>>
>>> http://dlang.org/phobos/std_array.html#.uninitializedArray is
>>> what you want.
>>
>> The OP wants minimallyInitializedArray. uninitializedArray is
>> only for special situations. Perhaps we have to fix the online
>> docs to underline this.
>>
>> Bye,
>> bearophile
>
> what's the difference?
Well for anything that does not have indirections eg numbers /
chars and structs without classes/arrays/pointers in them. Then
the two functions are equivalent.
So:
int[] first = uninitializedArray!(int[])(n);
int[] second = minimallyInitializedArray!(int[])(n);
Both first and second contain garbaged values.
int*[] third = uninitializedArray!(int*[])(n);
int*[] forth = minimallyInitializedArray!(int*[])(n);
The third array contains garbage but all elements in forth are
initialized to null.
I assume this behavior is this way since an int with a garbage
value is not as bad as a garbage pointer dereference into memory.
>> The OP wants minimallyInitializedArray. uninitializedArray is
>> only for special situations.
True but uninitializedArray corresponds to = void semantics for
all arrays.
minimallyInitializedArray is safer though so i guess it should be
used.
>>Wouldn't it be nice to have some kind of syntactic sugar for
>>this similar to what we have for static arrays?
>>
>>BTW: Why isn't simply the following allowed?
>>
>> int n = 3;
>> int[n] = void;
>>
>>Is it too easy to confuse with static array syntax?
I don't like this, since it would be wierd from an allocation
view point. How is int[n] allocated? Is it the GC? Is it alloca?
Can i overide the allocation mechanism? Does the allocated array
behave the same as a static array? (it gets destroyed if it goes
out of scope)
I think it gets a little confusing and also personally i don't
want more hidden memory allocations. We already have alot of
those.
More information about the Digitalmars-d-learn
mailing list