c++ - 这是什么意思? int foo = foo + 4;

标签 c++

#include <iostream>

int main(int argc, char** args) {
  int foo = foo + 4;
  std::cout << foo << std::endl;
}

还有一个后续问题,是否有一个编译器标志来阻止这种事情?我发现 -Wall 有时会起作用,最好完全阻止它。

My compiler:  
g++ -v  
Using built-in specs.  
Target: i486-linux-gnu  
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu  
Thread model: posix  
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)  

最佳答案

不,没有保证可以阻止这种事情。变量的名称在其初始化程序中可见是 C++ 和 C 的一个组成部分。它允许您执行以下操作:

T *t = malloc(sizeof(*t));

C++ 问题列表中有一份问题报告要求在简单情况下进行诊断,但目前不需要编译器来诊断您的情况。

也是有效的in a different context .

编辑:澄清 - 您的代码段的行为未定义:您正在读取未初始化变量的值。编译器不需要对此进行诊断并不意味着定义了行为。

关于c++ - 这是什么意思? int foo = foo + 4;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5450633/

相关文章:

c++ - 是否在两个不同的 fstream 未定义行为中打开相同的文件?

类成员的 C++ 内存泄漏

c++ - 如何将 Argb32 加载到 Eigen Matrix 中以获得最佳性能?

c++ - 在 shared_from_this() 中 boost weak_ptr_cast

c++ - QObject 真的有多重?

c++ - QList + QVariant + dbus,有什么区别?

python - 为什么 -1/2 在 C++ 中评估为 0,但在 Python 中评估为 -1?

c++ - Qt - QTextStream - 如何将光标位置设置为一行的开头?

c++ - 在 Cocos2d-x 中创建 InAppPurchase

c++ - 如何在 Windows 上使用 Vim 编译 C++ 代码?