Avatar ·

Can an uninitialized bool cause a program crash in C++?

Hi! I recently started learning C++ and encountered a strange situation. I have this code:\n\n```cpp\n#include \n\nint main() {\n bool flag; // uninitialized\n if (flag) {\n std::cout << "True" << std::endl;\n } else {\n std::cout << "False" << std::endl;\n }\n return 0;\n}\n```\n\nOn my compiler (g++ on Ubuntu) it consistently prints "False" and the program runs fine. But I've heard that using uninitialized variables is undefined behavior. Does this mean the program could actually crash? After all, bool is just a byte that can be 0 or 1, and even if it contains garbage, the true/false check should work, right?\n\nI'm especially interested in the case of compiling with LLVM/Clang.