Undefined behaviour in expressions

I

Ioannis Vranos

==> C95:


I read in some text that we get undefined behaviour in expressions when
we modify a variable more than once in the expression. So I wonder if
the following codes have undefined behaviour:


1.

#include <stdio.h>


int main()
{
int x=1;

/* implementation-dependent result? */
int y= x++/x--;

printf("%d\n", y);

return 0;
}



2. I think the following is well defined, since the expression is
evaluated from left to right, and y gets assigned the value 1.

#include <stdio.h>


int main()
{
int x=1;

int y= (x++, x--);

printf("%d\n", y);

return 0;
}
 
D

Dann Corbit

Ioannis Vranos said:
==> C95:


I read in some text that we get undefined behaviour in expressions when
we modify a variable more than once in the expression. So I wonder if
the following codes have undefined behaviour:


1.

#include <stdio.h>


int main()
{
int x=1;

/* implementation-dependent result? */
int y= x++/x--;

Undefined behavior. The variable x is modified more than once without any
intervening sequence point.
printf("%d\n", y);

return 0;
}



2. I think the following is well defined, since the expression is
evaluated from left to right, and y gets assigned the value 1.

#include <stdio.h>


int main()
{
int x=1;

int y= (x++, x--);

The comma is a sequence point. The behavior is well defined.
 
A

Andrey Tarasevich

Ioannis said:
I read in some text that we get undefined behaviour in expressions when
we modify a variable more than once in the expression.

Yes. But:

Firstly, it is not "in the expression". It is "between two adjacent
sequence points". We get undefined behavior if we modify a variable more
than once between two adjacent sequence points.

Secondly, we also get undefined behavior in expressions when we modify a
variable only once, but also read its value for some other purpose
(other than calculating its new value).
So I wonder if
the following codes have undefined behaviour:
...
/* implementation-dependent result? */
int y= x++/x--;
...

Why do you "wonder"? The above rule is clearly violated. SO the behavior
is undefined.
2. I think the following is well defined, since the expression is
evaluated from left to right, and y gets assigned the value 1.
...
int y= (x++, x--);
...

Yes, you are right. Operator ',' introduces a sequence point into the
expression, which "isolates" the modifications from each other. In this
case you have only one modification between each pair of adjacent
sequence points, so everything is OK.
 
F

Flash Gordon

Ioannis Vranos wrote, On 01/04/08 19:59:
==> C95:


I read in some text that we get undefined behaviour in expressions when
we modify a variable more than once in the expression.

You read correctly.
So I wonder if
the following codes have undefined behaviour:

1.

/* implementation-dependent result? */
int y= x++/x--;

Undefined and not necessarily consistent. The compiler could refuse to
compile it or produce anything at all including something that does not
behave consistently.

2. I think the following is well defined, since the expression is
evaluated from left to right, and y gets assigned the value 1.

int y= (x++, x--);

It is not the order of evaluation that is important, but the comma
operator (as opposed to the comma separator in a parameter list)
introduces a sequence point.
 
K

Kaz Kylheku

Undefined behavior.  The variable x is modified more than once without any
intervening sequence point.

How can anyone who is able to find the spacebar key on his keyboard to
separate two consecutive words not have learned a single thing since
first joining the newsgroup in 2000? About a topic that is rehashed on
what seems like a monthly if not weekly basis?

Come on, that's seven years. You can get a master's degree in this
stuff in that time frame.

I can identify about three possible explanations:

1. Troll.

2. Brain disorder (anterograde amnesia or similar).

3. April Fool's.

I'm tending to regard #2 as most plausible.

He will learn all about this stuff in the next little while, but then
forget all about it and ask the same question months or years from
now.

Check this out:

(Message ID: <[email protected]>)

http://groups.google.com/group/comp.lang.c/msg/a9adcaa62b3548ce?dmode=source

That's Ioannis, in a February 2002 posting, indicating that he knows
about i = i++ being undefined! In that thread, he would have learned
that i = func(i++) is well-defined because of the sequence point
immediately before a function call, together with the the data
dependency in the assignment.
 
I

Ioannis Vranos

Kaz said:
How can anyone who is able to find the spacebar key on his keyboard to
separate two consecutive words not have learned a single thing since
first joining the newsgroup in 2000? About a topic that is rehashed on
what seems like a monthly if not weekly basis?


I do not remember when I first joined, but I had left this newsgroup for
a long time. Also I do not know everything. You may have been
concentrating on C in all your time, but myself am learning other stuff
too all the time (including C++).
 
T

Thad Smith

Ioannis said:
2. I think the following is well defined, since the expression is
evaluated from left to right, and y gets assigned the value 1.

#include <stdio.h>


int main()
{
int x=1;

int y= (x++, x--);

printf("%d\n", y);

return 0;
}

It is well defined, but y is assigned 2.
 
N

Nick Keighley

Kaz Kylheku wrote:

I do not remember when I first joined, but I had left this newsgroup for
a long time. Also I do not know everything. You may have been
concentrating on C in all your time, but myself am learning other stuff
too all the time (including C++).

the stuff about sequence points applies to C++ as well
 
I

Ioannis Vranos

Thad said:
It is well defined, but y is assigned 2.


This one puzzled me in the beginning. However eventually I realised that

int y= x--;

and

int y= (x--)

are equivalent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top