-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Comments
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 |
Good point, I hadn't considered that. Do you think we should not support pointer arithmetic then? |
@andrewrk Only |
Actually I have an answer to that. In generic code you would expect to use |
Note that pointer arithmetic currently compiles but does the wrong thing. So this is a pretty high priority issue. |
@andrewrk It would make more sense to allow both Also, we already have precedent for |
With this change, I think a much better solution is to allow |
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. |
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.
The text was updated successfully, but these errors were encountered: