I get this warning if I try to use function geti() in the following code.
Warning C6385 Reading invalid data from 'snapPts': the readable size is '((geti()+1))*sizeof(DPoint3d)' bytes, but '48' bytes may be read.
But If I use integer like
int i
directly then I am not able to get the warning. I am unable to understand the scenario that what’s happening there, I googled it much but couldn’t find the solution. I am new to c++, please pardon me for spell mistake and please help me to understand this.
Note: I am building the code with "Microsoft Mixed(C++/CLR) Recommended Rules.
I am using the following code
#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;
}
A great answer is really appreciated.
Thanks
The message comes out of Visual Studio's static code analyzer, not the compiler. The analyzer is relatively new and does not work very well - it shows a lot of false positives. In your case, it simply did not notice that geti()
always returns 2.
The real problem in your code is that *snapPts2 = snapPts[1];
uses uninitialized memory. This is because _alloca
allocates memory from the stack, but does not initialize it.