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

Undefined behavior

I've read the FAQ, and I believe I know the answer to the below.
However, I've been involved in a rather "spirited" discussion on a
platform-specific mailing list and want to ensure that I'm "right"
before I make a further fool of myself...

int func( int x )
{
return x;
}

int main( int argc, char *argv[] )
{
int len = 5;
int arr[ 10 ];

arr[ len++ ] = func( len ); /* problem line */

return 0;
}

The above code was posted by someone who believed he had a problem
with the compiler, particularly the line with the comment. In previous
usage of that "construct", the argument to the function had always
been 5. Now, though, he's sometimes seeing 6. Which argument value he
sees seems to depend on other (not listed above) code that might be
executed before or after the commented line.

I maintain that the above results in "undefined behavior", since <len>
is both modified and read (apart from retrieving the value to be
stored) between two sequence points.

It has been pointed out on the "other" mailing list that the function
call results in a sequence point. I maintain that this sequence point
is irrelevant, because:
a) it can only "hit" when the operand on the RHS of the '=' operator
is evaluated, and
b) there's no guarantee that the operand on the RHS will be evaluated
before the operand on the LHS.

I believe that, for the purposes of determining when the side-effect
of the postfix operator ("len++") is "complete", the only sequence
point that matters is the semicolon at the end of the statement. And,
since <lenis both modified and read between that sequence point and
the last, this is Undefined Behavior.

Am I right?

-Don
--
remove roman numerals from email
Aug 13 '08 #1
3 1790
On Wed, 13 Aug 2008 13:54:41 -0700, DonStarr wrote:
[...]
arr[ len++ ] = func( len ); /* problem line */
[...]
It has been pointed out on the "other" mailing list that the function
call results in a sequence point. I maintain that this sequence point is
irrelevant, because:
a) it can only "hit" when the operand on the RHS of the '=' operator
is evaluated, and
b) there's no guarantee that the operand on the RHS will be evaluated
before the operand on the LHS.
This is mostly correct.
I believe that, for the purposes of determining when the side-effect of
the postfix operator ("len++") is "complete", the only sequence point
that matters is the semicolon at the end of the statement. And, since
<lenis both modified and read between that sequence point and the
last, this is Undefined Behavior.
There are four relevant sequence points:

1- start
2- function call: func
3- function return: func
4- semicolon

The increment of len may happen either between 1 and 2, or between 3 and
4. If it happens between 1 and 2, then there is a problem. On the other
hand, if it happens between 3 and 4, then all is fine, because there will
have been a sequence point between the read and the store. However, there
is no requirement on implementations to choose one or the other, or to
document how the choice is made, and there is no possibility for a correct
program to detect which choice is made. Because of that, the program
should be modified so that the increment of len is guaranteed to follow
the call to func.
Am I right?
Essentially, yes.
Aug 13 '08 #2
DonStarr wrote, On 13/08/08 21:54:
I've read the FAQ, and I believe I know the answer to the below.
However, I've been involved in a rather "spirited" discussion on a
platform-specific mailing list and want to ensure that I'm "right"
before I make a further fool of myself...
So you decided to see if you could make a fool of yourself here? ;-)
int func( int x )
{
return x;
}

int main( int argc, char *argv[] )
{
int len = 5;
int arr[ 10 ];

arr[ len++ ] = func( len ); /* problem line */

return 0;
}

The above code was posted by someone who believed he had a problem
with the compiler, particularly the line with the comment. In previous
usage of that "construct", the argument to the function had always
been 5. Now, though, he's sometimes seeing 6. Which argument value he
sees seems to depend on other (not listed above) code that might be
executed before or after the commented line.

I maintain that the above results in "undefined behavior", since <len>
is both modified and read (apart from retrieving the value to be
stored) between two sequence points.
You are correct.
It has been pointed out on the "other" mailing list that the function
call results in a sequence point. I maintain that this sequence point
is irrelevant, because:
a) it can only "hit" when the operand on the RHS of the '=' operator
is evaluated, and
It's even narrower than that. It can only be "hit" when the actual
function call occurs which is obviously after the arguments have been
evaluated.
--
Flash Gordon
b) there's no guarantee that the operand on the RHS will be evaluated
before the operand on the LHS.
Correct.
I believe that, for the purposes of determining when the side-effect
of the postfix operator ("len++") is "complete", the only sequence
point that matters is the semicolon at the end of the statement.
Correct.
And,
since <lenis both modified and read between that sequence point and
the last, this is Undefined Behavior.

Am I right?
You are correct.
Aug 13 '08 #3
DonStarr wrote:
I've read the FAQ, and I believe I know the answer to the below.
However, I've been involved in a rather "spirited" discussion on a
platform-specific mailing list and want to ensure that I'm "right"
before I make a further fool of myself...

int func( int x )
{
return x;
}

int main( int argc, char *argv[] )
{
int len = 5;
int arr[ 10 ];

arr[ len++ ] = func( len ); /* problem line */

return 0;
}

The above code was posted by someone who believed he had a problem
with the compiler, particularly the line with the comment.
Just tell him/her to not write such constructs.

arr[len] = func(len);
len++;

or

len++;
arr[len] = func(len);

is the way to go if you want maintainable code.
August
http://en.wikipedia.org/wiki/Command-query_separation
Aug 14 '08 #4

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

Similar topics

48
by: marbac | last post by:
Hi, i heard a lot about "undefined behaviour" in this and other newsgroups dealing with c/c++. Is there a list where all cases with undefined behaviour in C++ are listed? regards marbac
19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
66
by: Mantorok Redgormor | last post by:
#include <stdio.h> struct foo { int example; struct bar *ptr; }; int main(void) { struct foo baz; baz.ptr = NULL; /* Undefined behavior? */ return 0;
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...
30
by: jimjim | last post by:
Hello, #include <stdio.h> int main(int argc, char *argv) { int x = 1; printf("%d %d %d\n", ++x, x, x++); return 0; }
33
by: dragoncoder | last post by:
Hi all, Does the following code invoke undefined behaviour ? $ cat a1.cc #include <iostream> #include <limits> int main() { int a = INT_MAX/2;
14
by: avsharath | last post by:
In "Bjarne Stroustrup's C++ Style and Technique FAQ" at: http://www.research.att.com/~bs/bs_faq2.html#evaluation-order for the statement: f(v,i++); he says that "the result is undefined...
12
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the...
22
by: blargg | last post by:
Does ~0 yield undefined behavior? C++03 section 5 paragraph 5 seems to suggest so: The description of unary ~ (C++03 section 5.3.1 paragraph 8): But perhaps "one's complement" means the...
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
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
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.