473,385 Members | 2,029 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,385 software developers and data experts.

Is "?" a sequence point?

(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:Th*************@gmail.com>

Jun 9 '06 #1
7 1585
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.

Kenneth Brody wrote:
(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:Th*************@gmail.com>


Jun 9 '06 #2
> Kenneth Brody wrote:
(From something brought up on "Help with array/pointer segmentation
fault needed" thread.)

Is "?" a sequence point?
Yes. "The first operand is evaluated; there is a sequence point after
its evaluation."
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;
Yes, that is guaranteed.
What about this?

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

Yes again.

deepak wrote: 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.

Jun 9 '06 #3
Kenneth Brody <ke******@spamcop.net> writes:
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.

--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield
Jun 9 '06 #4
Kenneth Brody wrote:
(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.

Jun 9 '06 #5


Skarmander wrote On 06/09/06 14:08,:
Kenneth Brody wrote:
(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.


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.

--
Er*********@sun.com

Jun 9 '06 #6
Eric Sosman wrote:

Skarmander wrote On 06/09/06 14:08,:
Kenneth Brody wrote:
(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.


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.

<snip> 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.
Jun 9 '06 #7
Kenneth Brody wrote:

(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:Th*************@gmail.com>
Jun 9 '06 #8

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

Similar topics

15
by: Jordan Rastrick | last post by:
First, a disclaimer. I am a second year Maths and Computer Science undergraduate, and this is my first time ever on Usenet (I guess I'm part of the http generation). On top of that, I have been...
28
by: Alf P. Steinbach | last post by:
A few days ago I posted an "Hello, world!" tutorial, discussed in <url: http://groups.google.no/groups?threadm=41ba4c0a.76869078@news.individual.net>. As I wrote then: <quote> because there...
39
by: TonyJeffs | last post by:
Great book - I like the way that unlike other books, AC++ explains as much as possible about every piece of code discussed, so I'm not left thinking, "well...OK... I get line 12, but I wonder what...
17
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce...
8
by: fxc123 | last post by:
why this program snippet display "8,7,7,8,-7,-8" the program is: main() { int i=8; printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--); }
43
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
12
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
9
by: mdh | last post by:
Given the expression: while (isaspace(c = *s++)) x+1; (s is an array) Does the increment to 's' occur after "x+1" is evaluated, or after the content of s is assigned to c? Is there a...
1
by: mato81 | last post by:
Hi all! I am a newbie to WSDL. I have a questions which has been driving me crazy... If I would have a WSDL with a types element somewhat like below, what is the point of the third last row...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.