473,587 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1734
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.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
Nov 14 '05 #2
oz*****@yahoo.c om (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[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof 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.c om (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.c om (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.c om (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.c om (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.c om (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.hel sinki.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

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

Similar topics

29
3330
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 'atan2( )' Also, is there any function to get the round value, similar with floor and ceil, such like: round(3.1) = 3 round(3.6) = 4
7
1646
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) Would this work? If so, then how to you load the data into data? Malloc?
102
4498
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 spend twice the time to complete. If the loop is included it takes about 7.48 seconds to complete, but when removed it takes about 11.48 seconds. ...
33
2316
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
5802
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 address and will return the same pointer as the previous malloc(). I would like to have confirmation on whether this is practically a concern when...
7
318
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
1794
by: subramanian100in | last post by:
Consider the following code: #include <iostream> #include <cstdlib> using namespace std; int main() { const double& ref = 100;
32
2708
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 issue. A noddy mixin layer example should illustrate the issue... class Base { protected: int m_Field;
0
8215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8347
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7973
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...
0
8220
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...
1
5718
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...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
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...

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.