Skip to content

Instantly share code, notes, and snippets.

@kwatch
Created February 16, 2016 04:19
Show Gist options
  • Save kwatch/c9a43965182e44bc32ac to your computer and use it in GitHub Desktop.
Save kwatch/c9a43965182e44bc32ac to your computer and use it in GitHub Desktop.
Local variable is not initialized in C++
#include <iostream>
int fn() {
int x;
return x; // returns uninitialized local variable
}
int main(int argc, char*args[]) {
//std::cout << "Hello\n";
std::cout << fn() << "\n";
std::cout << fn() << "\n";
}
/****
$ g++ ex1.cpp && ./a.out
0
32767 <-- !!!!!!!
$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix
****/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment