arrays: if(null == [ ])

deadalnix deadalnix at gmail.com
Tue May 15 01:42:10 PDT 2012


Le 14/05/2012 21:53, Steven Schveighoffer a écrit :
> On Mon, 14 May 2012 15:30:25 -0400, deadalnix <deadalnix at gmail.com> wrote:
>
>> Le 14/05/2012 16:37, Steven Schveighoffer a écrit :
>>> Note that [] is a request to the runtime to build an empty array. The
>>> runtime detects this, and rather than consuming a heap allocation to
>>> build nothing, it simply returns a null-pointed array. This is 100% the
>>> right decision, and I don't think anyone would ever convince me (or
>>> Andrei or Walter) otherwise.
>>>
>>
>> Obviously this is the right thing to do !
>>
>> The question is why an array of length 0 isn't nulled ? It lead to
>> confusing semantic here, and can keep alive memory that can't be
>> accessed.
>
> int[] arr;
> arr.reserve(10000);
> assert(arr.length == 0);
>
> -Steve

The length isn't set to 0 here. You obviously don't want that to be nulled.


More information about the Digitalmars-d mailing list