Is empty array null?

Ali Çehreli acehreli at yahoo.com
Mon Feb 27 13:27:06 PST 2012


On 02/27/2012 01:17 PM, Justin Whear wrote:
 > On Mon, 27 Feb 2012 17:44:40 -0300, Pedro Lacerda wrote:
 >
 >> The expression "[] is null" evaluates to true here using 2.058, but I
 >> expected to be false. What am I missing?
 >>
 >> Pedro Lacerda
 >> <div>The expression"[] is null" evaluates to true here using
 >> 2.058, but I expected to be false. What am I
 >> missing?</div><div><br></div><div>Pedro Lacerda</div><br>  The 
expression
 >> "[] is null" evaluates to true here using 2.058, but I expected to be
 >> false. What am I missing?
 >>
 >> Pedro Lacerda
 >> <div>The expression"[] is null" evaluates to true here using
 >> 2.058, but I expected to be false. What am I
 >> missing?</div><div><br></div><div>Pedro Lacerda</div><br>
 >
 > null makes sense to me.

But null also means uninitialized (for reference types anyway). Is a 
slice that has just become empty is uninitialized?

 > If the length is null, where can the ptr member
 > point to other than null?

Since .ptr should not be derefenced, it can point to anywhere.

void main()
{
     int[] a = [ 1, 2 ];
     a = a[0..0];

     // Both of these pass
     assert(a is null);
     assert(a.ptr !is null);
}

And I think that's another problem: If a.ptr is non-null, then the 
memory occupied by the initial elements will not be freed by the GC.

 > Plus, anyone coming from C uses null to signify
 > empty

Not necessarily. A C data structure may contain no elements and be non-null.

 > and anyone coming from Lisp uses the empty list to signify Nil.

Ali



More information about the Digitalmars-d-learn mailing list