Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
May 09, 2007 dynamic array creation | ||||
---|---|---|---|---|
| ||||
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 Re: dynamic array creation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Myron Alexander | 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 Re: dynamic array creation | ||||
---|---|---|---|---|
| ||||
Posted in reply to orgoton | 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 Re: dynamic array creation | ||||
---|---|---|---|---|
| ||||
Posted in reply to orgoton | "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 Re: dynamic array creation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 Re: dynamic array creation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Myron Alexander | "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 Re: dynamic array creation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 20, 2007 Re: dynamic array creation | ||||
---|---|---|---|---|
| ||||
Posted in reply to orgoton | 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 : |
Copyright © 1999-2021 by the D Language Foundation