如果尝试使用功能,则会收到警告 C6385

Getting Warning C6385 if try to use function

如果我尝试在以下代码中使用函数 geti(),我会收到此警告。

Warning C6385   Reading invalid data from 'snapPts':  the readable size is '((geti()+1))*sizeof(DPoint3d)' bytes, but '48' bytes may be read.

但是如果我使用像

这样的整数
int i

直接那么我无法收到警告。我无法理解那里发生的情况,我用谷歌搜索了很多但找不到解决方案。我是c++的新手,请原谅我的拼写错误,请帮助我理解这一点。

注意:我正在使用“Microsoft Mixed(C++/CLR) 推荐规则”构建代码。

我正在使用以下代码

#include <windows.h>  
#include <stdio.h>  
#include <malloc.h>
#include <corecrt_wstring.h>

int geti() {
return 2;
}
struct  DPoint3d
{
//! x coordinate
double x;
//! y coordinate
double y;
//! z coordinate
double z;
};

int main(array<System::String ^> ^args)
{

int i = 2;


if (i > 1) {

    DPoint3d*   snapPts = (DPoint3d *)_alloca((geti() + 1) * sizeof(DPoint3d));

    DPoint3d* snapPts2 = new DPoint3d();

    *snapPts2 = snapPts[1];


}

return 0;
}

非常感谢一个很好的答案。

谢谢

消息来自 Visual Studio 的静态代码分析器,而不是编译器。该分析仪相对较新,但效果不是很好——它显示出很多误报。在你的情况下,它根本没有注意到 geti() 总是 returns 2.

您代码中的真正问题是 *snapPts2 = snapPts[1]; 使用了未初始化的内存。这是因为_alloca从栈中分配内存,但没有初始化