Skip to content

support pointer arithmetic on pointer to array #2018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
andrewrk opened this issue Mar 1, 2019 · 8 comments
Open

support pointer arithmetic on pointer to array #2018

andrewrk opened this issue Mar 1, 2019 · 8 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Mar 1, 2019

Similar to #863 (when slicing with a comptime length, result type should be pointer to array), pointers to arrays should support pointer arithmetic. The result type is a pointer to an array with a smaller len, and appropriate alignment. Compile error happens if you go out of bounds.

Ran into this working on https://github.com/andrewrk/zig-general-purpose-allocator/. I correctly used a *align(page_size) [page_size]u8 type, but then had to go back and use the worse option [*]align(page_size) u8 due to not having pointer arithmetic.

In general, users should prefer pointers to arrays when the length is compile time known, and the language should support that preference.

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Mar 1, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone Mar 1, 2019
@andrewrk andrewrk added the accepted This proposal is planned. label Mar 1, 2019
@Hejsil
Copy link
Contributor

Hejsil commented Mar 4, 2019

Isn't this the same as #863? With that, you could do:

const arr: [50]u8 = undefined;
const arr_ptr = &arr;
_ = @typeOf(arr_ptr[4..]); // *[46]u8
_ = @typeOf(arr_ptr[10..20]); // *[10]u8

@andrewrk
Copy link
Member Author

andrewrk commented Mar 4, 2019

Good point, I hadn't considered that. Do you think we should not support pointer arithmetic then?

@Hejsil
Copy link
Contributor

Hejsil commented Mar 4, 2019

@andrewrk Only + could realistically be supported as - would be unsafe. So why have 2 ways of doing the same thing? :)

@andrewrk andrewrk removed the accepted This proposal is planned. label Mar 4, 2019
@andrewrk
Copy link
Member Author

andrewrk commented Apr 30, 2019

So why have 2 ways of doing the same thing?

Actually I have an answer to that. In generic code you would expect to use + on both an unknown length pointer and a pointer to an array. It's generally supposed to be better to have a pointer to an array than an unknown length pointer wherever possible. So the argument is that if it works for unknown length pointer, and it could work safely for a pointer to array, then it should work.

@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Sep 20, 2019
@andrewrk andrewrk added the accepted This proposal is planned. label Dec 31, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Dec 31, 2019
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 9, 2020
@andrewrk andrewrk added breaking Implementing this issue could cause existing code to no longer compile or have different behavior. bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code. labels Nov 19, 2020
@andrewrk
Copy link
Member Author

Note that pointer arithmetic currently compiles but does the wrong thing. So this is a pretty high priority issue.

@andrewrk andrewrk modified the milestones: 0.8.0, 0.7.1 Nov 19, 2020
@foobles
Copy link
Contributor

foobles commented Nov 19, 2020

@andrewrk
If you want to allow generically "getting rid of" the first elements of a slice, ptr to statically-sized array, or unknown-size ptr, why not just allow x[i..] for unknown-size ptrs instead? (and would be analogous to ptr arithmetic)
That follows the existing convention of letting [*]T use indexing-syntax, without having to add 2 ways of reslicing a static-array or making a special case for pointer arithmetic.

It would make more sense to allow both x[i..] and x + i for unknown-size pointers, since they conceptually represent similar operations if you think of [*]T like a regular c-style array pointer.

Also, we already have precedent for x[i..] not necessarily returning []T, such as slicing a statically-sized array with a comptime-known range. This has shown to be very useful, and I think this would too.

@ghost
Copy link

ghost commented Nov 24, 2020

With this change, *[n]T can no longer be considered a pointer to [n]T -- it is special-cased as a kind of pointer to T, breaking separability. What if we have *[n][m]T? Does pointer arithmetic advance by the size of [m]T or of T? Either way, you break some expectation. Furthermore, even with this change, *[n]T does not behave like [*]T -- it still does not allow subtraction.

I think a much better solution is to allow x[n..] for [*]T, returning the equivalent of x + n -- that way generic code is happy, and there's no special cases in the type system. @andrewrk, I implore you to reconsider this change.

@andrewrk andrewrk removed the accepted This proposal is planned. label Dec 3, 2020
@andrewrk
Copy link
Member Author

andrewrk commented Dec 4, 2020

Thanks for the counter-proposal - I've removed the accepted label for re-consideration.

In the meantime, afaef36 makes pointer arithmetic on arrays a compile error, so this is no longer a miscompilation.

This issue can now be demoted to a 0.8.0 issue.

@andrewrk andrewrk removed breaking Implementing this issue could cause existing code to no longer compile or have different behavior. bug Observed behavior contradicts documented or intended behavior labels Dec 4, 2020
@andrewrk andrewrk removed the miscompilation The compiler reports success but produces semantically incorrect code. label Dec 4, 2020
@andrewrk andrewrk modified the milestones: 0.7.1, 0.8.0 Dec 4, 2020
aarvay pushed a commit to aarvay/zig that referenced this issue Jan 4, 2021
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
@andrewrk andrewrk modified the milestones: 0.14.0, 0.15.0 Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants