gpt4 book ai didi

c++ - VS2010代码分析奇怪的数组限制报错

转载 作者:行者123 更新时间:2023-11-28 03:32:12 25 4
gpt4 key购买 nike

我有一个这样定义的类:

#include <cassert>

class Vector
{
double v[2];

double operator()(int i) const
{
assert(i>=0 && i<2);
return this->v[i];
}
};

运行VS2010代码分析工具时,数组访问报错:

warning C6385: Invalid data: accessing 'this->v', the readable size is '16' bytes, but '-16' bytes might be read

但它对我来说似乎完全有效,因为断言应该防止任何负值。发生了什么事?


编辑:代码分析似乎没有正确处理断言:

assert(i<2)

产生

warning C6385: Invalid data: accessing 'this->v', the readable size is '16' bytes, but '24' bytes might be read

同时

assert(i>=0)

产生

warning C6385: Invalid data: accessing 'this->v', the readable size is '16' bytes, but '-16' bytes might be read

用 ifs 替换断言可以解决问题。

最佳答案

这是一个相当古老的问题,但我想在上面发布我的发现:

用 size_t 替换 int 可以修复它,因为 int 可以 < 0,因此您理所当然地得到分析错误。

要修复它,请将其更改为 size_t 或 unsigned int。

静态分析不考虑断言,这就是 if 有效但断言无效的原因。如果你想取消警告,你可以添加:

_analysis_assume(i>0 && i < MY_MAX_VALUE)

这将告诉分析器 i 需要哪些值。

关于c++ - VS2010代码分析奇怪的数组限制报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12216985/

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