Why is &array[0] @safer than array.ptr?
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 24 03:38:16 PST 2017
On Tuesday, January 24, 2017 11:28:17 Atila Neves via Digitalmars-d-learn
wrote:
> void main() {
> foo;
> }
>
> void foo() @safe {
> int[] array;
> auto ptr = array.ptr;
> }
>
>
> foo.d(7): Deprecation: array.ptr cannot be used in @safe code,
> use &array[0] instead
>
>
> &array[0] is incredibly ugly and feels like an unnecessary hack,
> and I'm wondering why it's @safe.
Likely because it does bounds checking, so you at least know that it's not
null. But I don't see why that would really improve much considering that
the odds are that you're really going to be accessing far more than just the
first element with the pointer. It seems _slightly_ better from a safety
perspective but only slightly. So, I don't know what the point is in
suggesting it as an alternative.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list