473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Expression evaluation: object read rules confusion

I'm having trouble understanding this part of the expression
evaluation rules ('6.5 Expressions', second item, C99 spec; the C++
wording is presumably identical):
>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.
What precisely does the second part of this statement mean? The
example states that "a[i++] = i" is invalid. But what about, for
example:

a = (b = c) || (c = d);
a = (c = d) || (b = c);

Are these valid or not? Could someone give me a more verbose and
precise interpretation of the second part of the statement?

Thanks -

Tim
Dec 12 '06 #1
2 1982
Tim Johnson wrote:
I'm having trouble understanding this part of the expression
evaluation rules ('6.5 Expressions', second item, C99 spec; the C++
wording is presumably identical):
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.

What precisely does the second part of this statement mean? The
example states that "a[i++] = i" is invalid. But what about, for
example:

a = (b = c) || (c = d);
a = (c = d) || (b = c);

Are these valid or not? Could someone give me a more verbose and
precise interpretation of the second part of the statement?
They're valid. The reason they're valid is because the || operator
introduces a sequence point. If you had used mostly any other operator,
it would not have been valid. Pretending you used |:

a = (b = c) | (c = d);

This is not valid. For a general (but not 100% accurate) guideline:
this is not allowed because the first 'c' can be read at the same time
as the second 'c' is written to.

Dec 12 '06 #2
Tim Johnson wrote:
I'm having trouble understanding this part of the expression
evaluation rules ('6.5 Expressions', second item, C99 spec; the C++
wording is presumably identical):
>>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.

What precisely does the second part of this statement mean?
If you write to some X during evaluation, that must be to work out
what value to write, not for some other reason. So the auto-increment
operations are legal (which is good, otherwise they'd be useless)
but things like:
The example states that "a[i++] = i" is invalid.
are not.

You can read the value as many times as you like while doing this,
so `i = i + i` is legal. (I infer this from the fact that there's
no language that forbids it here and it would otherwise be allowed.)

The phrase "the prior value shall be read only to determine the value
to be stored" means that the write can't happen [1] until all the reads
have happened -- otherwise the value to store couldn't be determined --
and hence there's no undefinedness loopholes from timing problems. In
`a[i++] = i`, you don't know whether the value assigned could be taken
from `i` before or after (or during ...) the increment.
But what about, for
example:

a = (b = c) || (c = d);
a = (c = d) || (b = c);

Are these valid or not?
These are OK because || has a specific evaluation rule; the left
operand is evaluated and then there's a sequence point.
Could someone give me a more verbose and
precise interpretation of the second part of the statement?
Which bit do you need precision engineering on?

[1] Or at least the implementation is obliged to make it /look/
like it can't/didn't happen.

--
Chris "Perikles triumphant" Dollin
Nit-picking is best done among friends.

Dec 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: John Temples | last post by:
Given this code: extern volatile unsigned char v; int main(void) { v; return 0; }
8
by: manan.kathuria | last post by:
hi all , the expression in question is ++i&&++j||++k most sources say that since the result of the || operation is decided by the LHS itself , the right side is not computed my point of...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
8
by: Brian Blais | last post by:
Hello, I have a string input from the user, and want to parse it to a number, and would like to know how to do it. I would like to be able to accept arithmetic operations, like: '5+5'...
5
by: Cylix | last post by:
I am going to write a function that the search engine done. in search engine, we may using double quotation to specify a pharse like "I love you", How can I using regular expression to sperate...
22
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try to set the innerHTML=''; but it does not work. ...
8
by: junky_fellow | last post by:
Hi, Sorry, for asking similar questions again and again. 1) I want to know how should we reslove the ambiguities in a c expression ? Should we use precedence table as mentioned in K&R book ...
2
by: Tim Johnson | last post by:
I'm having trouble understanding this part of the expression evaluation rules ('6.5 Expressions', second item, C99 spec; the C++ wording is presumably identical): What precisely does the...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.