我收到 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/