How To Avoid the 5 Most Common C++ Mistakes

June 11th, 2013 Leave a comment
Like the article?
cpp most common mistakes

We’ve all been there. From newbie to old hat, the unexpected compiler error and subsequent hair pulling as we look for the problem that could have caused it is one of the most frustrating things a programmer can experience. Yes, it’s probably even worse than the old “C is better than C++” argument (or “C++ is better than C”, take your pick) that continuously makes the rounds.

If you’ve ever been an instructor teaching people how to program in C++, you’ve no doubt learned what the five most common, frustrating mistakes programmers can make are. If so, here’s a refresher course. If not, here they are. Knowing them will make your life easier.

Mistake № 1 – Undeclared and Uninitialized Variables

Probably the mistake most often made by both noobs and experienced programmers alike, and not just in C++, this one is easy to avoid but often isn’t. That one little "int x;" line can save you a lot of hair loss.

Just as bad are the variables that are left uninitialized. In the same vein, that magic "int x;" line can make or break your code. It’s hard to come to grips with the amount of eyeball-scrubbing time most of us have spent digging through code to find a problem when all that was missing was a variable initialization line to keep it within parameters for a loop. A declared variable in C++ is not an initialized one.

Mistake № 2 – Initializing Too Soon

Often, as programmers, we get caught up in the “final output” thought process. We’re thinking in terms of what the final outcome of the program will be rather than thinking through the code logically. So we do things like initialize variables based on one or two uninitialized variables added together before we do the algorithm that sums them up. For example:

int a, b;
int sum=a+b;

Oops. The variables a and b were initialized correctly, but now have only random values because they have not been defined. So adding them together will just produce another random number. If the point of the program is to work as a simple calculator, it’s going to fail because a and b are unknown (and thus, in C++, random assignments) so the sum of them is also unknown (random). Instead, sum should have been initialized with a and b (e.g. “int a, b, sum;”) and then defined after a and b were defined (e.g. “sum=a+b;”).

This seems like a simple, dumb mistake that wouldn’t happen to a seasoned programmer, but it’s amazing how often it’s done by programmers who otherwise really know their stuff simply because they got in a hurry or thought the code would be “really simple and quick to write.” Hours later.. they’re in the forum asking for help.

Mistake № 3 – Typos

Seems simple enough: you make a typo, you find it when the error occurs and fix it. It’s not hard to spot a variable that’s been misspelled later in the code and remedy the problem. But when it’s something specific to syntax, like that equals (“=”) sign, it can be very hard to spot as you scan the lines to find your problem.

Using the equal as our example, let’s consider a couple of lines of code:

char x='A';
while (x='A')
{
  //...
}

Trust me, if that’s embedded in 300 lines of code where the declaration happened far before the loop, the mistake will be tough to spot. Obviously, this loop will never end as is, since we just told the compiler that x='A' as the definition of our loop. It should read "while (x=='A')".

Similarly, other typos like extra semi-colons, mis-placed brackets and so forth can really jack up otherwise beautiful code. When in a hurry, we often type by instinct rather than from thought process, so we’ll do things like "for (x=0; x<100; x++);". Spot the extra semi-colon yet?

Mistake № 4 – Misusing Common Operators

The && and || are the ones most likely to be abused in C++, but any common operator can be a victim here. Swapping a || for a && is likely one of the most common “I’m in a hurry” mistakes C++ programmers make. If a penny were transferred to my bank account every time an infinite loop was created because of this, I’d be a billionaire. Watch those operators!

Mistake № 5 – Overstepping Arrays

This happens usually to beginners, but can happen to anyone. It’s easy to forget that an array is one size smaller than we defined it to be. So if we initialized an array to have 10 elements, then the final number is 9, since arrays are indexed from 0 to the number of initialized elements – in this case 0 through 9 to make for 10 total. If we then call on element 10 (array[10]) in our code, we’ll get the wrong value (or an error, depending on how it’s called) because array position 10 doesn’t exist, it’s actually number 9 (array[9]).

Honestly, if this one thing were changed retroactively in all C++ code, fully half the hair pulled out while learning to program in the language would regrow.

Conclusion

These five mistakes are easily the most common ones confronting C++ programmers everywhere. Learn to watch for them and your code (and sanity) will improve immediately.

Help us spread the word!
  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Delicious
  • DZone
  • Reddit
  • Sphinn
  • StumbleUpon
  • Google Plus
  • RSS
  • Email
  • Print
Don't miss another post! Receive updates via email!

Comment