Is it possible to enable array bounds checking in g++?(是否可以在 g++ 中启用数组边界检查?)
问题描述
在编译带有一些标志的以下文件时,是否可以让 g++ 显示错误?
#include <iostream>使用命名空间标准;主函数(){int arr[2];cout <我看到了像 gcc -Wall -O2 main.c
这样的东西,它只适用于 C,而不适用于 C++.
解决方案 您可以使用静态分析器,例如 Cppcheck一个>.在上面的代码上运行时:
<上一页>$ cppcheck --enable=all test.cpp检查 test.cpp...[test.cpp:6]:(样式)变量arr"未赋值[test.cpp:8]:(错误)数组 'arr[2]' 索引 4 超出范围
您可以将 Cppcheck 集成到您的构建过程中,并且只有在 Cppcheck 通过时才认为您的代码构建成功.
Is it possible to have g++ show an error when compiling the following file with some flag?
#include <iostream>
using namespace std;
int main()
{
int arr[ 2 ];
cout << arr[ 4 ] << endl;
return 0;
}
I saw some things like gcc -Wall -O2 main.c
which only works with C, not C++.
You can use a static analyser such as Cppcheck. When run on your above code:
$ cppcheck --enable=all test.cpp Checking test.cpp... [test.cpp:6]: (style) Variable 'arr' is not assigned a value [test.cpp:8]: (error) Array 'arr[2]' index 4 out of bounds
You can integrate Cppcheck into your build procedure and consider your code built successfully only if Cppcheck passes.
这篇关于是否可以在 g++ 中启用数组边界检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以在 g++ 中启用数组边界检查?


- 将函数的返回值分配给引用 C++? 2022-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- DoEvents 等效于 C++? 2021-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- 如何提取 __VA_ARGS__? 2022-01-01
- XML Schema 到 C++ 类 2022-01-01
- OpenGL 对象的 RAII 包装器 2021-01-01
- 将 hdc 内容复制到位图 2022-09-04
- GDB 不显示函数名 2022-01-01