marbac said:
Hi,
i read through this faq now, but still there are some questions open to me.
-what is ``sequence point'' in ANSI C's terminology?
Pardon ... but i really dont know what this is.
No problem. But allow me to delay the answer to this question
based on my own sequence point, the end of the posting
-what does the autor mean with: ``after'' ?
The author uses "after" because a lot of programmers think that
in
a++
after means 'immediatly after'. That's not true. It is 'sometimes after'.
-what does the autor mean with : considered ``finished''?
If you have 2 statements
i = 2 * j;
k = 2 * i;
then at the first ';', the first statement is, lously speaking, considered to
be finished. You would expect the code to process the second statement only
after the first one has finished (copletely evaluated) completely.
-what are the: ``multiple, ambiguous side effects''?
The author explained what he means by that.
Anyway: in
i = a++;
There are 2 things going on
the assignment
the increment
The assignment is said to be the 'main action'. After all the whole thing is an
assignment statement. But during evaluation of that statement a second action
is done: the increment. So while the whole thing has the main purpose of
an assignment, it's side effect is to increment a.
Since your post has now triggered the sequence point, back to your first question:
Basically a sequence point is a theoretical concept. Lously speaking every ';'
as well as function calls form a sequence point. It means: during the execution
of an statement, which is done by a sequence of operations, when this sequence point
is reached all side effects (such as increments) must have happend.
Example:
i = a++ + b;
The operations that need to be done to evaluate the above statements are
get value of a
get value of b
add both values
assign to i
increment a
sequence point
The compiler can choose any order it likes as long as the result is correct.
This also includes the positioning of the increment (which is a side effect).
The compiler can do it at any time it likes. The only restriction is: It has to happen
somewhere before the sequence point. So in
i = a++ + b;
a = 5;
a valid sequence would be
1) get a
2) get b
3) increment a
4) add 1) with 2)
5) store result from 4) to i
6) store 5 to a
But it would not be valid for the compiler to do
1) get a
2) get b
3) add 1) with 2)
4) store result from 3) to i
5) store 5 to a
6) increment a
because there is a sequence point after i = a++ + b; and the compiler
is not allowed to move the increment of a after that sequence point.
Another place where a sequence point is located is: after all function
arguments are evaluated, but before the function is called.
Example:
foo( a++ );
The events that need to be done are
1) get a
2) inrement a
3) pass value from 1) to foo
4) call function foo
As said: before the actual function call takes place, there is a sequence
point. That means: the increment has to take place *before* the function
is called. It would not be valid for the compiler to implement the whole
thing as follows:
1) get a
2) pass value from 1) to foo
3) call function foo
4) increment a
That's the whole concept of sequence points: Points in the evaluation
of statements, where side effects are guaranteed to have finished. In a
different wording: The last possible point in time, where they must have
been executed already.