比较无效指针的行为的澄清

发布于 2025-01-10 02:30:41 字数 1432 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2025-01-17 02:30:41

我是否理解这恰好是数组末尾的一个,或者所有超出数组末尾的指针都是有效的。

只有one-past-the-array 或one-past-the-object 指针有效(尽管您不能取消引用这样的指针)。之后的指针无法构造,因为指针算术在这一点之后具有未定义的行为。

上面的注释让我相信,不指向实例化对象的指针自动无效,因为它指向潜在的未分配内存。

如果该指针是后一指针,则该指针不需要指向实际对象。然而这样的指针不能被取消引用。一旦对象/数组的存储期限结束,指向数组/对象的指针(包括尾数指针)就会失效。

对我来说,如果指针不指向数组元素或尚未到达其生命周期结束的对象,则它是无效的。

末尾后一位指针被视为引用子句的(假设)数组的假设元素,请参阅引用 [basic.compound] 的部分下的注释。

如果 elem 不在区间 [arr_first, arr_last] 中,因为不能保证 elem 指向任何内容,这是否是未定义的行为?

假设 arr_first 是数组的第一个元素,arr_last 是数组的最后一个元素,如果 elem 不指向,则您的函数将具有未指定的行为范围为 arr_firstarr_last+1(含)。

这并不意味着它具有未定义的行为,只是函数的返回值可能是完全任意的。

然而,尝试形成一个指针arr_last+2来传递给函数本身已经具有未定义的行为,因为指针算术只有在数组边界内(或一个-过去的数组)。

这反过来又使该函数的存在无效,因为我无法保证定义了其(预期的)错误结果?

所编写的函数在技术上没有用处,尽管我认为它在大多数情况下或多或少会按预期工作。这是验证数组索引而不是指针的更好方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文