取消引用空指针

发布于 2024-09-02 16:33:50 字数 97 浏览 1 评论 0原文

int* p = 0;
int* q = &*p;

这是未定义的行为吗?我浏览了一些相关的问题,但这个具体方面没有出现。

int* p = 0;
int* q = &*p;

Is this undefined behavior or not? I browsed some related questions, but this specific aspect didn't show up.

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

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

发布评论

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

评论(4

各自安好 2024-09-09 16:33:50

这个问题的答案是:这取决于您遵循哪种语言标准:-)。

在 C90 和 C++ 中,这是无效的,因为您对空指针执行间接寻址(通过执行 *p),并且这样做会导致未定义的行为。

然而,在 C99 中,这个是有效的、格式良好且定义良好的。在 C99 中,如果 unary-& 的操作数是通过应用 unary-* 或执行下标 ([]),则既不应用 &,也不应用 *[]。例如:

int* p = 0;
int* q = &*p; // In C99, this is equivalent to int* q = p;

同样,

int* p = 0;
int* q = &p[0]; // In C99, this is equivalent to int* q = p + 0;

来自 C99 §6.5.3.2/3:

如果[一元 & 运算符] 的操作数是一元 * 运算符的结果,则该运算符和 & 都不是> 运算符被求值,结果就像两者都被省略一样,除了对运算符的约束仍然适用并且结果不是左值。

同样,如果操作数是 [] 运算符的结果,则隐含的 & 运算符和一元 * 都不是由 [] 进行求值,结果就好像删除了 & 运算符并将 [] 运算符更改为 + 运算符。

(及其脚注,#84):

因此,&*E 等价于 E(即使 E 是空指针)

The answer to this question is: it depends which language standard you are following :-).

In C90 and C++, this is not valid because you perform indirection on the null pointer (by doing *p), and doing so results in undefined behavior.

However, in C99, this is valid, well-formed, and well-defined. In C99, if the operand of the unary-& was obtained as the result of applying the unary-* or by performing subscripting ([]), then neither the & nor the * or [] is applied. For example:

int* p = 0;
int* q = &*p; // In C99, this is equivalent to int* q = p;

Likewise,

int* p = 0;
int* q = &p[0]; // In C99, this is equivalent to int* q = p + 0;

From C99 §6.5.3.2/3:

If the operand [of the unary & operator] is the result of a unary * operator, neither that operator nor the & operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue.

Similarly, if the operand is the result of a [] operator, neither the & operator nor the unary * that is implied by the [] is evaluated and the result is as if the & operator were removed and the [] operator were changed to a + operator.

(and its footnote, #84):

Thus, &*E is equivalent to E (even if E is a null pointer)

淡淡離愁欲言轉身 2024-09-09 16:33:50

是的,这将是未定义的行为,但您的编译器可能会优化 &*

为什么它是未定义的,是因为您正在尝试访问可寻址空间之外的内存。

Yes that would be undefined behavior, but your compiler might optimize the &* out.

Why it its undefined, is that you are attempting to access memory outside your addressable space.

一瞬间的火花 2024-09-09 16:33:50

是的,取消引用空指针是未定义的行为。指针上下文中的整数常量 0 是空指针。就是这样。

现在,如果您的第二行是 int *q = p; 这将是一个简单的指针赋值。如果编译器删除了 &* 并减少了对赋值的取消引用,那就没问题了。

Yes, dereferencing the null pointer is undefined behavior. Integer constant 0 in a pointer context is the null pointer. That's it.

Now, if your second line was int *q = p; that would be a simple pointer assignment. If the compiler removes the &* and reduces the dereference to an assignment, you're OK.

提笔落墨 2024-09-09 16:33:50

恕我直言,就这两行代码而言,地址空间之外没有任何访问。第二条语句只是获取 (*p) 的地址,该地址又是“p”,因此它将存储“0”。但该位置永远不会被访问

IMHO, As far as the two code lines are concerned, there isn't any access outside the address space. The second statement is simply taking the address of (*p) which would be 'p' again and hence it will store '0'. But the location is never accessed.

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