473,320 Members | 2,048 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,320 software developers and data experts.

Does this cause undefined behaviour?

Someone was asking in another forum how to translate:
if (x && (y1=(y+y1)/2)) /* all integers with defined values */
to another language.

I am at odds with myself as to whether this causes undefined behaviour

in the first place. Does the subexpression (y1=(y+y1)/2)
cause UB because /y/ is both modified and used without an
intervening sequence point attached to the larger expression
or is it ok because the value of the assignment is the left
hand side is used?

In otherwords, the simple statement:

y = (y+y1)/2;

is well defined assuming reasonable values for y and y1 but when
used in a larger expression as above does it become UB since
/y/ is now both modified and used?

Or, is it ok since /y/ isn't actually used in the larger
expression but the well-defined value assigned to it?

My question would equally apply, I think, to something like...

x = y = (y+y2)/2; /* ok, not ok, or bad analogy? */

Regards, Oz
Nov 14 '05 #1
14 1718
ozbear <oz*****@yahoo.com> scribbled the following:
Someone was asking in another forum how to translate:
if (x && (y1=(y+y1)/2)) /* all integers with defined values */
to another language. I am at odds with myself as to whether this causes undefined behaviour in the first place. Does the subexpression (y1=(y+y1)/2)
cause UB because /y/ is both modified and used without an
intervening sequence point attached to the larger expression
or is it ok because the value of the assignment is the left
hand side is used?
ITYM /y1/, not /y/, there.
In otherwords, the simple statement: y = (y+y1)/2; is well defined assuming reasonable values for y and y1 but when
used in a larger expression as above does it become UB since
/y/ is now both modified and used?
No, I don't think it becomes UB, unless your larger expression
modifies or uses /y1/ (again, ITYM that) in a different place.
Or, is it ok since /y/ isn't actually used in the larger
expression but the well-defined value assigned to it?
Yes, I think that's the case.
My question would equally apply, I think, to something like... x = y = (y+y2)/2; /* ok, not ok, or bad analogy? */


It's a good analogy. That expression is, AFAIK, completely safe.
What would cause UB would be y = y = (y+y2)/2, modifying y twice
without intervening sequence points, but your expression above
doesn't do that.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
Nov 14 '05 #2
oz*****@yahoo.com (ozbear) writes:
Someone was asking in another forum how to translate:
if (x && (y1=(y+y1)/2)) /* all integers with defined values */
to another language.

I am at odds with myself as to whether this causes undefined behaviour
in the first place. Does the subexpression (y1=(y+y1)/2)
cause UB because /y/ is both modified and used without an
intervening sequence point attached to the larger expression
or is it ok because the value of the assignment is the left
hand side is used?
No, this subexpression is well-defined, for the same reason that
`x = x + 1' is well-defined.
In otherwords, the simple statement:

y = (y+y1)/2;

is well defined assuming reasonable values for y and y1 but when
used in a larger expression as above does it become UB since
/y/ is now both modified and used?


Not in this case: y (and y1) aren't mentioned in the larger
expression other than in that subexpression. Furthermore, there
is a sequence point between the left and right side of &&.

--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #3
Joona I Palaste wrote:
ozbear <oz*****@yahoo.com> scribbled the following:
x = y = (y+y2)/2; /* ok, not ok, or bad analogy? */

It's a good analogy. That expression is, AFAIK, completely safe.
What would cause UB would be y = y = (y+y2)/2, modifying y twice
without intervening sequence points, but your expression above
doesn't do that.


Undefined behavior also results from /using/ the value of an object (for
some purpose other than determining its new value) and also modifying it
without an intervening sequence point. But in this case, the object is
only used for determining the new value.

So this is obviously undefined because it modifies i twice:

i = a[i++];

but this is also undefined, even though it does not modify anything
multiple times:

a[i++] = i;

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #4
oz*****@yahoo.com (ozbear) writes:
Does the subexpression (y1=(y+y1)/2) cause UB because /y/ is both
modified and used without an intervening sequence point


It is not necessarily undefined behavior to modify an object and use its
value without intervening sequence point. Essentially, there are two rules
(with respect to the modification of objects) about what is allowed
between two sequence points:

(1) The value of the object may be modified at most once.
(2) If the value of the object is modified at all, the previous value
may be used only to determine the new value.

The standard gives two examples of undefined behavior:

i = ++i + 1; violates (1), because the value of `i' is modified
twice.
a[i++] = i; violates (2). While `i' is modified only once, its
previous value is used to determine its storage
location (as opposed to its new value).

Martin
Nov 14 '05 #5
On Tue, 30 Dec 2003 21:59:29 GMT, oz*****@yahoo.com (ozbear) wrote:
In otherwords, the simple statement:

y = (y+y1)/2;

is well defined assuming reasonable values for y and y1 but when
used in a larger expression as above does it become UB since
/y/ is now both modified and used?


No. The behaviour of an expression which is modifying an object and
using the value of this object without intervening sequence point
would only be undefined if a serial execution of the read and write
accesses to the object could possibly occur either in the order
read(y)->write(y) _or_ in the order write(y)->read(y). In this case
the read access to y _must_ occur before the write access because the
old value of y is needed to determine the new value stored to y.

The behaviour of the expression /*X*/ in

int x,y=0;

x = y + (y=3) /*X*/;

is undefined because read|write could occur in either order. Note that
the reason for undefinedness is not primarily the fact that -
depending on the order of evaluation of the subexpressions - the
result could be either 0+3=3 or 3+3=6 but the bare fact that the read
access to y for <y> might occur before or after the write access to y
for <y=3>.

--
Horst

Nov 14 '05 #6
oz*****@yahoo.com (ozbear) wrote:
My question would equally apply, I think, to something like...

x = y = (y+y2)/2; /* ok, not ok, or bad analogy? */


An additional point: it may seem that this causes undefined behaviour,
because

- a new value is computed for y (involving y, but that part is OK);
- the value of y is then assigned to x, without an intervening
sequence point.

However, this is not what happens. _This_ is:

- a new value is computed for y (involving the old y in an OK manner);
- that same _value_ is also the value of the assignment _expression_,
which is then assigned to x.

IOW, the evaluation of the inner assignment expression does not, in the
abstract machine, read y again; it evaluates its right hand side, and
then _both_ assigns this to y _and_ passes it on to the outer
assignment.

Richard
Nov 14 '05 #7
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
oz*****@yahoo.com (ozbear) wrote:
My question would equally apply, I think, to something like...

x = y = (y+y2)/2; /* ok, not ok, or bad analogy? */
An additional point: it may seem that this causes undefined behaviour,
because

- a new value is computed for y (involving y, but that part is OK);
- the value of y is then assigned to x, without an intervening
sequence point.

However, this is not what happens.


Even if it does, it's not UB according to 6.5#2. It's not forbitten to
modify two distinct objects without intervening sequence point, only to
modify the same object more than once.
_This_ is:

- a new value is computed for y (involving the old y in an OK manner);
- that same _value_ is also the value of the assignment _expression_,
which is then assigned to x.

IOW, the evaluation of the inner assignment expression does not, in the
abstract machine, read y again; it evaluates its right hand side, and
then _both_ assigns this to y _and_ passes it on to the outer
assignment.


Does the standard really guarantee that `y' is only read once in the
abstract machine? (C&V?) Why would it be a problem if the abstract machine
first stored the new value of `y', then read that same value again and
stored it in `x'?

The rules (6.5#2) don't forbit that an object is read an arbitrary number
of times between two sequence points, as long as it is only /modified/
once and only read to determine its new value. For example, I think in
this statement (which has defined behavior)

i = i + i + i + i + i;

the abstract machine would be allowed to read the value of `i' five times.

Martin
Nov 14 '05 #8
Martin Dickopp <ex****************@zero-based.org> scribbled the following:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
oz*****@yahoo.com (ozbear) wrote:
> My question would equally apply, I think, to something like...
>
> x = y = (y+y2)/2; /* ok, not ok, or bad analogy? */
An additional point: it may seem that this causes undefined behaviour,
because

- a new value is computed for y (involving y, but that part is OK);
- the value of y is then assigned to x, without an intervening
sequence point.

However, this is not what happens.

Even if it does, it's not UB according to 6.5#2. It's not forbitten to
modify two distinct objects without intervening sequence point, only to
modify the same object more than once.


And to use the old value for anything other than computing the new
value.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
Nov 14 '05 #9
Martin Dickopp wrote:

(1) The value of the object may be modified at most once.
(2) If the value of the object is modified at all, the previous value
may be used only to determine the new value.

The standard gives two examples of undefined behavior:

i = ++i + 1; violates (1), because the value of `i' is modified
twice.
a[i++] = i; violates (2). While `i' is modified only once, its
previous value is used to determine its storage
location (as opposed to its new value).


What about
a[++i] = i;

The standard states that the previous value can only be *read* to
determine the value to be stored. Isn't it questionable wether or
not the previous value (as in the value just before this statement)
is read twice or once? Maybe we are looking at unspecified behaviour
since it depends on exactly when i is incremented and the order
of evaluation.

--
Thomas.

Nov 14 '05 #10
On Wed, 31 Dec 2003 15:34:22 +0000, Thomas Stegen
<ts*****@cis.strath.ac.uk> wrote in comp.lang.c:
Martin Dickopp wrote:

(1) The value of the object may be modified at most once.
(2) If the value of the object is modified at all, the previous value
may be used only to determine the new value.

The standard gives two examples of undefined behavior:

i = ++i + 1; violates (1), because the value of `i' is modified
twice.
a[i++] = i; violates (2). While `i' is modified only once, its
previous value is used to determine its storage
location (as opposed to its new value).


What about
a[++i] = i;

The standard states that the previous value can only be *read* to
determine the value to be stored. Isn't it questionable wether or
not the previous value (as in the value just before this statement)
is read twice or once? Maybe we are looking at unspecified behaviour
since it depends on exactly when i is incremented and the order
of evaluation.


Nope, absolutely undefined, per Martin's #2 above. The RHS of the
assignment reads the value of i outside of the expression on the LHS
that uses it to compute the new value.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 14 '05 #11
On 31 Dec 2003 12:29:29 +0100, Martin Dickopp
<ex****************@zero-based.org> wrote in comp.lang.c:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
oz*****@yahoo.com (ozbear) wrote:
My question would equally apply, I think, to something like...

x = y = (y+y2)/2; /* ok, not ok, or bad analogy? */


An additional point: it may seem that this causes undefined behaviour,
because

- a new value is computed for y (involving y, but that part is OK);
- the value of y is then assigned to x, without an intervening
sequence point.

However, this is not what happens.


Even if it does, it's not UB according to 6.5#2. It's not forbitten to
modify two distinct objects without intervening sequence point, only to
modify the same object more than once.
_This_ is:

- a new value is computed for y (involving the old y in an OK manner);
- that same _value_ is also the value of the assignment _expression_,
which is then assigned to x.

IOW, the evaluation of the inner assignment expression does not, in the
abstract machine, read y again; it evaluates its right hand side, and
then _both_ assigns this to y _and_ passes it on to the outer
assignment.


Does the standard really guarantee that `y' is only read once in the
abstract machine? (C&V?) Why would it be a problem if the abstract machine
first stored the new value of `y', then read that same value again and
stored it in `x'?


The standard does not guarantee that 'y' is only read once, unless it
is volatile, but paragraph 3 of 6.5.16 states:

"An assignment operator stores a value in the object designated by the
left operand. An assignment expression has the value of the left
operand after the assignment, but is not an lvalue."

Note that what is stored in 'x' is the value yielded by the original
value after conversion (if necessary) to the type of 'y'. The as-if
rule allows the implementation to read 'y' more than once as long as
'y' is not volatile, since the result is the same, but if it does so
must do so after the assignment to produce the correct value.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 14 '05 #12
Jack Klein wrote:
On Wed, 31 Dec 2003 15:34:22 +0000, Thomas Stegen
<ts*****@cis.strath.ac.uk> wrote in comp.lang.c:

Martin Dickopp wrote:
(1) The value of the object may be modified at most once.
(2) If the value of the object is modified at all, the previous value
may be used only to determine the new value.
[snip]What about
a[++i] = i;
[snip]

Nope, absolutely undefined,
I think it is undefined too.
per Martin's #2 above.
But I am not sure this is the reason. Because it is the prior value
that cannot be read for other reasons than determining the new value.
The RHS of the
assignment reads the value of i outside of the expression on the LHS
that uses it to compute the new value.


The value assigned is not necessarily the prior value, is my point.
This value is probably undefined though, since it only needs to be
incremented before the next sequence point.

My point is that the above is undefined, but I am not sure if it is
6.5#2 (C99) that is the clause that causes this.

--
Thomas.

Nov 14 '05 #13
On Wed, 31 Dec 2003 18:35:22 +0000, Thomas Stegen
<ts*****@cis.strath.ac.uk> wrote in comp.lang.c:
Jack Klein wrote:
On Wed, 31 Dec 2003 15:34:22 +0000, Thomas Stegen
<ts*****@cis.strath.ac.uk> wrote in comp.lang.c:

Martin Dickopp wrote:

(1) The value of the object may be modified at most once.
(2) If the value of the object is modified at all, the previous value
may be used only to determine the new value. [snip]What about
a[++i] = i;

[snip]

Nope, absolutely undefined,


I think it is undefined too.
per Martin's #2 above.


But I am not sure this is the reason. Because it is the prior value
that cannot be read for other reasons than determining the new value.
The RHS of the
assignment reads the value of i outside of the expression on the LHS
that uses it to compute the new value.


The value assigned is not necessarily the prior value, is my point.
This value is probably undefined though, since it only needs to be
incremented before the next sequence point.

My point is that the above is undefined, but I am not sure if it is
6.5#2 (C99) that is the clause that causes this.


That's exactly the point of the wording from the standard, this is an
EXACT quote from paragraph 2 of section 6.5 of the C standard:

========
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.
========

Note the second sentence.

Now in the expression under discussion:

a[++i] = i;

The read of the value 'i' on the left hand side is perfectly proper,
it is used in the computation of the new value. The read of the value
of 'i' on the right hand side causes undefined behavior because it is
not involved in the calculation of the value finally assigned to 'i'.

Unless you can contend that the RHS of the expression does not need to
perform an lvalue-to-rvalue conversion, that is read the value of 'i',
and I don't think you are doing so.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 14 '05 #14
Many thanks to all that replied.

Oz

Nov 14 '05 #15

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

Similar topics

29
by: Vol | last post by:
I think 'atan' can get the angle but it is not the four quadrant angle. Is there any function that i can get the angle from -pi to pi? or I have to use some if ... else? I know in Matlab, we use...
7
by: Daniel Rudy | last post by:
Hello, I have a peice of code that I'm making an attempt to code. The problem is that I need to return an arbitrary number of char strings. int function(char *fname, int *dcount, char *data)...
102
by: tom fredriksen | last post by:
Hi I was doing a simple test of the speed of a "maths" operation and when I tested it I found that removing the loop that initialises the data array for the operation caused the whole program to...
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;
48
by: avasilev | last post by:
Hi all, my question is: if i allocate some memory with malloc() and later free it (using free()), is there a possibility that a consequent malloc() will allocate memort at the same starting...
7
by: rsk | last post by:
char *i_reg_fname = "none"; -- Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/ More information at http://www.talkaboutprogramming.com/faq.html
10
by: subramanian100in | last post by:
Consider the following code: #include <iostream> #include <cstdlib> using namespace std; int main() { const double& ref = 100;
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.