Undefined behaviour in C [was Re: The Cost of Dynamism]

Paul Rubin no.email at nospam.invalid
Sun Mar 27 02:13:21 EDT 2016


Steven D'Aprano <steve at pearwood.info> writes:
> For example, would you consider that this isolated C code is
> "meaningless"?
> int i = n + 1;

It's meaningful as long as n is in a certain range of values so there's
no overflow.

> But according to the standard, it's "meaningless", since it might
> overflow, and signed int overflow is Undefined Behaviour.

No it's not meaningless if it "might" overflow, it's meaningless if it
-does- overflow, so the compiler can do whatever it wants in that case.
The compiler is only obliged to do anything specific in the case where
there is no overflow, so if it can get some speedups by only generating
code for the non-overflow case, it does so:

http://kristerw.blogspot.com/2016/02/how-undefined-signed-overflow-enables.html

> Compilers are well known for only doing what you tell them to do, not what
> you want them to do. But in the case of C and C++ they don't even do what
> you tell them to do.

They do what you tell them, not what you meant to tell them or what you
thought you were telling them.  That goes way back before compilers and
computers.  Think of all the stories about a guy rubbing lamps and
getting three wishes.  Srsly though, the conclusion is that C and C++
are terrible languages if you want to code anything really solid, not
that the compilers are doing anything bad.  Unfortunately there's a
mountain of legacy code full of such errors, and still a lot of C
programmers out there who don't understand the issue.



More information about the Python-list mailing list