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

1

6

CppCheck 1.67 has identified and array accessed out of bounds error on one of my projects. I didn't think the code was wrong, so I have stripped down the code to the bare minimum example that still raises the same error. Why does CppCheck give the following error for the first C++ example (inside a namespace) but not for the second example (without a namespace)?

Am I doing something wrong with the namespace on my array initialisation or is this an error in CppCheck?

Reported error: "Array 'testArray[5]' accessed at index 5, which is out of bounds."

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

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

No reported errors:

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

const int TestClass::testArray[] = { 1, 2, 3, 4, 5};
Narvaez answered 21/11, 2014 at 11:38 Comment(0)
M
4

Seems to be an error in CppCheck, maybe is connected with this issue on the tracker:

FP arrayIndexOutOfBounds: member variable of class declared in namespace.

Mitzi answered 21/11, 2014 at 11:52 Comment(2)
This answers my question, thanks. Will not worry about it any further. I think the bug is closed without a fix though?Narvaez
Indeed. I've re-opened the ticket and provided a link to this question, hopefully they'll fix this.Mitzi

© 2022 - 2024 — McMap. All rights reserved.