How to check whether an empty array variable is null?

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Oct 10 22:10:34 PDT 2015


On Saturday, 10 October 2015 at 20:07:11 UTC, Jonathan M Davis 
wrote:
> On Saturday, October 10, 2015 15:20:02 tcak via 
> Digitalmars-d-learn wrote:
>> [code]
>>   int[] list;
>>
>>   list = new int[0];
>>
>>   std.stdio.writeln("Is Null ? ", (list is null));
>> [/code]
>>
>> Result is "Is Null? true".
>>
>> Is this the correct behaviour? I would expect compiler to 
>> point to an address in the heap, but set the length as 0. So, 
>> it wouldn't return null, but the length would be 0 only.
>
> It basically didn't bother to allocate an array on the heap, 
> because you asked for one with a length of zero. 
> Efficiency-wise, it makes no sense to allocate anything. You 
> wouldn't be doing anything with the memory anyway. The only way 
> that you're going to get an array of length 0 which doesn't 
> have a null ptr is to slice an array down to a length of 0.
>
> - Jonathan M Davis

The situation is that the "length" parameter comes from user. 
Also the item values come from user as well. I create the array 
with "length" parameter. At another part of code, I check firstly 
whether the array is created [code] if( array is null ) [/code], 
then the items are checked for validation.


More information about the Digitalmars-d-learn mailing list