On C/C++ undefined behaviours

bearophile bearophileHUGS at lycos.com
Fri Aug 20 09:38:40 PDT 2010


Three good blog posts about undefined behaviour in C and C++:
http://blog.regehr.org/archives/213
http://blog.regehr.org/archives/226
http://blog.regehr.org/archives/232

In those posts (and elsewhere) the expert author gives several good bites to the ass of most compiler writers.

Among other things in those three posts he talks about two programs as:

import std.c.stdio: printf;
void main() {
    printf("%d\n", -int.min);
}

import std.stdio: writeln;
void main() {
    enum int N = (1L).sizeof * 8;
    auto max = (1L << (N - 1)) - 1;
    writeln(max);
}

I believe that D can't be considered a step forward in system language programming until it gives a much more serious consideration for integer-related overflows (and integer-related undefined behaviour).

The good thing is that Java is a living example that even if you remove most integer-related undefined behaviours your Java code is still able to run as fast as C and sometimes faster (on normal desktops).

Bye,
bearophile


More information about the Digitalmars-d mailing list