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?
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:
*a
must be evaluated to produce a variable, call it var
*b
must be evaluated to produce a value, call it val
val
must be assigned to var
.a
must be incremented.b
must be incremented.What are the constraints on how the compiler may order those?
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:
a
b
Note that I did not say that the increments are required to happen afterwards. Rather, I said that the original values must be used. As 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 exception before b
is evaluated.
Again, a C compiler is permitted to do it the C# way, but not required to.
1)
*a = *b;
a++;
b++;
is equivalent to
*a = *b;
a = a+1;
b = b+1
2)
x = *a++
is equivalent to
x = *a;
a = a+1;
and
*b++ = x
is equivalent to
*b = x;
b = b+1;
so
*a++ = *b++
is equivalent to
*a = *b;
a = a+1;
b = b+1
3)
*(++a) = *(++b)
is equivalent to
a = a+1;
b = b+1
*a = *b;
The exact sequence in which the expressions are evaluated and side effects applied is left unspecified; all that is guaranteed is that the result of *b++
(the value that b
currently points to) is assigned to the result of *a++
(the value that a
currently points to), and that both pointers are advanced. The exact order of operations will vary.
If you want to know how your platform handles it, you can look at the generated machine code, but be aware that it can still vary depending on compiler settings or surrounding code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With