【发布时间】:2017-09-26 08:23:19
【问题描述】:
我正在使用 gcc 4.9.0,我希望看到编译器警告我超出数组范围。如果我编译这个
int main()
{
int table[5]={0};
table[8] = 1234;
int x = table[10];
}
使用 g++ -O2 -Wall main.cpp -o main.exe 我只会收到有关未使用 x 的警告:
main.cpp: In function 'int main()':
main.cpp:8:7: warning: unused variable 'x' [-Wunused-variable]
int x = table[10];
^
从 gcc 文档 (https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options) 我看到 -O2 和 -Wall 应该启用 -Warray-bounds=1 检查。如果我尝试添加 -Warray-bounds,情况不会改变。事实上,编译器甚至无法识别 -Warray-bounds=1:
g++: error: unrecognized command line option '-Warray-bounds=1'
现在,为什么编译器不给出关于错误地写入/读取数组的警告?为什么编译器不能识别'-Warray-bounds=1'?
【问题讨论】:
-
无论优化如何,clang 仍然会发出警告。 godbolt.org/g/e6yv89 gcc 不会对此发出警告,无论优化如何:wandbox.org/permlink/gLXfIl7dd87Uwsew
-
请注意,您链接到的文档是最新版本的 gcc,因此它可能与 4.9 的文档不同。
-
您不要将
=1或=0与-W选项一起使用。仅限-Wsome-warning或-Wno-some-warning。
标签: c++ g++ compiler-warnings