Skip to content
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

Closed
wants to merge 5 commits into from
Closed

ptrEnd for arrays #798

wants to merge 5 commits into from

Conversation

monarchdodra
Copy link
Collaborator

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 just arr.ptrEnd

Discussion here: http://forum.dlang.org/thread/kcfqyxbnlhxdkqibfpiu@forum.dlang.org

@alexrp
Copy link
Member

alexrp commented Sep 17, 2012

Please add a big fat warning (with $(RED ...) saying that when the only reference to a GC-managed array is a one-past-the-end pointer, the array can be collected.

@monarchdodra
Copy link
Collaborator Author

Please add a big fat warning (with $(RED ...) saying that when the only reference to a GC-managed array is a one-past-the-end pointer, the array can be collected.

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,
Copy link
Member

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@andralex
Copy link
Member

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.

@alexrp
Copy link
Member

alexrp commented Sep 17, 2012

@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.

@jmdavis
Copy link
Member

jmdavis commented Sep 17, 2012

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.

@alexrp
Copy link
Member

alexrp commented Sep 17, 2012

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.

@jmdavis
Copy link
Member

jmdavis commented Sep 17, 2012

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.

@andralex
Copy link
Member

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.

@andralex andralex closed this Sep 17, 2012
@monarchdodra
Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants