c++debugging

Methods for avoiding common typo bugs


So I just spent the last few hours pouring over code trying to figure out the source of a bug only to find that my error was none other than the obviously wrong but compiler accepted:

if (a = b)

where it should have been

if (a == b)

What do you guys do to safeguard against these frustrating errors? What other common "obviously wrong, but compiler won't complain" bugs should I also watch out for?


Solution

  • Most compilers provide options to give warnings in these situations. These are typically called "lint" warnings after the name of an early program to provide them for C source code (in the early days C compilers didn't have them built in, but they mostly do now). You might check to see you're enabling all warnings your compiler provides. If your compiler doesn't provide lint features, look into a good lint tool.