Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

c++ - Do parentheses force order of evaluation and make an undefined expression defined?

I was just going though my text book when I came across this question

  1. What would be the value of a after the following expression ?
    Assume the initial value of a = 5.Mention the steps
    • a+=(a++)+(++a)

At first I thought this is undefined behaviour because a has been modified more than once.
So then I read the question and it said Mention the steps so I probably thought this question is right.

So my question is :

  • Does applying parentheses make an undefined behaviour defined ?
  • Is a sequence point created after evaluating a parentheses expression ?
  • If it is defined,how does the parentheses matter since ++ and () have the same precedence

Note: A well explained and clear answer will get my vote

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, applying parentheses doesn't make it a defined behaviour. It's still undefined. The C99 standard §6.5 ¶2 says

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

Putting a sub-expression in parentheses may force the order of evaluation of sub-expressions but it does not create a sequence point. Therefore, it does not guarantee when the side effects of the sub-expressions, if they produce any, will take place. Quoting the C99 standard again §5.1.2.3¶2

Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.

For the sake of completeness, following are sequence points laid down by the C99 standard in Annex C.

  1. The call to a function, after the arguments have been evaluated.

  2. The end of the first operand of the following operators: logical AND &&; logical OR ||; conditional ?; comma ,.

  3. The end of a full declarator.

  4. The end of a full expression; the expression in an expression statement; the controlling expression of a selection statement (if or switch); the controlling expression of a while or do statement; each of the expressions of a for statement; the expression in a return statement.

  5. Immediately before a library function returns.

  6. After the actions associated with each formatted input/output function conversion specifier.

  7. Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...