赋值语句的评价Ç秩序 [英] C order of evaluation of assignment statement

查看:124
本文介绍了赋值语句的评价Ç秩序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遇到了在那里的跨平台code的一个基本赋值语句的行为不同的情况。

I've been encountered on a case where cross-platform code was behaving differently on a basic assignment statement.

一个编译器评估的左值第一,第二右值,然后分配。

One compiler evaluated the Lvalue first, Rvalue second and then the assignment.

另外一个编译器做了右值第一个,第二个左值,然后分配。

Another compiler did the Rvalue first, Lvalue second and then the assignment.

这可以具有在壳体的影响左值影响右值的值作为显示在以下情况下:

This may have impact in case Lvalue influence the value of Rvalue as shown in the following case:

struct MM {
    int m;
}
int helper (struct MM** ppmm ) { 
    (*ppmm) = (struct MM *) malloc (sizeof (struct MM)); 
    (*ppmm)->m = 1000;
    return 100;
}

int main() { 
    struct MM mm = {500};
    struct MM* pmm = &mm
    pmm->m = helper(&pmm);
    printf(" %d %d " , mm.m , pmm->m);
}

上面的例子中,行 pmm-> M =帮手(安培;毫米); ,取决于评估的顺序。如果左值评估第一,比pmm-> m是相当于mm.m,如果右值第一计算比pmm-> m是相当于上堆上分配MM的实例。

The example above, the line pmm->m = helper(&mm);, depend on the order of evaluation. if Lvalue evaluated first, than pmm->m is equivalent to mm.m, and if Rvalue calculated first than pmm->m is equivalent to the MM instance that allocated on heap.

我的问题是,是否有一个C标准来确定评估顺序(没有发现任何),或每个编译器可以选择做什么。
还有没有其他类似的陷阱我应该知道的?

My question is whether there's a C standard to determine the order of evaluation (didn't find any), or each compiler can choose what to do. are there any other similar pitfalls I should be aware of ?

推荐答案

= 前pression评价的语义包括

The semantics for evaluation of an = expression include that

更新所述左操作数的存储值的副作用是左和右操作数的值计算后进行测序。 操作数的评价是unsequenced。

The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands. The evaluations of the operands are unsequenced.

(C2011,6.5.16 / 3;强调)

(C2011, 6.5.16/3; emphasis added)

在不同的编译器编译的强调明确规定允许在程序的行为的观察到的差异。此外, unsequenced 的装置,除其他事项外,这是允许的,即使在非常相同的生成程序的不同的运行发生在不同的顺序评价。如果其中unsequenced评估显示,被称为不止一次的功能,那么这将是允许的评估发生在过程中的程序相同的执行中不同的调用顺序不同。

The emphasized provision explicitly permits your observed difference in the behavior of the program when compiled by different compilers. Moreover, unsequenced means, among other things, that it is permissible for the evaluations to occur in different order even in different runs of the very same build of the program. If the function in which the unsequenced evaluations appear were called more than once, then it would be permissible for the evaluations to occur in different order during different calls within the same execution of the program.

这已经回答了这个问题,但要看到更大的画面是很重要的。修改对象或调用这样做的函数是一个副作用(C2011,5.1.2.3/2)。因此,这项关键条款发挥作用的:

That already answers the question, but it's important to see the bigger picture. Modifying an object or calling a function that does so is a side effect (C2011, 5.1.2.3/2). This key provision therefore comes into play:

如果一个标对象的副作用是相对于同标对象或使用同一个标量对象的值的值计算在任一个不同的副作用unsequenced,行为是不确定的。

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.

(C2011,6.5 / 2)

(C2011, 6.5/2)

被调用的函数修改存储在值主的)副作用(的变量 PMM ,赋值的左侧操作数的评价涉及到使用 PMM 值的值计算,这些都是unsequenced,因此该行为是不确定的。

The called function has the side effect of modifying the value stored in main()'s variable pmm, evaluation of the left-hand operand of the assignment involves a value computation using the value of pmm, and these are unsequenced, therefore the behavior is undefined.

未定义行为是不惜一切代价避免。因为你的程序的行为是不确定的,并不仅限于你所观察到的两个备选方案(在情况下还不够糟糕)。 C标准的地方没有任何限制什么,也是可能的。它可能不是崩溃,零出你的硬盘的分区表,或者,如果你有合适的硬件,召唤恶魔鼻。或其他任何东西。其中大部分是不太可能的,但最好的观点是,如果你的程序有不确定的行为那么你的程序的错误

Undefined behavior is to be avoided at all costs. Because your program's behavior is undefined, is not limited to the two alternatives you observed (in case that wasn't bad enough). The C standard places no limitations whatever on what it may do. It might instead crash, zero out your hard drive's partition table, or, if you have suitable hardware, summon nasal demons. Or anything else. Most of these are unlikely, but the best viewpoint is that if your program has undefined behavior then your program is wrong.

这篇关于赋值语句的评价Ç秩序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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