编译器优化与优化增量

发布于 2024-10-01 13:53:36 字数 158 浏览 0 评论 0原文

编译器优化是否会导致这样的代码出现问题?具体来说,可以回复自动增量以按正确的顺序进行评估吗?

uint result = (array[i++] << 16) | (array[i++] << 8) | array[i++];

Does compiler optimization cause a problem with code like this? Specifically, can the auto-increments be replied upon to evaulate in the correct order?

uint result = (array[i++] << 16) | (array[i++] << 8) | array[i++];

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

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

发布评论

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

评论(2

假装不在乎 2024-10-08 13:53:36

是;这是指定的

规范说

表达式中的操作数是
从左到右评价。为了
例如,在 F(i) + G(i++) * H(i) 中,
使用旧值调用方法 F
i,然后调用方法 G
i 的旧值,最后是方法 H
使用 i 的新值进行调用。
这是独立于且无关的
运算符优先级。

Yes; this is specified.

The spec says:

Operands in an expression are
evaluated from left to right. For
example, in F(i) + G(i++) * H(i),
method F is called using the old value
of i, then method G is called with the
old value of i, and, finally, method H
is called with the new value of i.
This is separate from and unrelated to
operator precedence.

稳稳的幸福 2024-10-08 13:53:36

不,我认为您在序列点之间多次修改对象 (i)。 IIRC 按位或或数组索引都不是序列点。您只能修改该对象一次,否则是未指定或未定义的行为。

笔记。这不一定是优化器的问题,只是不合法。

编辑:这是关于序列点的链接 - http://msdn .microsoft.com/en-us/library/d45c7a5d(VS.80).aspx

No, I think you're modifying the object (i) multiple times between sequence points. IIRC neither bitwise or, nor array indexing are sequence points. You are only allowed to modify the object once, doing otherwise is unspecified or undefined behaviour.

NOTE. This is not necessarily an issue to do with the optimizer, it's just not legal.

Edit: Here's a link on sequence points - http://msdn.microsoft.com/en-us/library/d45c7a5d(VS.80).aspx

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