www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dynamic array creation

reply Myron Alexander <someone somewhere.com> writes:
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
parent reply orgoton <orgoton mindless.com> writes:
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
next sibling parent Alexander Panek <alexander.panek brainsware.org> writes:
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
prev sibling next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"orgoton" <orgoton mindless.com> wrote in message 
news:f1t00h$h6i$1 digitalmars.com...
 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).
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 }
May 09 2007
parent reply Myron Alexander <someone somewhere.com> writes:
Jarrett Billingsley wrote:
 "orgoton" <orgoton mindless.com> wrote in message 
 news:f1t00h$h6i$1 digitalmars.com...
 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).
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 }
Am I right if I say that the two forms of dynamic array creation are effectively the same?
May 09 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
parent Myron Alexander <someone somewhere.com> writes:
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
prev sibling parent nobody nowhere.nonet writes:
orgoton <orgoton mindless.com> spewed this unto the Network: 
 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.
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 :
May 19 2007