小编Owe*_*wen的帖子

为什么CppCheck为这个静态const数组提供了一个数组访问超出范围的错误?

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};
}
Run Code Online (Sandbox Code Playgroud)

没有报告错误:

class TestClass
{
    static const int testArray[5];
};

const int TestClass::testArray[] = { 1, 2, 3, 4, 5};
Run Code Online (Sandbox Code Playgroud)

c++ static-analysis namespaces cppcheck indexoutofboundsexception

6
推荐指数
1
解决办法
1429
查看次数