赋值语句的执行顺序

I have just started to learn C, and I get that

*a = *b;
a++;
b++;

and

*a++ = *b++

are equivalent, but is that what's actually happening when the line

*a++ = *b++

is called? Can someone clarify how the compiler is interpreting the second line? I know about right-to-left precedence and such, but can someone precisely write the steps the compiler uses to interpret this line of code?


ANSWER:

You said that you believe that:

*a = *b; a++; b++;

is equivalent to

*a++ = *b++;

but that is false, so you have a false belief. Let's correct your false belief.

In the first case, the following things must happen:

  • VAR: *a must be evaluated to produce a variable, call it var
  • VAL: *b must be evaluated to produce a value, call it val
  • ASSIGN: val must be assigned to var.
  • INCA: a must be incremented.
  • INCB: b must be incremented.

What are the constraints on how the compiler may order those?

  • VAR and VAL must happen before ASSIGN.
  • ASSIGN must happen before INCA.
  • INCA must happen before INCB.

The rule here is that all the side effects of one statement have to be complete before the next statement starts. So there are two legal orderings. VAR VAL ASSIGN INCA INCB, or VAL VAR ASSIGN INCA INCB.

Now let's consider the second case.

*a++ = *b++;

We have the same five operations, but the constraints on their ordering are completely different because these are all in the same statement, so the rule about statements does not apply. Now the constraints are:

  • VAR and VAL must happen before ASSIGN.
  • the evaluation of VAR must use the original value of a
  • the evaluation of VAL must use the original value of b

Note that I did not say that the increments are required to happen afterwards. Rather, I said thatthe original values must be usedAs long as the original value is used, the increment can happen at any time.

So for example, it would be perfectly legal to generate this as

var = a;
a = a + 1; // increment a before assign
*var = *b;
b = b + 1; // increment b after assign

It would also be legal to do this:

val = *b;
b = b + 1; // increment b before assign
*a = val;
a = a + 1; // increment a after assign

It would also be legal to do it as you suggest: do the assignment first, and then both increments in left-to right order. And it would also be legal to do the assignment first, and then both increments in right-to-left order.

A C compiler is given broad latitude to generate code however it likes for this kind of expression. Make sure this is very clear in your mind, because most people get this wrong: just because the ++ comes after the variable does not mean that the increment happens late. The increment can happen as early as the compiler likes as long as the compiler ensures that the original value is used.

That's the rule for C and C++. In C#, the language specification requires that the side effects of the left side of an assignment happen before the side effects of the right side of an assignment, and that both happen before the side effect of the assignment. That same code in C# would be required to be generated as:

var_a = a;
a = a + 1;
// must pointer check var_a here
var_b = b;
b = b + 1;
val = *var_b; // pointer checks var_b
*var_a = val;

The "pointer check" is the point at which C# requires that the runtime verify that var_a is a valid pointer; in other words, that *var_a is actually a variable. If it is not then it must throw an exceptionbefore b is evaluated.

Again, a C compiler is permitted to do it the C# way, but not required to.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值