473,480 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is the following expression undefined behavior ?

I don't know if the following expression is UB:

i=2;
x = (i=3) * i;

Since in C, evaluation order is unspecified, this expression is 'at
least' unspecified, since we don't know which operand evalutes first,
(i=3) or i, So, would x be always 6 or 9 ? depending on the evaluation
order.

TIA

Feb 9 '07 #1
12 1587
In article <11**********************@m58g2000cwm.googlegroups .com>,
Neroku <n3****@gmail.comwrote:
>I don't know if the following expression is UB:
>i=2;
x = (i=3) * i;
>Since in C, evaluation order is unspecified, this expression is 'at
least' unspecified, since we don't know which operand evalutes first,
(i=3) or i, So, would x be always 6 or 9 ? depending on the evaluation
order.
The relevant wording in C89 (ANSI X3.159-1989) is,

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. [34]

with footnote:

[34] This paragraph renders undefined statement expressions such as
i = ++i + 1;
while allowing
i = i + 1;
This wording occurs in a main heading for the description of the
operators, and in my interpretation must be treated as equivilent
as a constraint. But we can answer the question more directly without
resorting to interpretations: notice that the footnote specifically
says that "renders undefined", so the behaviour is "undefined",
not merely "unspecified".
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Feb 9 '07 #2
Neroku wrote:
>
I don't know if the following expression is UB:

i=2;
x = (i=3) * i;

Since in C, evaluation order is unspecified, this expression is 'at
least' unspecified, since we don't know which operand evalutes first,
(i=3) or i, So, would x be always 6 or 9 ? depending on the evaluation
order.
While most implementations will probably give you one of those
values, the definition of UB means that you can't guarantee it.

Consider a platform capable of parallel operations, and the code
generated includes these two operations to be carried out in
parallel:

stor 3,i ; store 3 in i
mult 3,i,a1 ; multiply i by 3, return in register a1

This could generate a hardware fault, as &i is accessed for both
read and write at the same time.
While most UB examples include modifying an item twice between
sequence points, as in:

i = i++;
or
x = i++ + ++i;

I believe that the UB in question is really "modified, and accessed
for some purpose other than determining the value to modify" (or
similar phrasing), so the following is UB as well:

x = i++ + i;
If I'm wrong (though I don't believe that I am), I'm sure someone
will correct me shortly. :-)

--
+-------------------------+--------------------+-----------------------+
| 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>

Feb 9 '07 #3
Neroku wrote, On 09/02/07 17:51:
I don't know if the following expression is UB:

i=2;
x = (i=3) * i;

Since in C, evaluation order is unspecified, this expression is 'at
least' unspecified, since we don't know which operand evalutes first,
(i=3) or i, So, would x be always 6 or 9 ? depending on the evaluation
order.
Evaluation order is not the problem here. The problem is that you read
"i" for a reason other than determining its new value. So it is
undefined behaviour and you could get any value or a crash or wreck the
process due to a bus clash when it tries to simultaneously read and
write "i".
--
Flash Gordon
Feb 9 '07 #4
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11**********************@m58g2000cwm.googlegroups .com>,
Neroku <n3****@gmail.comwrote:
>>I don't know if the following expression is UB:
>>i=2;
x = (i=3) * i;
>>Since in C, evaluation order is unspecified, this expression is 'at
least' unspecified, since we don't know which operand evalutes first,
(i=3) or i, So, would x be always 6 or 9 ? depending on the evaluation
order.

The relevant wording in C89 (ANSI X3.159-1989) is,

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. [34]

with footnote:

[34] This paragraph renders undefined statement expressions such as
i = ++i + 1;
while allowing
i = i + 1;
This wording occurs in a main heading for the description of the
operators, and in my interpretation must be treated as equivilent
as a constraint. But we can answer the question more directly without
resorting to interpretations: notice that the footnote specifically
says that "renders undefined", so the behaviour is "undefined",
not merely "unspecified".
No, it's not a constraint, since it's not marked as one. The standard
says:

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

which applies in this case.

Constraint violations must be diagnosed at compile time, which is not
possible in general for this requirement. Rather than this:
x = (i = 3) * i;
consider this:
x = (*p1 = 3) * *p2;
It's not possible to determine at compile time whether *p1 and *p2 are
the same object. If they aren't, there's nothing wrong with the
statement.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 10 '07 #5

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
Constraint violations must be diagnosed at compile time, which is not
possible in general for this requirement. Rather than this:
x = (i = 3) * i;
consider this:
x = (*p1 = 3) * *p2;
well in general its not possible indeed, but can the compiler give an error
when it knows for certain it's the same object?
or warning at most?

Feb 10 '07 #6
Serve Laurijssen wrote:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
Constraint violations must be diagnosed at compile time, which is not
possible in general for this requirement. Rather than this:
x = (i = 3) * i;
consider this:
x = (*p1 = 3) * *p2;

well in general its not possible indeed, but can the compiler give an error
when it knows for certain it's the same object?
or warning at most?
A compiler in conforming mode may not refuse to compile it, unless it
can prove that the code will always be executed. And naturally, a
compiler may additionally support non-conforming modes in which such
code does cause a hard error.

Feb 10 '07 #7
Harald van Dijk wrote, On 10/02/07 16:16:
Serve Laurijssen wrote:
>"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>>ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
Constraint violations must be diagnosed at compile time, which is not
possible in general for this requirement. Rather than this:
x = (i = 3) * i;
consider this:
x = (*p1 = 3) * *p2;
well in general its not possible indeed, but can the compiler give an error
when it knows for certain it's the same object?
or warning at most?

A compiler in conforming mode may not refuse to compile it, unless it
can prove that the code will always be executed. And naturally, a
compiler may additionally support non-conforming modes in which such
code does cause a hard error.
The compiler is allowed to produce a warning for it, since compilers are
allowed to warn anything they want.
--
Flash Gordon
Feb 10 '07 #8
Flash Gordon wrote:
Harald van Dijk wrote, On 10/02/07 16:16:
Serve Laurijssen wrote:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
Constraint violations must be diagnosed at compile time, which is not
possible in general for this requirement. Rather than this:
x = (i = 3) * i;
consider this:
x = (*p1 = 3) * *p2;
well in general its not possible indeed, but can the compiler give an error
when it knows for certain it's the same object?
or warning at most?
A compiler in conforming mode may not refuse to compile it, unless it
can prove that the code will always be executed. And naturally, a
compiler may additionally support non-conforming modes in which such
code does cause a hard error.

The compiler is allowed to produce a warning for it, since compilers are
allowed to warn anything they want.
Right, so long as the warning does not cause the compilation to fail
in any of the compiler's modes that are meant to be conforming. (A
warning is not necessarily a non-fatal diagnostic, though admittedly
the only cases of fatal warnings in real-world compilers I've seen
were the result of compiler bugs.)

Feb 10 '07 #9
"Serve Laurijssen" <se*@n.tkwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
Constraint violations must be diagnosed at compile time, which is not
possible in general for this requirement. Rather than this:
x = (i = 3) * i;
consider this:
x = (*p1 = 3) * *p2;

well in general its not possible indeed, but can the compiler give an error
when it knows for certain it's the same object?
or warning at most?
Of course, the compiler can give a warning for anything it likes, and
it can give a fatal error if it can prove that it will always invoke
undefined behavior (not, for example, if the statement is enclosed in
"if (0) { ... }". But since it's not a constraint violation, it's not
required to do so, even when it happens to be easy to figure it out at
compile time.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 11 '07 #10
Harald van Dijk wrote:
[regarding undefined behavior for x = (i = 3) * i;]
>
A compiler in conforming mode may not refuse to compile it, unless it
can prove that the code will always be executed. And naturally, a
compiler may additionally support non-conforming modes in which such
code does cause a hard error.
A compiler may additionally support non-conforming modes which define
the behavior for such constructs.

--
Thad
Feb 12 '07 #11
Thad Smith wrote:
Harald van Dijk wrote:
[regarding undefined behavior for x = (i = 3) * i;]
>>
A compiler in conforming mode may not refuse to compile it, unless it
can prove that the code will always be executed. And naturally, a
compiler may additionally support non-conforming modes in which such
code does cause a hard error.

A compiler may additionally support non-conforming modes which define
the behavior for such constructs.
Or a conforming mode which defines the behavior for such constructs.

--
Thad
Feb 12 '07 #12
Thad Smith wrote, On 12/02/07 05:48:
Harald van Dijk wrote:
[regarding undefined behavior for x = (i = 3) * i;]
>>
A compiler in conforming mode may not refuse to compile it, unless it
can prove that the code will always be executed. And naturally, a
compiler may additionally support non-conforming modes in which such
code does cause a hard error.

A compiler may additionally support non-conforming modes which define
the behavior for such constructs.
Defining behaviour for such constructs would not make it non-conforming.
Implementations are allowed to define what the C standard leave undefined.
--
Flash Gordon
Feb 12 '07 #13

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

Similar topics

19
2552
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
2992
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
3049
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
22690
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; }
30
2101
by: Kiuhnm | last post by:
#include <new> class T { }; int main() { T t = t; T u(u);
33
2294
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;
21
2369
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the...
15
5186
by: Dan Henry | last post by:
I have run across functions in the Linux kernel's MTD driver that have me scratching my head a bit. The functions have the general form: extern int bar(size_t len, size_t *retlen, unsigned char...
18
7920
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
0
7046
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
7048
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
7088
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...
1
6741
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...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4783
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...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.