C undefined behaviour

bearophile bearophileHUGS at lycos.com
Sun May 15 06:49:10 PDT 2011


"What Every C Programmer Should Know About Undefined Behavior", #1/3 and #2/3, by Chris Lattner himself (main author of LLVM):

http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
http://www.reddit.com/r/programming/comments/h9rf9/what_every_c_programmer_should_know_about/

http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html
http://www.reddit.com/r/programming/comments/hbepu/what_every_c_programmer_should_know_about/

The third blog post is yet to be shown.

>From my tests the Clang -fcatch-undefined-behavior -ftrapv switches work, they are going to be used for my C code. But I'd like a similar switch for unsigned values too, because currently it doesn't catch situations like:


Using:
clang -fcatch-undefined-behavior -ftrapv


#include <stdio.h>
int main(void) {
    long x = 2000000000;
    unsigned y = 2000000000;
    long z = x + y;
    printf("z = %ld\n", z);
    return 0;
}


Outputs the wrong result still:
z = -294967296

This attitude of C compiler writers (and C language standard writers) is terrible (this also is why I use Python everywhere I can).

Bye,
bearophile


More information about the Digitalmars-d mailing list