c++ - Lint 可能访问/创建越界指针

标签 c++ lint

我收到 Lint 警告 661/662。它们表示可能访问/创建越界指针。但是我确实检查了边界,并且不可能越界。任何想法如何摆脱它?

const my_enum_type my_array[] = {MY_FIRST_ENUM_VALUE, MY_SECOND_ENUM_VALUE, MY_THIRD_ENUM_VALUE};  
for(i = 0; i < sizeof(my_array); i++){ 
    //do stuff such as
    my_variable = my_array[i];
    my_function(my_array[i]);
}  

我使用 my_array[i] 的行提示代码 661/662。

最佳答案

sizeof(my_array) 不是您想要的。你想要 sizeof(my_array)/sizeof(*my_array)

你可以使用迭代器:

for(auto it = std::begin(my_array); it != std::end(my_array); ++it){ 
    //do stuff such as
    my_variable = *it;
    my_function(*it);
}

或直接获取范围:

for (const auto& e : my_array){ 
    //do stuff such as
    my_variable = e;
    my_function(e);
}

关于c++ - Lint 可能访问/创建越界指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31581727/

相关文章:

c++ - std::basic_string 是可逆容器吗?

android - Android Studio 3.0-alpha8 中的 Kotlin apply() 扩展 lint 消息

Java:如何使 lint 尊重从内部函数调用抛出的 RuntimeException?

python - 当 docstring 需要一个字符串时,你能传递一个 None 参数吗?

asp.net-mvc - ASP.NET MVC 的 Lint?

c++ - 使用模板函数重载

c++ - 比工厂更好的设计模式?

c++ - static_assert 内部/外部类定义

理解缩进的 php 语法检查器

c++ - 从函数返回指向全局数组的指针