IS" * P = ++(* Q)QUOT;未定义当p和q指向同一个对象? [英] Is "*p = ++(*q)" undefined when p and q point to the same object?

查看:154
本文介绍了IS" * P = ++(* Q)QUOT;未定义当p和q指向同一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读序列点后,我才知道, I = ++我是不确定的。

after reading about sequence points, I learned that i = ++i is undefined.

那么这个怎么样code:

So how about this code:

int i;
int *p = &i;
int *q = &i;
 *p = ++(*q);           // that should also be undefined right?

比方说,如果p和q的初始化依赖于一些(复杂)条件。
并且它们可以指向同一对象像在上述情况下。
会发生什么?如果它是不确定的,我们可以用什么工具来检测?

Let's say if initialization of p and q depends on some (complicated) condition. And they may be pointing to same object like in above case. What will happen? If it is undefined, what tools can we use to detect?

编辑:如果两个指针不应该指向同一个对象,我们可以使用C99限制?
它是什么严格意味着什么呢?

If two pointers are not supposed to point to same object, can we use C99 restrict? Is it what 'strict' mean?

推荐答案

是的,这是不确定的行为 - 你有一个对象的两处修改他们之间没有一个序列点。不幸的是,检查此自动很难 - (!P = Q)我能想到的是添加断言最好之前这个权利,这将至少给一个干净的运行时错误而不是更坏的东西。在编译时检查,这是在一般情况下不可判定的。

Yes, this is undefined behavior -- you have two modifications of an object without a sequence point between them. Unfortunately, checking for this automatically is very hard -- the best I can think of is adding assert(p != q) right before this, which will at least give a clean runtime fault rather than something worse. Checking this at compile time is undecidable in the general case.

这篇关于IS" * P = ++(* Q)QUOT;未定义当p和q指向同一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆