Is "?" a sequence point?

K

Kenneth Brody

(From something brought up on "Help with array/pointer segmentation
fault needed" thread.)

Is "?" a sequence point?

Or, more directly, is the following defined?

/* Will "ptr" be guaranteed to have been assigned before the "?"
* part is evaluated?
*/
foo = ( ( ptr = some_long_expression ) != NULL )
? ptr->bar
: NULL;

What about this?

/* Is the increment of x guaranteed to be complete before the
* "?" part is evaluated?
*/
foo = ( ++x == end ) ? x : y;

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
D

deepak

Hi Kennedy,

The increment is guarenteed, because after doing the comparison only we
can go for the
next part. I mean the code after '?'.

In the first case also the same 'll happen.

Deepak.
 
B

Ben Pfaff

Kenneth Brody said:
Is "?" a sequence point?

6.5.15 Conditional operator
Syntax
1 conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression

[...]

Semantics
4 The first operand is evaluated; there is a sequence point after its evaluation.
 
G

Guest

Yes. "The first operand is evaluated; there is a sequence point after
its evaluation."

Yes, that is guaranteed.

Yes again.
Hi Kennedy,

The increment is guarenteed, because after doing the comparison only we
can go for the
next part.

If there were no sequence point, it would be entirely possible to do
the store to x after doing the comparison. ++x is equivalent to x += 1,
so the below applies here.
I mean the code after '?'.

In the first case also the same 'll happen.

If there were no sequence point, it would be entirely legitimate to do
the store to ptr after doing the comparison. Ignoring possible special
behaviour with volatile objects, the result of (a = b) is b, converted
to the type of a. It is possible to get this result without even
evaluating a, let alone actually storing a value in it. The only
guarantee you have is that the store will be done before the next
sequence point, and there are real-world cases where behaviour by
compilers will give different results than your logic would.
 
S

Skarmander

Kenneth said:
(From something brought up on "Help with array/pointer segmentation
fault needed" thread.)

Is "?" a sequence point?

Or, more directly, is the following defined?

/* Will "ptr" be guaranteed to have been assigned before the "?"
* part is evaluated?
*/
foo = ( ( ptr = some_long_expression ) != NULL )
? ptr->bar
: NULL;

What about this?

/* Is the increment of x guaranteed to be complete before the
* "?" part is evaluated?
*/
foo = ( ++x == end ) ? x : y;

Notwithstanding the topical answers given, and the obvious need to know, the
actual answer to this question ought to be "you shouldn't have to be
required to care, so don't do that".

Is there a separate word yet for the incurable urge to cram as much
side-effects into one expression as possible? Somehow "terseness" isn't
descriptive enough.

S.
 
E

Eric Sosman

Skarmander wrote On 06/09/06 14:08,:
Notwithstanding the topical answers given, and the obvious need to know, the
actual answer to this question ought to be "you shouldn't have to be
required to care, so don't do that".

Is there a separate word yet for the incurable urge to cram as much
side-effects into one expression as possible? Somehow "terseness" isn't
descriptive enough.

Personally, I see nothing wrong with

value = (ptr == NULL) ? 0 : ptr->value;

.... and find it easier to read than some of the lengthier
alternatives. Terseness should not be a goal in itself
(although 'tis said "brevity is the soul of wit"), but
neither should verbosity, prolixity, unnecessary redundant
persiflage, wordiness, and verbosity (if you're only half-
brief, you're a half-wit).

A fellow who worked for me once was a native speaker
of one of the agglutinative languages, and had the habit
of jamming many words together to form enormous identifiers.
(How enormous? When we ported the program to a system where
external identifiers were only significant in their first
thirty-two characters, his stuff ran afoul of the limit.)

He was a smart and subtle coder, but his stuff was very
nearly unreadable simply because of the number of characters
you had to digest. His `for' usually required three lines
all to itself and never fewer than two, and no assignment
statement with more than two operators on the r.h.s. could
fit on a single line. Try to read

secondaryFragmentImpactTime = (
sqrt(secondaryFragmentInitialVelocityX
* secondaryFragmentInitialVelocityX
- 4 * secondaryFragmentAccelerationX
* secondaryFragmentImpactOffsetX)
- secondaryFragmentInitialVelocityX)
/ (2 * secondaryFragmentAccelerationX);

.... and ponder whether you'd have preferred to read

t = (sqrt(b*b - 4*a*c) - b) / (2 * a);

Terseness shouldn't be an end, but it's a good means.
 
S

Skarmander

Eric said:
Skarmander wrote On 06/09/06 14:08,:

Personally, I see nothing wrong with

value = (ptr == NULL) ? 0 : ptr->value;
Number of side-effects, excluding the top level statement: 0.

I'm specifically talking about the examples above, or really anything that
requires you to know what a sequence point is.
... and find it easier to read than some of the lengthier
alternatives. Terseness should not be a goal in itself
(although 'tis said "brevity is the soul of wit"), but
neither should verbosity, prolixity, unnecessary redundant
persiflage, wordiness, and verbosity (if you're only half-
brief, you're a half-wit).
While this applies without reservation to natural languages, matters are
slightly different for programming languages. Clarity of expression and not
repeating yourself are universally important, but other aspects do not
transfer as clearly. I've yet to hear much linguistic debate on sequence
points.

He was a smart and subtle coder, but his stuff was very
nearly unreadable simply because of the number of characters
you had to digest. His `for' usually required three lines
all to itself and never fewer than two, and no assignment
statement with more than two operators on the r.h.s. could
fit on a single line. Try to read

secondaryFragmentImpactTime = (
sqrt(secondaryFragmentInitialVelocityX
* secondaryFragmentInitialVelocityX
- 4 * secondaryFragmentAccelerationX
* secondaryFragmentImpactOffsetX)
- secondaryFragmentInitialVelocityX)
/ (2 * secondaryFragmentAccelerationX);

... and ponder whether you'd have preferred to read

t = (sqrt(b*b - 4*a*c) - b) / (2 * a);

Terseness shouldn't be an end, but it's a good means.

All this I do not dispute, although it's largely orthogonal to the
particular windmill I was tilting at.

S.
 
K

Kenneth Brody

Kenneth said:
(From something brought up on "Help with array/pointer segmentation
fault needed" thread.)

Is "?" a sequence point?
[...]

Thanks to all those who responded, including C&V.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Sequence point. 10
Sequence point(s) 12
realloc() to zero size 5
Calling functions with the wrong parameters 7
Sorta-OT: John Backus obit 5
YKYBRclcTLW 13
Pointer arithmetic question 23
C FAQs section 20.27 9

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top