digitalmars.D.learn - Is empty array null?
- Pedro Lacerda (3/3) Feb 27 2012 The expression "[] is null" evaluates to true here using 2.058, but I
- Justin Whear (4/18) Feb 27 2012 null makes sense to me. If the length is null, where can the ptr member
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (17/38) Feb 27 2012 But null also means uninitialized (for reference types anyway). Is a
- Ellery Newcomer (3/5) Feb 27 2012 In the case of empty array slices, ptr can point to anywhere. But then,
- Steven Schveighoffer (10/12) Mar 05 2012 The runtime is asked to allocate an empty array. Instead of consuming
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
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. 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
On 02/27/2012 01:17 PM, Justin Whear wrote:On Mon, 27 Feb 2012 17:44:40 -0300, Pedro Lacerda wrote:expressionThe 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> TheBut null also means uninitialized (for reference types anyway). Is a slice that has just become empty is uninitialized?"[] 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.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 emptyNot 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
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
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