> 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
On Wed, Apr 20, 2011 at 7:50 AM, John Regehr wrote:>> 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.http://www.catb.org/jargon/html/N/nasal-demons.html Csaba -- GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++ The Tao of math: The numbers you can count are not the real numbers. Life is complex, with real and imaginary parts. "Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds "People disagree with me. I just ignore them." -- Linus Torvalds
On Wed, Apr 20, 2011 at 7:50 AM, John Regehr <regehr at cs.utah.edu> wrote:>> 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 >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 :-) 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.
On Wed, Apr 20, 2011 at 10:44 AM, Joe Armstrong wrote:> > 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 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 -- GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++ The Tao of math: The numbers you can count are not the real numbers. Life is complex, with real and imaginary parts. "Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds "People disagree with me. I just ignore them." -- Linus Torvalds
On 04/19/2011 10:50 PM, John Regehr wrote:> The compiler is perfectly within its rights to send a rude email to your > department chair if you compile that code.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