Thread overview
Non-Initialized Dynamic Arrays Construction
Feb 24, 2014
Nordlöw
Feb 24, 2014
TheFlyingFiddle
Feb 24, 2014
bearophile
Feb 24, 2014
Nordlöw
Feb 24, 2014
simendsjo
Feb 24, 2014
Andrej Mitrovic
Feb 24, 2014
Mengu
Feb 24, 2014
bearophile
Feb 24, 2014
TheFlyingFiddle
February 24, 2014
Is it possible void construct a dynamic array such as b in

    int n = 3;
    auto b = new float[n];

similar to what we do with static arrays as in

    int[3] c = void;
February 24, 2014
On Monday, 24 February 2014 at 11:11:44 UTC, Nordlöw wrote:
> Is it possible void construct a dynamic array such as b in
>
>     int n = 3;
>     auto b = new float[n];
>
> similar to what we do with static arrays as in
>
>     int[3] c = void;

http://dlang.org/phobos/std_array.html#.uninitializedArray is what you want.
February 24, 2014
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
February 24, 2014
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.

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?
February 24, 2014
On 02/24/2014 01:08 PM, "Nordlöw" 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.
>
> 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?

Seems very dangerous. If n is available at compile-time it's a static array, else it's dynamic..?
February 24, 2014
On Monday, 24 February 2014 at 12:08:31 UTC, Nordlöw 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.
>
> 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?

to me, it also looks like you are creating an array of ints, and trying to void it's reference. I honestly don't like the look of it either.
Something like "auto arr = new float[n].void" would fit better, but still looks horrible IMO :)
February 24, 2014
On 2/24/14, bearophile <bearophileHUGS@lycos.com> wrote:
> The OP wants minimallyInitializedArray. uninitializedArray is only for special situations.

There needs to be a ddoc-ed sample demonstrating *exactly* what the difference between minimallyInitializedArray and uninitializedArray is.
February 24, 2014
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?
February 24, 2014
Mengu:

> what's the difference?

After you have read the online docs of both function what's your best guess of an answer?

Bye,
bearophile
February 24, 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.