if(arr) now a warning

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 22 03:35:53 PDT 2015


On Wednesday, April 22, 2015 10:19:01 Kagamin via Digitalmars-d wrote:
> On Monday, 20 April 2015 at 18:01:20 UTC, Jonathan M Davis wrote:
> > An array in C is a pointer, not a struct.
>
> True. I only question the claim that they don't care about such
> things.
> Also difference between pointer and reference types seems to be
> no problem for STL string. If it wasn't designed by C
> programmers, then by whom?
>
> > So, the obvious thing for a C programmer when they see
> >
> > if(arr)
> >
> > would be to think that it was equivalent to
> >
> > if(arr != NULL)
>
> Don't you contradict yourself now?
> arr!=null is equivalent to arr.length!=0

Um. No. In C, arr != NULL, and arr.length are completely different. If you
wanted to check the length, you'd either have to have a separate length
variable or being operating on a zero-terminated string and use strlen. And
arr != NULL isn't legal D, so I would have thought that it would be clear
that I meant C code with that line.

> > D tries to avoid segfaulting for null arrays
>
> In fact nothing like that ever happened. Bug 14436 exists because
> there was never any special treatment for null slices.

D arrays were designed in a way that they avoid segfaults; otherwise an
empty array and a null array would not be considered equal, and doing stuff
like trying to append to a null array would segfault. You have to work at it
to get a segfault with D arrays. That doesn't mean that the optimizer does
the best job (as evidenced by 14436), but D arrays are quite clearly
designed in a manner that avoids segfaults and conflates null with empty as
a result.

- Jonathan M Davis



More information about the Digitalmars-d mailing list