Is this a bug in clang?

This code is undefined, meaning that all bets are off, don't do it.
I.e. It reads the value of I between two sequence points and uses it
for something other than determining the value written. From: Csaba
Raduly

So... Are 40 and 41 the only legal behaviors or are there more?

Robby

So... Are 40 and 41 the only legal behaviors or are there more?

Since the program invokes undefined behavior, anything goes.

The compiler is perfectly within its rights to send a rude email to your department chair if you compile that code.

John

You should search "Nasal daemons" in Google.

http://www.catb.org/jargon/html/N/nasal-demons.html

Csaba

Ummmm

The problem is I can't represent undefined in C.

I guess I just let (i + i++) and (i++ + i) both evaluate to 42. Then +
become commutative :slight_smile:

It seems very strange to me that the ansi standard says "XXX is
undefined" and that both clang and gcc
can detect that something is undefined and that by default they
compile the offending code without
any feelings of guilt.

My compiler will vigorously refuse to compile such nonsense - there
will be no flags to change this
behavior.

/Joe

My compiler will vigorously refuse to compile such nonsense - there
will be no flags to change this
behavior.

[ Forgot to Reply-All first time, sorry. ]

But how would it deal with:

*p + *q++;

It can't know in general whether *p and *q denote the same object.

"The good thing — the only good thing! — about undefined behavior in
C/C++ is that it simplifies the compiler’s job..."

http://blog.regehr.org/archives/213

I don't think "clang and gcc can detect that something is undefined".
It's more likely that undefined behavior is lumped together with valid
C code. The machine code generated for valid C code is required to
behave correctly. Code generated for undefined behavior "works" on a
best-effort basis.

Csaba

[ Forgot to Reply-All first time, sorry. ]

But how would it deal with:

*p + *q++;

It can't know in general whether *p and *q denote the same object.

Shouldn't that be *p + (*q)++ ?

It seems very strange to me that the ansi standard says "XXX is
undefined" and that both clang and gcc
can detect that something is undefined and that by default they
compile the offending code without
any feelings of guilt.

"The good thing — the only good thing! — about undefined behavior in
C/C++ is that it simplifies the compiler’s job..."

http://blog.regehr.org/archives/213

Excellent article - thanks

I don't think "clang and gcc can detect that something is undefined".
It's more likely that undefined behavior is lumped together with valid
C code. The machine code generated for valid C code is required to
behave correctly. Code generated for undefined behavior "works" on a
best-effort basis.

I still think that in the cases where clang/gcc can detect undefined behavior
they should absolutely refuse to compile the programs. i + i++ is undefined
value, both clang an gcc compile this without any moral qualms - no warnings -
nothing - complete psychopathic behavior :slight_smile:

/joe

Yes, sorry.

At least GCC 4.4.5 does generate a warning here.

Joerg

Wouldn't it technically be only within its rights to compile code that sends the rude email when executed, but not send the email itself? Or are the undefined results of compilation not confined to the contents of the executable file?

Dustin, thinking the world would be a more interesting place if compiler
         writers actually produced such compilers