digitalmars.D.learn - Non-Initialized Dynamic Arrays Construction

• =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (5/5) Feb 24 2014 Is it possible void construct a dynamic array such as b in
• TheFlyingFiddle (3/8) Feb 24 2014 http://dlang.org/phobos/std_array.html#.uninitializedArray is
• bearophile (6/8) Feb 24 2014 The OP wants minimallyInitializedArray. uninitializedArray is
• =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (7/10) Feb 24 2014 Wouldn't it be nice to have some kind of syntactic sugar for this
• simendsjo (3/14) Feb 24 2014 Seems very dangerous. If n is available at compile-time it's a static
• Francesco Cattoglio (6/17) Feb 24 2014 to me, it also looks like you are creating an array of ints, and
• Andrej Mitrovic (4/6) Feb 24 2014 There needs to be a ddoc-ed sample demonstrating *exactly* what the
• Mengu (2/10) Feb 24 2014 what's the difference?
• bearophile (5/6) Feb 24 2014 After you have read the online docs of both function what's your
• TheFlyingFiddle (26/50) Feb 24 2014 Well for anything that does not have indirections eg numbers /
=?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
```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;
```
Feb 24 2014
"TheFlyingFiddle" <theflyingfiddle gmail.com> writes:
```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.
```
Feb 24 2014
"bearophile" <bearophileHUGS lycos.com> writes:
```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
```
Feb 24 2014
=?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
```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?
```
Feb 24 2014
simendsjo <simendsjo gmail.com> writes:
```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..?
```
Feb 24 2014
"Francesco Cattoglio" <francesco.cattoglio gmail.com> writes:
```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 :)
```
Feb 24 2014
Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
```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.
```
Feb 24 2014
"Mengu" <mengukagan gmail.com> writes:
```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?
```
Feb 24 2014
"bearophile" <bearophileHUGS lycos.com> writes:
```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
```
Feb 24 2014
"TheFlyingFiddle" <theflyingfiddle gmail.com> writes:
```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.
```
Feb 24 2014