是否可以在 g++ 中启用数组边界检查? [英] Is it possible to enable array bounds checking in g++?

查看:18
本文介绍了是否可以在 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++ 中启用数组边界检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆