473,386 Members | 1,606 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,386 software developers and data experts.

Sequence points

j
In a footnote in the c99 standard the following is labeled as
undefined:

a[i++] = i;

And in the second clause of section 6.5 the following is stated:

"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. 70)"

So my question is, how do they derive undefined behaviour from "a[i++]
= i;" ?

With, a[i++] = i; this is one expression and there is only one
sequence point here, ';'.
'i' has its value modified only once and that is in the subscript
operator to designate which object the value of 'i', on the right side
of the assignment operator, will be stored at.

But, I am guessing I am missing something here. So if anyone could
enlighten me it would be much appreciated :)
Also, what is the difference between unspecified behaviour and
undefined behaviour? The standard attempts to make a distinction
between the two in Annex J but my dictionary gives the same definition
for "unspecified" and "undefined" so I fail to see how they can be
used to describe certain things as though they were different.
Nov 13 '05 #1
4 8126
j wrote:

In a footnote in the c99 standard the following is labeled as
undefined:

a[i++] = i;

And in the second clause of section 6.5 the following is stated:

"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. 70)"

So my question is, how do they derive undefined behaviour from "a[i++]
= i;" ?

With, a[i++] = i; this is one expression and there is only one
sequence point here, ';'.
'i' has its value modified only once and that is in the subscript
operator to designate which object the value of 'i', on the right side
of the assignment operator, will be stored at.

But, I am guessing I am missing something here. So if anyone could
enlighten me it would be much appreciated :)
It's the "furthermore" part that bites you here.

The `i' on the right-hand side retrieves the value stored
in the variable `i', correct? And is this retrieval for the
purpose of determining what new value to store into `i'? No,
it is not: the value retrieved is intended to be stored somewhere
in the `a' array (but, because of U.B., there's no telling what
might actually happen). So the right-hand-side use of `i' falls
afoul of the "furthermore," and you've got trouble.

Why the strange restriction? Because the Standard allows
the side-effect of incrementing `i' to occur *anywhere* between
the sequence point prior to this statement and the sequence
point at the end. It might be the very first thing that happens,
it might be the very last, it might even happen in parallel with
other activities. That being the case, there's just no way to
say what value the right-hand-side evaluation of `i' should
produce, or even whether it *can* produce a meaningful value
(think of 32-bit `long' on an 8-bit machine, where the operation
of storing the new value might take several cycles).

Of course, this "fuzziness" about the value doesn't extend
to the `i++' on the left-hand side, if used in isolation. This
sub-expression is required to yield the old value of `i', but
the compiler can indulge in whatever sleight-of-hand it feels
like to get this to occur. For example, it might rewrite the
sub-expression as `(a - 1)[++i]' if that leads to smaller or
faster code. There's just no telling when (between one sequence
point and the next) the incrementation side-effect will become
"visible" to the world at large.
Also, what is the difference between unspecified behaviour and
undefined behaviour? The standard attempts to make a distinction
between the two in Annex J but my dictionary gives the same definition
for "unspecified" and "undefined" so I fail to see how they can be
used to describe certain things as though they were different.


The Standard uses these words (and others) in a restricted
and specialized sense because it needs more precision than is
afforded by ordinary English. Other fields of discourse also
use ordinary words in specialized ways -- for example, a "field"
means rather different things to farmers, mathematicians, and
footballers. In Humpty Dumpty's phrase, "When I use a word, it
means exactly what I wan it to mean-- neither more nor less. It's
a question of who's to be master, that's all."

The Standard gives its particular definitions of these terms
in sections 3.4.3 and 3.4.4, and you're welcome to ponder them
there. Informally,

- For "unspecified" behavior, the Standard enumerates all
the permissible behaviors but doesn't say which of them
might actually occur. You know you'll get either A or
B or C, but not which -- and you might not get the same
one next time, either. However, you won't get X.

- For "undefined" behavior, the Standard specifies nothing
at all. You cannot assume the result will be A, B, or C;
anything at all could happen: you could get X, or Y, or
the program could halt, or chocolate pudding could ooze
from your keyboard -- or, of course, you could get A.

Nothing prevents a particular implementation from specifying
what the Standard leaves unspecified or defining what the Standard
leaves undefined. A particular implementation might specify that
all operands are evaluated left-to-right, which would make the
behavior of `a[i++] = i' perfectly well-defined -- but only for
that implementation, of course. Another might actually define
`a[i++] = i' as causing demons to fly from your nose -- and much
as I might admire their technical expertise, I have absolutely
no desire to meet the people responsible for that outcome ...

--
Er*********@sun.com
Nov 13 '05 #2

Shill <no****@example.com> wrote in message
news:bf***********@biggoron.nerim.net...
Another might actually define
`a[i++] = i' as causing demons to fly from your nose -- and much
as I might admire their technical expertise, I have absolutely
no desire to meet the people responsible for that outcome ...


I've read the demon-from-nose bit several times in this group.

Pray tell, which compiler might pull off such an engineering feat?
If Bill Gates has really made a pact with the Devil, then perhaps VC
is our closest bet?


I don't know about Bill, but Mother Nature has achieved
this long ago. Ask any allergy sufferer. :-)

-Mike

Nov 13 '05 #3


j wrote:

In a footnote in the c99 standard the following is labeled as
undefined:

a[i++] = i;
Please review the FAQ list:

http://www.eskimo.com/~scs/C-faq/top.html

Also, what is the difference between unspecified behaviour and
undefined behaviour? The standard attempts to make a distinction
between the two in Annex J but my dictionary gives the same definition
for "unspecified" and "undefined" so I fail to see how they can be
used to describe certain things as though they were different.


And again, see the FAQ.


Brian Rodenborn
Nov 13 '05 #4
On Wed, 23 Jul 2003, Eric Sosman wrote:
j wrote:
Furthermore, the prior value shall be read only to determine the value
to be stored. 70)"


It's the "furthermore" part that bites you here.

The `i' on the right-hand side retrieves the value stored
in the variable `i', correct? And is this retrieval for the
purpose of determining what new value to store into `i'? No,


A perverse interpretation would even say that a = i++ has
undefined behavior, because the prior value of i is accessed to
determine *two* values to be stored into i and a respectively,
without an intervening sequence point.

Tak-Shing

Nov 13 '05 #5

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

Similar topics

2
by: Dave Theese | last post by:
Hello all, I have read the definition of a sequence point in the standard and can follow it mechanically, but for some reason, I'm having a hard time grasping the conceptual meaning and...
3
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: ...
4
by: Timothy Madden | last post by:
Hello I've read a long time ago in the MSDN that C++ language defines no sequence points Now I read in the 1998 ISO standard a small list of sequence points in C++ Does C++ defines sequence...
53
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an...
7
by: akarl | last post by:
Hi all, Why do I get a warning from gcc with the following program? $ cat test.c #include <stdio.h> int f(int n) { return n;
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
1
by: lovecreatesbea... | last post by:
---quoting--- Annex C (informative) Sequence points 1 The following are the sequence points described in 5.1.2.3: - The end of a full expression: an initializer (6.7.8); the expression in an...
4
by: Daniel Kraft | last post by:
Hi all! I do not have a standard-document right next to me to cite from, but as far as I know, doing something like: a()=b()=c()=d(); or foo(d()+c()+b()+a()); has a fixed evaluation order...
3
by: joe | last post by:
Consider the following program: include <iostream> class Bar { public: int getData9() { m_data = 9; return m_data;} int getData11() { m_data = 11; return m_data;} int m_data;
7
by: Jrdman | last post by:
hi According to the standard these are how we define sequence points: *the call to a function ,after the arguments have been evaluated *the end of the first operand of the following...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.