seq point/atomic var

R

Roopa

Hi,

Can anybody explain me the meanings of -

1. *Sequence Point* in a expression.
For ex: arr[i++] = i; /* Is undefined cause of non intervening seq point !*/
How does one go about computing the sequence point of long expressions.
Any example illustrating seq points in long expressions would be very
helpful.

2. *Atomic access* ( context of signal handler)

Thanks in advance
- Roopa
 
J

Joona I Palaste

Roopa said:
Can anybody explain me the meanings of -
1. *Sequence Point* in a expression.
For ex: arr[i++] = i; /* Is undefined cause of non intervening seq point !*/
How does one go about computing the sequence point of long expressions.
Any example illustrating seq points in long expressions would be very
helpful.

Sequence points include the following:
- The operators "&&", "||" and ","
- The conditional operator's "?" and ":" parts
- The end of a complete statement
Also there is a sequence point in calling a function, but I can never
remember exactly *where*.
For example, as you say, "arr[i++] = i;" is invalid, but for example
"i++, arr=i;" is completely valid, and so is
"arr[i++ ? 10 : 20] = i;". (Of course assuming the array arr is big
enough to have the appropriate index.)
 
P

pete

Roopa said:
Hi,

Can anybody explain me the meanings of -

1. *Sequence Point* in a expression.
For ex: arr[i++] = i;
/* Is undefined cause of non intervening seq point !*/

You need a sequence point between accesses
when there are side effects.

The side effect of i++, is that i is incremented.
You don't know whether the right operand of the assignment, (i),
is supposed to be accessed prior to or after the side effect.

x = i + i; is fine because there are no side effects.

x = i++ + i; isn't fine

If the initial value of i was zero,
then it might seem like (i++ + i) should either equal zero or one,
depending on whether i++ or i is evaluated first,
but the case is that the C standard commitee just doesn't care
what happens if you're going to write code like that,
so it's undefined.
 
R

Richard Bos

Joona I Palaste said:
Sequence points include the following:
- The operators "&&", "||" and ","
- The conditional operator's "?" and ":" parts

Only at the ?; it makes no sense to have a sequence point at the :,
since it is never possible to execute _both_ the expression before, and
that after the :.
- The end of a complete statement

Complete _expression_. E.g., also after each of the three expressions in
a single for statement.
Also there is a sequence point in calling a function, but I can never
remember exactly *where*.

After the evaluation of its arguments, and just before actually calling
the function.

Just before a library function returns.
After each printf()/scanf() output specification.
Surrounding the comparison function calls to qsort() and bsearch().
After a full declarator.

For more precise descriptions, and references to paragraph numbers in
the Standard, see its appendix C.
For example, as you say, "arr[i++] = i;" is invalid, but for example
"i++, arr=i;" is completely valid, and so is
"arr[i++ ? 10 : 20] = i;".


Not the latter. There's a sequence point between i++ and 10 or 20; but
there's not necessarily one between i++ and i, nor between 10, 20 and i.
(Interestingly, since there is a sequence point at the ?, there must be
a sequence point either between the i and the i++, or between the i and
then 10/20. The problem is, there is no way to tell which of those two
is true in your program.)

Richard
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top