www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is empty array null?

reply Pedro Lacerda <pslacerda gmail.com> writes:
The expression "[] is null" evaluates to true here using 2.058, but I
expected to be false. What am I missing?

Pedro Lacerda
Feb 27 2012
next sibling parent reply Justin Whear <justin economicmodeling.com> writes:
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 &quot;[] is null&quot; 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 &quot;[] is null&quot; 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. If the length is null, where can the ptr member point to other than null? Plus, anyone coming from C uses null to signify empty and anyone coming from Lisp uses the empty list to signify Nil.
Feb 27 2012
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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&quot;[] is null&quot; 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&quot;[] is null&quot; 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
Feb 27 2012
prev sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 02/27/2012 03:17 PM, Justin Whear wrote:
 null makes sense to me. If the length is null, where can the ptr member
 point to other than null?
In the case of empty array slices, ptr can point to anywhere. But then, empty array slices aren't null.
Feb 27 2012
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 27 Feb 2012 15:44:40 -0500, Pedro Lacerda <pslacerda gmail.com>  
wrote:

 The expression "[] is null" evaluates to true here using 2.058, but I
 expected to be false. What am I missing?
The runtime is asked to allocate an empty array. Instead of consuming heap space creating a block with no data in it, it simply returns a null array which is a valid empty array. You should try not to make your code dependent on whether an array pointer is null or not. A null and empty array are for the most part interchangeable. The only place where they differ is when you check to see if the pointer is null. -Steve
Mar 05 2012