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 /
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
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;what you want.
Feb 24 2014
TheFlyingFiddle: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
On Monday, 24 February 2014 at 11:29:39 UTC, bearophile wrote:TheFlyingFiddle: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?what you want.
Feb 24 2014
On 02/24/2014 01:08 PM, "Nordlöw" wrote:On Monday, 24 February 2014 at 11:29:39 UTC, bearophile wrote:Seems very dangerous. If n is available at compile-time it's a static array, else it's dynamic..?TheFlyingFiddle: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?you want.
Feb 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: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 :)TheFlyingFiddle: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?what you want.
Feb 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.
Feb 24 2014
On Monday, 24 February 2014 at 11:29:39 UTC, bearophile wrote:TheFlyingFiddle:what's the difference?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
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
On Monday, 24 February 2014 at 13:08:52 UTC, Mengu wrote:On Monday, 24 February 2014 at 11:29:39 UTC, bearophile wrote: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.TheFlyingFiddle:what's the difference?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, bearophileTrue but uninitializedArray corresponds to = void semantics for all arrays. minimallyInitializedArray is safer though so i guess it should be used.The OP wants minimallyInitializedArray. uninitializedArray is only for special situations.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.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