Skip to content

Instantly share code, notes, and snippets.

@rpearl
Created June 11, 2012 13:04
Show Gist options
  • Save rpearl/2910012 to your computer and use it in GitHub Desktop.
Save rpearl/2910012 to your computer and use it in GitHub Desktop.
Uninitialized variables? Who needs warnings!
#include <stdio.h>
void foo(int bar);
void foo(int bar) {
int lol;
if (bar) {
lol = 7;
}
if (lol == 7) {
printf("haha, owned.\n");
}
}
int main(int argc, char *argv[])
{
(void)argv; /* silence. */
foo(argc < 2);
return 0;
}
/*
[16:08]rpearl@sihnon:~$ gcc --version
gcc (GCC) 4.7.0
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[16:08]rpearl@sihnon:~$ gcc -O3 -Wall -Wextra -pedantic uninit.c -o uninit
[16:08]rpearl@sihnon:~$ ./uninit
haha, owned.
[16:08]rpearl@sihnon:~$ ./uninit 1 2 3 4 5 6 7 8
haha, owned.
[16:08]rpearl@sihnon:~$ #However, clang has more sophisticated warnings.
[10:40]rpearl@sihnon:~$ clang --version
Apple clang version 3.1 (tags/Apple/clang-318.0.58) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin10.8.0
Thread model: posix
[10:40]rpearl@sihnon:~$ clang -Weverything uninit.c -o uninit
uninit.c:12:6: warning: variable 'lol' may be uninitialized when used here
[-Wconditional-uninitialized]
if (lol == 7) {
^~~
uninit.c:6:9: note: initialize the variable 'lol' to silence this warning
int lol;
^
= 0
1 warning generated.
[10:40]rpearl@sihnon:~$ #Passing -Wall -Wextra to clang makes it gcc-compatible.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment