Second article about some bugs

bearophile bearophileHUGS at lycos.com
Mon Apr 4 09:51:53 PDT 2011


The second article about common bugs in C/C++ (and probably sometimes D too) code (I have shown the first link here some time ago), it's nice because it shows real world bugs:
http://www.viva64.com/en/a/0072/

The Reddit thread:
http://www.reddit.com/r/programming/comments/gi8i1/how_to_make_fewer_errors_at_the_stage_of_code/

On Reddig one person (LittlemanTAMU) says:
>What's more productive, whining about a language's design choices that cannot now be changed or pointing out common errors with real world examples so the people that have to use the language can write better code?<

My answer is something like: thankfully new languages are being developed, so some troubles/bugs are avoidable.


The article shows probable bugs like:

void main() {
    enum uint BAR = 0b011;
    int foo = 10;
    auto x = !foo & BAR;
}

The comments of their static analysis tool is something like:
>V564 The '&' operator is applied to bool type value. You've probably forgotten to include parentheses or intended to use the '&&' operator.<

-------------------

Another class of bugs shown is of the kind:

int Z = X + (A == B) ? 1 : 2;

Meant to be:
int Z = X + (A == B ? 1 : 2);

But in reality is this, because of the low priority of the ternary operator:
int Z = (X + (A == B)) ? 1 : 2;

Bye,
bearophile


More information about the Digitalmars-d mailing list