我有一个这样定义的类:
#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 替换断言可以解决问题。