-
-
Notifications
You must be signed in to change notification settings - Fork 706
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
ptrEnd for arrays #798
ptrEnd for arrays #798
Conversation
|
Please add a big fat warning (with |
Is the message good? Prose has never been my strong suit. |
| ---- | ||
| ), $(ARGS), $(ARGS), $(ARGS import std.array;)) | ||
|
|
||
| $(RED The returned pointer should be used ONLY for pointer arithmatic, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$(RED Warning: The pointer returned by this function will not be considered a live reference to the array by the garbage collector.) should be fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
|
So we define what's supposed to be a convenience function, which comes with a big fat red warning. Doesn't sound like a great abstraction. I think we should just close this. |
|
@andralex This is pretty typical when doing pointer arithmetic. It just so happens that the garbage collector isn't very fond of this. So long as you keep a normal/interior reference to the array around on the stack, in registers, or in the heap, things will be fine. |
|
Given that pointer arithmetic is supposed to be a bit of a rarity, I question that adding a helper function for this is justifiable, especially when you take the warning into account. |
|
It's a helper that can eliminate at least one source of human error when dealing with pointers. I think the warning is completely irrelevant as to whether this should be merged or not. D is a garbage collected language at the same time as being a systems language, and this is just a result of that. In practice, I think people will very rarely iterate an array via pointer arithmetic without keeping a proper reference to it around. |
|
But how often is pointer arithmetic even used in D? This just seems fairly pointless to me, cluttering the library with an extremely simple function for a rare use case. |
|
The warning has relevance because it underlines the intended use and audience of the function. Even assuming the GC was good enough to mind pointers past end, I'd still be very convinced the function is not appropriate for inclusion into Phobos. I will close this now. Thanks again for the contribution @monarchdodra - let's focus on the more fertile ones. Feel free to reopen if new insights come forward. |
No problem. Kind of wish I had closed it myself, but no problem. |
Just a tiny something I've found to be missing: Getting the (one-past-the-) end pointer of an array (dynamic or static).
This is a tiny convenience/helper function, but is (IMO) quite useful.
No more writing
arr.ptr + arr.length. Now, it's justarr.ptrEndDiscussion here: http://forum.dlang.org/thread/kcfqyxbnlhxdkqibfpiu@forum.dlang.org