digitalmars.D.learn - dynamic array creation
- Myron Alexander (11/11) May 09 2007 Hello.
- orgoton (8/11) May 09 2007 If you put
- Alexander Panek (3/5) May 09 2007 Sounds reasonable and correct to me.
- Jarrett Billingsley (15/21) May 09 2007 You're thinking C++. D never calls constructors unless you use 'new'.
- Myron Alexander (3/29) May 09 2007 Am I right if I say that the two forms of dynamic array creation are
- Jarrett Billingsley (6/8) May 09 2007 They will give you exactly the same result, though the
- Myron Alexander (4/12) May 09 2007 Jarrett, Orgoton, Alexander,
- nobody nowhere.nonet (6/19) May 19 2007 I believe this difference only applies to arrays of class objects, and
Hello. What is the difference between: int[] ar = new int[10]; and int[] ar; ar.length = 10; Regards, Myron. d_programming...myron_alexander... (Replace first ... with 'a t', second with .com and remove underscores - getting a lot of spam from this list :) ).
May 09 2007
Assume "Foo" to be a class:int[] ar = new int[10];If you put foo[] ar=new foo[10]; will will generate an array with 10 _instances_ of foo (using default ctor).int[] ar; ar.length = 10;Using foo[] ar; ar.length=10; will generate an array of _references_ (all null) without instantiating foo. I'd prefer if someone would confirm this.
May 09 2007
On Wed, 09 May 2007 13:22:25 -0400 orgoton <orgoton mindless.com> wrote:[...] I'd prefer if someone would confirm this.Sounds reasonable and correct to me.
May 09 2007
"orgoton" <orgoton mindless.com> wrote in message news:f1t00h$h6i$1 digitalmars.com...Assume "Foo" to be a class:You're thinking C++. D never calls constructors unless you use 'new'. class A { this() { writefln("ctor"); } } void main() { A[] a = new A[10]; // nothing is printed writefln(a[0]); // null }int[] ar = new int[10];If you put foo[] ar=new foo[10]; will will generate an array with 10 _instances_ of foo (using default ctor).
May 09 2007
Jarrett Billingsley wrote:"orgoton" <orgoton mindless.com> wrote in message news:f1t00h$h6i$1 digitalmars.com...Am I right if I say that the two forms of dynamic array creation are effectively the same?Assume "Foo" to be a class:You're thinking C++. D never calls constructors unless you use 'new'. class A { this() { writefln("ctor"); } } void main() { A[] a = new A[10]; // nothing is printed writefln(a[0]); // null }int[] ar = new int[10];If you put foo[] ar=new foo[10]; will will generate an array with 10 _instances_ of foo (using default ctor).
May 09 2007
"Myron Alexander" <someone somewhere.com> wrote in message news:f1t63d$s5f$1 digitalmars.com...Am I right if I say that the two forms of dynamic array creation are effectively the same?They will give you exactly the same result, though the T[] arr = new T[n]; form will be slightly faster, only because it doesn't have to check to see what the current length of the array is when it resizes it.
May 09 2007
Jarrett Billingsley wrote:They will give you exactly the same result, though the T[] arr = new T[n]; form will be slightly faster, only because it doesn't have to check to see what the current length of the array is when it resizes it.Jarrett, Orgoton, Alexander, Thanks for the info. Myron.
May 09 2007
orgoton <orgoton mindless.com> spewed this unto the Network:Assume "Foo" to be a class:I believe this difference only applies to arrays of class objects, and not basic types. -- Delete all files? <Y>es, <S>ure, <A>bsolutely, <W>hy not :int[] ar = new int[10];If you put foo[] ar=new foo[10]; will will generate an array with 10 _instances_ of foo (using default ctor).int[] ar; ar.length = 10;Using foo[] ar; ar.length=10; will generate an array of _references_ (all null) without instantiating foo. I'd prefer if someone would confirm this.
May 19 2007