Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
November 09, 2020 New vs length on dymamic array | ||||
---|---|---|---|---|
| ||||
Hello, Are here any differences in creation of dynamic array with known size? > auto array = new wchar[](111); and > wchar[] array; > array.length = 111; |
November 09, 2020 Re: New vs length on dymamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey | On Monday, 9 November 2020 at 08:06:54 UTC, Andrey wrote: > Hello, > > Are here any differences in creation of dynamic array with known size? > >> auto array = new wchar[](111); > > and > >> wchar[] array; >> array.length = 111; You can check using compiler explorer: https://godbolt.org/ |
November 09, 2020 Re: New vs length on dymamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Imperatorn | On Monday, 9 November 2020 at 09:05:58 UTC, Imperatorn wrote:
> On Monday, 9 November 2020 at 08:06:54 UTC, Andrey wrote:
>> Hello,
>>
>> Are here any differences in creation of dynamic array with known size?
>>
>>> auto array = new wchar[](111);
>>
>> and
>>
>>> wchar[] array;
>>> array.length = 111;
>
> You can check using compiler explorer:
> https://godbolt.org/
I don't understand assembly, so this does not tell me anything useful.
Are there any reasons to prefer one over the other? I assume this is what OP is actually asking about.
|
November 09, 2020 Re: New vs length on dymamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey | On Monday, 9 November 2020 at 08:06:54 UTC, Andrey wrote:
> Hello,
>
> Are here any differences in creation of dynamic array with known size?
>
>> auto array = new wchar[](111);
>
> and
>
>> wchar[] array;
>> array.length = 111;
In theory
auto array = new wchar[111]; // or new wchar[](111);
should do less work, but in practice I would guess there will be almost zero difference in speed.
If you need to create new dynamic array with known size you should prefere
auto array = new wchar[111]; // or new wchar[](111);
because it is make much more sense than create empty non initialized array and then set it a length
|
November 09, 2020 Re: New vs length on dymamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey | On Monday, 9 November 2020 at 08:06:54 UTC, Andrey wrote:
> Hello,
>
> Are here any differences in creation of dynamic array with known size?
>
>> auto array = new wchar[](111);
>
> and
>
>> wchar[] array;
>> array.length = 111;
It's the same. If the two are valid then you are in a function. So it's an alloca for the dynamic array payload (8+8 bytes on x86_64) then a LengthExpression that will lead to a GC.malloc, and finally a memset for all the elements default values.
|
November 09, 2020 Re: New vs length on dymamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Imperatorn | On Monday, 9 November 2020 at 09:05:58 UTC, Imperatorn wrote: > On Monday, 9 November 2020 at 08:06:54 UTC, Andrey wrote: >> Hello, >> >> Are here any differences in creation of dynamic array with known size? >> >>> auto array = new wchar[](111); >> >> and >> >>> wchar[] array; >>> array.length = 111; > > You can check using compiler explorer: > https://godbolt.org/ Cool tool! Array creation disassemble: https://godbolt.org/z/GzxWao |
Copyright © 1999-2021 by the D Language Foundation