Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

ptrEnd for arrays #798

Closed
wants to merge 5 commits into from

4 participants

@monarchdodra
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
Collaborator

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
Collaborator

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.

std/array.d
@@ -621,6 +621,38 @@ unittest
return decode(a, i);
}
+/**
+ Returns a pointer to one-past-the-end of an array.
+
+Example:
+$(D_RUN_CODE
+$(ARGS
+----
+int[] a = [ 1, 2, 3 ];
+int[] b;
+assert(a.ptr + a.length == a.ptrEnd);
+assert(b.ptr == b.ptrEnd);
+----
+), $(ARGS), $(ARGS), $(ARGS import std.array;))
+
+ $(RED The returned pointer should be used ONLY for pointer arithmatic,
@alexrp Collaborator
alexrp added a note

$(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.

@monarchdodra Collaborator

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@andralex
Owner

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
Collaborator

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

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
Collaborator

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
Collaborator

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
Owner

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
@monarchdodra
Collaborator

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
Commits on Sep 17, 2012
  1. @monarchdodra

    ptrEnd

    monarchdodra authored
  2. @monarchdodra

    Property

    monarchdodra authored
  3. @monarchdodra

    Red warning

    monarchdodra authored
  4. @monarchdodra

    '-_-

    monarchdodra authored
  5. @monarchdodra
This page is out of date. Refresh to see the latest.
Showing with 32 additions and 1 deletion.
  1. +32 −1 std/array.d
View
33 std/array.d
@@ -621,6 +621,37 @@ unittest
return decode(a, i);
}
+/**
+ Returns a pointer to one-past-the-end of an array.
+
+Example:
+$(D_RUN_CODE
+$(ARGS
+----
+int[] a = [ 1, 2, 3 ];
+int[] b;
+assert(a.ptr + a.length == a.ptrEnd);
+assert(b.ptr == b.ptrEnd);
+----
+), $(ARGS), $(ARGS), $(ARGS import std.array;))
+
+ $(RED Warning: The pointer returned by this function will not be
+ considered a live reference to the array by the garbage collector.)
+*/
+@property @trusted pure nothrow
+auto ptrEnd(T)(T arr)
+ if(isArray!T)
+{
+ return arr.ptr + arr.length;
+}
+unittest
+{
+ int[] a = [ 1, 2, 3 ];
+ int[] b;
+ assert(a.ptr + a.length == a.ptrEnd);
+ assert(b.ptr == b.ptrEnd);
+}
+
// overlap
/*
Returns the overlapping portion, if any, of two arrays. Unlike $(D
@@ -648,7 +679,7 @@ inout(T)[] overlap(T)(inout(T)[] r1, inout(T)[] r2) @trusted pure nothrow
static U* min(U* a, U* b) nothrow { return a < b ? a : b; }
auto b = max(r1.ptr, r2.ptr);
- auto e = min(r1.ptr + r1.length, r2.ptr + r2.length);
+ auto e = min(r1.ptrEnd, r2.ptrEnd);
return b < e ? b[0 .. e - b] : null;
}
Something went wrong with that request. Please try again.