share
Stack OverflowMost common one-line bugs in C?
[+64] [0] thkala
[2011-03-25 21:33:33]
[ c teaching bugs ]
[ http://stackoverflow.com/questions/5438347] [DELETED]

I am co-teaching a practical session on C for a group of first-year CS students that have just started programming.

It seems that they often respond quite well to a carrot and stick [1] approach, where you point out common mistakes made by new programmers and then offer a solution - or threaten to poke their eyes out if they repeat them (just joking!).

In order to have more examples at my disposal, I have started collecting a list of common snippets of one to three lines that do not behave as an inexperienced programmer would expect. Explaining what really happens seems to give a better understanding of the language, while at the same time saving us from helping the students with the same little mistakes over and over again.

For example, I mentioned the all-time classic assignment-as-condition land-mine [2]:

if (a = b) ...

as well as the more sinister undefined-evaluation-order issue:

func(a, a++);

which always tends to appear when a bunch of new programmers are in their "Wow, look how short this code is!" phase, while still being unaware of little details such as sequence points [3].

What are the most common bugs of this kind that you have encountered in C code?

I am interested in little code snippets that contain practices dangerous for a new programmer or where the bug is obvious to more experienced eyes - not logical errors that should be placed within a significantly larger context.

Especially appreciated would be any references (e.g. mailing-list archive links) to actual bugs of this kind that have been found in open source projects.

@thkala: I thought func(a, a++) works the same way no matter what the argument evaluation order is? - Mehrdad
(17) Very relevant to SO: thinking C/C++ is a programming language! - pmg
(1) +1 @pmg: - But it worked at home! -You did change the file extension to .c, right?. Or, possibly even worse, compiling C code with a C++ compiler and then claiming to know C++. - thkala
(3) @Mehrdad, there is no sequence point between function arguments, so you can't know whether the side effect of a++ takes place before or after the first a is evaluated. This is an example of Undefined Behavior. - RBerteig
(7) Many of the answers in this thread are invalid if you just turn up your compiler warning level sufficiently. Turn them up, turn warnings into errors and you turn runtime problems into compile-time problems. - Dustin
@Rberteig: I always thought the side effect happens after the statement is executed, not after the expression is evaluated... huh. - Mehrdad
@Mehrdad: int a = 1; printf("%i %i\n", a, a++); gives 2 1 on both my Linux/GCC and my Windows/VS2010 development systems... - thkala
@thkala: Yeah... it's definitely a subtle bug, glad I learned it. :) - Mehrdad
(1) These look set to multiply just like "Hidden features" and other cancerous growth. I say we dust off and nuke them from orbit. It's the only way to be sure. - dmckee
(3) @Dustin That's an excellent point, but I'd hazard a guess that most people that know to turn up the warnings dial to 11 also know about these pitfalls. - David Heffernan
@dmckee: I honestly did not expect this kind of response - nor did I envision opening Pandora's box (sorry!). Since I rarely make any of the mistakes below anymore, I had kinda forgotten how many of them there are. I was also hoping that my encouraging the presence of links to occurences in a public project would keep things in check a bit. Apparently not... - thkala
@David: I think the point @Dustin was making is that the first order of business should be to instruct new programmers to crank that very warning dial up to 11. - Cody Gray