RSS | technovelty home | page of ian | ian@wienand.org
What is wrong with the following code
#include <stdio.h>
int main(void)
{
char **blah;
char *astring = "hello, world\n";
*blah = astring;
printf("%s\n", *blah);
return 0;
}
Fairly obvious when it is layed out like this; that *blah =
astring should be blah = &astring (this might be
less obvious when it is buried deep within several functions :).
blah is uninitalised, so you can't dereference it.
Unfortunatley, this code will compile with -Wall with
no warnings. This is because of a little fact
-Wuninitialized
Warn if an automatic variable is used without first being
initialized or if a variable may be clobbered by a "setjmp"
call.
These warnings are possible only in optimizing compilation,
because they require data flow information that is computed
only when optimizing. If you don't specify -O, you simply
won't get these warnings.
So always turn on at least -O to get the full checking gcc can give you, and you'll probably catch things like the above before they even segfault.
posted at: Fri, 18 Feb 2005 12:03 | in /code/c | permalink | add comment (0 others)

This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.