Why is &array[0] @safer than array.ptr?
Atila Neves via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 24 03:49:59 PST 2017
On Tuesday, 24 January 2017 at 11:32:47 UTC, TheFlyingFiddle
wrote:
> On Tuesday, 24 January 2017 at 11:28:17 UTC, Atila Neves 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.
>>
>> Atila
>
> Just a speculative guess.
>
> unittest @safe
> {
> int[] array;
>
> auto ptr = array.ptr; //could be null
> auto ptr2 = &array[0]; //Does a bounds check?
> auto ptr3 = &array[5]; //Should do a bounds check.
> }
&array[5] makes sense to bounds check, and I guess then the issue
is I could instead do `array.ptr + 5` which would be bad. But
it's still annoying to have to do &array[0] just to pass it to a
C function, since `my_c_func(array.ptr)` isn't going to screw up
anything.
BTW, in that example above array.ptr is null even though array is
null. It doesn't crash.
Atila
More information about the Digitalmars-d-learn
mailing list