【问题标题】:Why does CppCheck give an array access out of bounds error for this static const array?为什么 CppCheck 会为此静态 const 数组提供数组访问越界错误?
【发布时间】:2015-01-19 13:10:45
【问题描述】:

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

我的数组初始化时的命名空间有什么问题,还是 CppCheck 中的错误?

报告错误:“在索引 5 处访问的数组 'testArray[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};

【问题讨论】:

    标签: c++ namespaces indexoutofboundsexception static-analysis cppcheck


    【解决方案1】:

    似乎是CppCheck中的错误,可能与跟踪器上的这个问题有关:

    FP arrayIndexOutOfBounds: member variable of class declared in namespace.

    【讨论】:

    • 这回答了我的问题,谢谢。不会再担心了。我认为这个错误在没有修复的情况下关闭了?
    • 确实如此。我已重新打开票证并提供了此问题的链接,希望他们能解决此问题。
    猜你喜欢
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    相关资源
    最近更新 更多