我似乎从 Visual Studio 2019(16.5 预览版以及 16.4 及更早版本)代码分析工具中收到了错误的警告消息。这是一个错误,还是我真的只是错过了什么?
生成的警告(确切地说)是:
警告 C6385:从 'prodlist' 读取无效数据:可读大小为 '(size_t)*32+8' 字节,但可以读取 '64' 字节。
这是生成警告的代码(尽可能少)
#include <cstdint>
#include <string>
#include <iostream>
struct Product {
std::string price_profile;
};
int getNumRows() {
return 5;
}
Product *getProductsFromDB( int &numelements ) {
numelements = 0;
const int num_rows = getNumRows();
if ( num_rows == 0 ) {
numelements = 0;
return nullptr;
}
Product *prodlist = new Product[num_rows];
for ( int i = 0; i < num_rows; ++i ) {
prodlist[i].price_profile = "test"; // Warning on this line
}
numelements = num_rows;
return prodlist;
}
int main() {
int num_rows;
Product *prodlist = getProductsFromDB( num_rows );
for ( int i = 0; i < num_rows; ++i ) {
std::cout << prodlist[i].price_profile;
}
getchar();
}
如果我将 更改price_profile
为一个int
(及其相应的值),或者如果我更改num_rows
为一个常量(如5
),那么警告就会消失。