www.digitalmars.com         C & C++   DMDScript  

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

reply =?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
parent reply "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
parent reply "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
next sibling parent reply =?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
next sibling parent 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
prev sibling parent "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
prev sibling next sibling parent 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
prev sibling parent reply "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
next sibling parent "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
prev sibling parent "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