unsequenced修改和访问指针 [英] unsequenced modification and access to pointer

查看:2663
本文介绍了unsequenced修改和访问指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个警告,对于这款C前pression:

I'm getting a warning for this C expression:

*p0++ = mult(*p0, psign[i1]); 

该警告是:

unsequenced modification and access to 'p0' [-Wunsequenced]

我觉得前pression应加以修改,以这样的:

I think the expression should be modified to this:

*p0 = mult(*p0, psign[i1]);
p0++;

时的行为(修改后)将是如预期?我认为后的值指向P0已更新指针增量应该发生。

Is the behavior (after modification) going to be as intended? I would think that the pointer increment should happen after the value pointed to by p0 has been updated.

推荐答案

您在上面提供的代码片段调用未定义的行为。据C标准

The snippet you have provided above invokes undefined behavior. According to C standard

如果一个标对象的副作用是相对无论是不同的侧面同样的标量对象或使用同一个标量对象的值的值计算,该行为是不确定的。如果有一个前pression的SUBEX pressions多个允许排序,该行为是未定义如果在任何排序中发生这样的unsequenced副作用。 84)

If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84).

在除权pression * P0 ++ = MULT(* P0,psign [I1])的修改 P0 赋值运算符的左侧前或除权pression的右手侧使用 P0 之后都不测序。因此,段

In the expression *p0++ = mult(*p0, psign[i1]), the modification to p0 on left side of the assignment operator is not sequenced before or after the use of p0 on right hand side of the expression. Therefore, the snippet

*p0++ = mult(*p0, psign[i1]);   

不等同于

*p0 = mult(*p0, psign[i1]);
p0++;                       // Side effect to p0 is guaranteed after the use  
                            // of p0 in mult function

这篇关于unsequenced修改和访问指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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