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

undefined behaviour a[i] = ++i;

Consider

a[i] = ++i;

This will invoke undefined behaviour because the order of evaluation
of operands of an operator is not specified. Is the reasoning
correct ?

Thanks

Mar 16 '07 #1
4 1389
su**************@yahoo.com, India said:
Consider

a[i] = ++i;

This will invoke undefined behaviour because the order of evaluation
of operands of an operator is not specified.
I suppose that, if the order of evaluation were *completely* defined,
the code would be okay, and it isn't so it isn't, and therefore, in a
way, you are right. But if you want the Chapter and Verse, it goes like
this:

"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." (That's from 3.3.)

So the code violates a "shall" requirement that, as it happens, is not
part of a constraint. And within the definition of "undefined behavior"
in 1.6, we find:

"If a ``shall'' or ``shall not'' requirement that appears outside of a
constraint is violated, the behavior is undefined."

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 16 '07 #2
Richard Heathfield wrote:
>
su**************@yahoo.com, India said:
Consider

a[i] = ++i;

This will invoke undefined behaviour because the order of evaluation
of operands of an operator is not specified.

I suppose that, if the order of evaluation were *completely* defined,
the code would be okay, and it isn't so it isn't, and therefore, in a
way, you are right. But if you want the Chapter and Verse, it goes like
this:
[...]

I don't think this is strictly an "order of evaluation" issue.
Consider:

int i = 2;
int j = i++ + i++;

Strictly speaking, "order of evaluation" would be irrelevent, as
2+3 and 3+2 are both 5, so it doesn't matter which i++ is "evaluated"
first. Unless, by "evaluation" you also mean "all side-effects are
completed as well" -- but that's what "sequence points" are for, as
in your C&V quote.

--
+-------------------------+--------------------+-----------------------+
| 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>
Mar 16 '07 #3
Kenneth Brody said:

<snip>
I don't think this is strictly an "order of evaluation" issue.
Consider:

int i = 2;
int j = i++ + i++;

Strictly speaking, "order of evaluation" would be irrelevent, as
2+3 and 3+2 are both 5, so it doesn't matter which i++ is "evaluated"
first. Unless, by "evaluation" you also mean "all side-effects are
completed as well" --
Well, it would be more accurate simply to admit that I didn't choose my
words, or indeed my thoughts, quite as thoroughly as I should have
done. Thanks for your clarification, which is of course perfectly
correct.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 16 '07 #4
On Mar 16, 5:04 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Consider

a[i] = ++i;

This will invoke undefined behaviour because the order of evaluation
of operands of an operator is not specified. Is the reasoning
correct ?
It is undefined behavior in the C language because the C Standard has
a rule that makes it explicitely undefined.

If you didn't know that rule, but knew that order of evaluation of
operands is not specified in C, then common sense would tell you that
the expression is at least suspicious; if you started with i = 2 then
a [2] = 3 and a [3] = 3 would be two obvious possibilities. In a
language like Java, the behavior is actually defined; in such a
language the expression is just plain horrible.

But something like

a [i] = (i += 0);

also has undefined behavior. And here the only reason is: It is
undefined behavior because the C Standard says so.

Mar 17 '07 #5

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

Similar topics

6
by: Simon Bailey | last post by:
In the following code at the end of the program z = 20 & y = 99. void doit(const int* x) { int* nonconst; nonconst = const_cast<int*>(x); *nonconst = 99; } int main(int argc, char* argv)
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
8
by: Joona I Palaste | last post by:
We all know that this: void *p; if (p=malloc(1)) { free(p); p; } causes undefined behaviour if malloc() succeeds. But what about this?
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
12
by: RoSsIaCrIiLoIA | last post by:
On Mon, 07 Feb 2005 21:28:30 GMT, Keith Thompson <kst-u@mib.org> wrote: >"Romeo Colacitti" <wwromeo@gmail.com> writes: >> Chris Torek wrote: >>> In article <4205BD5C.6DC8@mindspring.com> >>>...
26
by: Frederick Gotham | last post by:
I have a general idea of the different kinds of behaviour described by the C Standard, such as: (1) Well-defined behaviour: int a = 2, b = 3; int c = a + b; (Jist: The code will work...
12
by: Franz Hose | last post by:
the following program, when compiled with gcc and '-std=c99', gcc says test.c:6: error: jump into scope of identifier with variably modified type that is, it does not even compile. ...
10
by: subramanian100in | last post by:
Consider the following code: #include <iostream> #include <cstdlib> using namespace std; int main() { const double& ref = 100;
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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...
0
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...

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.