Thread overview
How to check whether an empty array variable is null?
Oct 10, 2015
tcak
Oct 10, 2015
rumbu
Oct 10, 2015
Meta
Oct 11, 2015
Ozan
Oct 10, 2015
Jonathan M Davis
Oct 11, 2015
Meta
Oct 11, 2015
Meta
Oct 11, 2015
tcak
Oct 11, 2015
Jonathan M Davis
October 10, 2015
[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.
October 10, 2015
On Saturday, 10 October 2015 at 15:20:04 UTC, tcak 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.


Long discussion: http://forum.dlang.org/thread/rrrtkfosfnfuybblexow@forum.dlang.org

October 10, 2015
On Saturday, 10 October 2015 at 15:20:04 UTC, tcak 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.

Yes, it's correct behaviour. `array is null` checks whether array.ptr is null, which is the case for a 0-length array.

void main()
{
	auto a = new int[0];
	writeln(a.ptr);      //a.ptr is null
	
	auto a2 = new int[1];
	writeln(a2.ptr); //a2.ptr is not null
	a2 = a[0..$];    //Slice off the only element of a2
	writeln(a2.ptr); //Now a2.ptr is null
}
October 10, 2015
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

October 11, 2015
On Saturday, 10 October 2015 at 20:07:11 UTC, Jonathan M Davis wrote:
> 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

Look at my second example.
October 11, 2015
On Sunday, 11 October 2015 at 00:18:54 UTC, Meta wrote:
> On Saturday, 10 October 2015 at 20:07:11 UTC, Jonathan M Davis wrote:
>> 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
>
> Look at my second example.

Sorry, I thought you were responding to me.
October 11, 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.
October 11, 2015
On Saturday, 10 October 2015 at 15:46:51 UTC, Meta wrote:
> On Saturday, 10 October 2015 at 15:20:04 UTC, tcak wrote:
>> [code]
>> 	int[] list;
>>
>> 	list = new int[0];
>>
>> 	std.stdio.writeln("Is Null ? ", (list is null));
>> [/code]
>>
>> Result is "Is Null? true".
>>
....
> }

Do I miss the point?
Shouldn't have "new int[0]" , "[]" , ".length = 0" and "null" the same meaning like "array is empty"? I never understood why [] and null have different meanings in other languages.

And following the discussion...In my mind, aren't pointer operations dangerous?...yes,sometimes necessary...but always like an operation on a open heart.

Regards, Ozan
October 11, 2015
On Sunday, October 11, 2015 05:10:34 tcak via Digitalmars-d-learn wrote:
> 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.

In general, because of how arrays tend to conflate null and empty, it's a bad idea to differentiate between null and empty with arrays. I don't know exactly what you're doing, but there's no reason to check for null before iterating over an array, because null == "" and null == []. You'll never get a segfault from operating on a null array unless you try and do something with its ptr property explicitly. Almost everything treats a null array the same as an empty array. If you really need to have a null value for arrays, consider using std.typecons.Nullable to wrap the array.

- Jonathan M Davis