Why is &array[0] @safer than array.ptr?

David Nadlinger via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 25 15:07:31 PST 2017


On Wednesday, 25 January 2017 at 18:12:18 UTC, Jonathan M Davis 
wrote:
> Fine, but in the vast majority of cases, you're calling .ptr, 
> because you're going to be passing the pointer to C code, in 
> which case, doing &arr[0] buys you very little, since the C 
> code is inevitably going to be reading more than that one 
> element,

In that case, calling the C function isn't going to be @safe 
anyway, so you might as well use .ptr.

> So, telling the programmer to use &arr[0] instead of arr.ptr is 
> just plain bizarre.

What you call bizarre is a simple, actionable explanation (which 
is especially important as the behaviour was necessarily a 
backwards-incompatible change). If &arr[0] doesn't actually apply 
to your code, then it was mistakenly @safe before.

  — David


More information about the Digitalmars-d-learn mailing list