gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:24:37 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com