473,657 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "unspecifie d" 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 8185
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 "furthermor e" 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 "furthermor e," 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 "unspecifie d" 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 "unspecifie d" 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.ne rim.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 "unspecifie d" 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 "furthermor e" 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
603
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 significance. Can someone out there offer good intuition on sequence points??? Thank you! Dave
3
2383
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: An expression a = b() + c() * d++; can be transformed with the rules of operator associativity and operator precedence into a tree
4
1661
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 points ? It really should if anyone asks me ... Thank you Timothy Madden
53
4063
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 object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored." Can someone give me examples of expressions that "barely"...
7
2071
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
2502
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 uses errno, modified by right operand): (log(x) - mu) * (log(x) - mu) Code has unspecified behavior. Order of evaluation of function parameters or subexpressions is not defined, so if a value is used and
1
2881
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 expression statement (6.8.3); ... ---quoting ends--- What does a full expression exactly mean?
4
1778
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 (right-to-left in the first case and
3
1680
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
215
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 operators : {logical AND :&&
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.