为什么 CppCheck 会为此静态 const 数组提供越界访问错误

Why does CppCheck give an array access out of bounds error for this static const array?

本文关键字:越界 访问 错误 数组 const CppCheck 静态 为什么      更新时间:2023-10-16

CppCheck 1.67 已识别并在我的一个项目上越界访问数组错误。我不认为代码是错误的,所以我将代码精简到仍然引发相同错误的最小示例。为什么 CppCheck 对第一个C++示例(在命名空间内)给出以下错误,而对第二个示例(没有命名空间)没有给出以下错误?

我在数组初始化时对命名空间做了什么错误,还是这是 CppCheck 中的错误?

报告错误:"数组'testArray[5]'在索引 5 处访问,这超出了界限。

namespace TestNamespace
{
    class TestClass
    {
        static const int testArray[5];
    };
    const int TestClass::testArray[] = { 1, 2, 3, 4, 5};
}

未报告错误:

class TestClass
{
    static const int testArray[5];
};
const int TestClass::testArray[] = { 1, 2, 3, 4, 5};
似乎是

CppCheck 中的一个错误,可能与跟踪器上的此问题有关:

FP arrayIndexOutOfBounds:在命名空间中声明的类的成员变量。