Am I to understand that this is exactly one past the end of the array or that all pointers past the end of the array are valid.
Only a pointer one-past-the-array or one-past-the-object is valid (although you cannot dereference such a pointer). Pointers after that cannot be constructed, because pointer arithmetic has undefined behavior past this point.
The note above would have me believe that a pointer that does not point to an instantiated object is automatically invalid, since it is pointing to potentially unallocated memory.
The pointer doesn't need to point to an actual object if it is the one-past-end pointer. However such a pointer cannot be dereferenced. The pointers to the array/object, including the one-past-the-end pointer become invalid as soon as the storage duration of the object/array ends.
Which to me would suggest that if a pointer does not point to an array element or an object that has not reached the end of its lifetime, it is invalid.
The one-past-the-end pointers are considered a hypothetical element of the (hypothetical) array for the quoted clauses, see the note under the section referencing [basic.compound].
Would this be undefined behaviour if elem is not in the interval [arr_first, arr_last] since there is no guarantee elem points to anything?
Assuming arr_first is the first element of an array and arr_last the last element of the array, your function has unspecified behavior if elem doesn't point into the range arr_first to arr_last+1 inclusive.
This doesn't mean that it has undefined behavior, just that the return value of the function may be completely arbitrary.
However, trying to form e.g. a pointer arr_last+2 to pass to the function already has undefined behavior itself, since pointer arithmetic is only defined as long as one stays within the bounds of the array (or one-past-the array).
Which in turn invalidates the existence of this function since I can't guarantee its (expected) false results are defined?
The function as written is technically not useful, although I suppose it will work more or less as expected in practice most of the time. It is a much better approach to validate indices into the array, rather than pointers.
发布评论
评论(1)
只有one-past-the-array 或one-past-the-object 指针有效(尽管您不能取消引用这样的指针)。之后的指针无法构造,因为指针算术在这一点之后具有未定义的行为。
如果该指针是后一指针,则该指针不需要指向实际对象。然而这样的指针不能被取消引用。一旦对象/数组的存储期限结束,指向数组/对象的指针(包括尾数指针)就会失效。
末尾后一位指针被视为引用子句的(假设)数组的假设元素,请参阅引用 [basic.compound] 的部分下的注释。
假设
arr_first
是数组的第一个元素,arr_last
是数组的最后一个元素,如果elem
不指向,则您的函数将具有未指定的行为范围为arr_first
到arr_last+1
(含)。这并不意味着它具有未定义的行为,只是函数的返回值可能是完全任意的。
然而,尝试形成一个指针
arr_last+2
来传递给函数本身已经具有未定义的行为,因为指针算术只有在数组边界内(或一个-过去的数组)。所编写的函数在技术上没有用处,尽管我认为它在大多数情况下或多或少会按预期工作。这是验证数组索引而不是指针的更好方法。
Only a pointer one-past-the-array or one-past-the-object is valid (although you cannot dereference such a pointer). Pointers after that cannot be constructed, because pointer arithmetic has undefined behavior past this point.
The pointer doesn't need to point to an actual object if it is the one-past-end pointer. However such a pointer cannot be dereferenced. The pointers to the array/object, including the one-past-the-end pointer become invalid as soon as the storage duration of the object/array ends.
The one-past-the-end pointers are considered a hypothetical element of the (hypothetical) array for the quoted clauses, see the note under the section referencing [basic.compound].
Assuming
arr_first
is the first element of an array andarr_last
the last element of the array, your function has unspecified behavior ifelem
doesn't point into the rangearr_first
toarr_last+1
inclusive.This doesn't mean that it has undefined behavior, just that the return value of the function may be completely arbitrary.
However, trying to form e.g. a pointer
arr_last+2
to pass to the function already has undefined behavior itself, since pointer arithmetic is only defined as long as one stays within the bounds of the array (or one-past-the array).The function as written is technically not useful, although I suppose it will work more or less as expected in practice most of the time. It is a much better approach to validate indices into the array, rather than pointers.