

Eight C++ programming mistakes the compiler won’t catch - sonic0002
http://pixelstech.net/article/index.php?id=1333893320

======
danieldk
Three notes:

1\. The SafeInt class helps detecting integer overflows:

<http://safeint.codeplex.com/>

Also, most SafeInt binary operators do not allow for mixing of SafeInts for
different integer types.

2\. Valgrind helps with detecting use of uninitialized memory.

3\. A (rather ugly) solution to prevent accidental use of = rather than ==
when a constant expression is used, is to make the constant expression the
first operand. E.g.:

    
    
        if (0 = x) // fail!

~~~
pagekalisedown
3: I've rarely seen "Yoda conditions" [1] in the wild. I think it's because
most compilers will output a warning when doing an assignment inside an if
statement.

[1] [http://www.dodgycoder.net/2011/11/yoda-conditions-pokemon-
ex...](http://www.dodgycoder.net/2011/11/yoda-conditions-pokemon-
exception.html)

------
CountHackulus
This depends entirely on which compiler you're using. Saying that these are
mistakes "the compiler" won't catch is disingenuous. While VSC++2005 didn't
catch these, I know that xlC on AIX will catch all of these except the integer
divide "bug" at the default warning level.

------
dlsym
Every language has its quirks... You get used to it; learn to avoid beginners
mistakes; and eventually can use the language in production.

C++ is not different - it can even provide you with even more interesting and
unexpected ways to shoot yourself in the foot:

    
    
        C++ (from: http://m5p.com/~pravn/foot.html)
    
        You accidentally create a dozen instances of yourself
        and shoot them all in the foot.
        Providing emergency medical care is impossible since
        you can't tell which are bitwise copies and which are 
        just pointing at others and saying, "That's me over there."

------
Tatyanazaxarova
For find such errors, and many others, I recommend using static code analyzer
tools. I suggest to try PVS-Studio :-) <http://www.viva64.com/en/pvs-studio/>

