Please examine the following code:
if (foo->bar == NULL);
foo->bar = strdup(\"Unknown\");
I spent the last part of three hours hun
/* foo.c */
int main() {
if (1) ;
return 0;
}
gcc -Wextra -c foo.c
foo.c: In function ‘main’:
foo.c:2: warning: empty body in an if-statement
In addition to the above, if you find yourself getting frustrated hunting for a bug using valgrind or a similar execution profiler, you should perhaps consider using a static analysis tool, such as lint. Personally, I use PC-LINT, which catches all sorts of these types of bugs.
Try -Wextra
As an alternative to the compiler, I find that running an autoindenter over the code helps find these situations.
In vim for example:
gg=G
After digging deep into the gcc manual:
-Wempty-body
Warn if an empty body occurs in an `if', `else' or `do while' statement. This warning is also enabled by
-Wextra.
As some other posters wrote, -Wextra should do it
Sample code:
int main(){
if (0);
printf("launch missiles");
return 0;
}
$gcc -Wempty-body foo.c
warn.c: In function ‘main’:
warn.c:5: warning: suggest braces around empty body in an ‘if’ statement