[英]Cannot solve warning C6386
Please guys I need some help.请各位大侠帮帮忙。 I get Warning C6386 Buffer overrun while writing to 'AnArray': the writable size is 'nrows*8' bytes, but '16' bytes might be written.
我在写入“AnArray”时收到警告 C6386 缓冲区溢出:可写大小为“nrows*8”字节,但可能会写入“16”字节。 on the following code
在下面的代码
#include <math.h>
void SubMain(int, int);
int CSTEBit(int, int, int);
double Fact(int);
double Perm(int, int);
int Comb(int, int);
int main()
{
SubMain(13, 5);
}
void SubMain(int N, int R)
{
int** AnArray;
int nrows = Comb(N, R) + 1;
int ncolumns = 8;
int Pos;
int Count;
AnArray = new int* [nrows];
for (int i = 0; i < nrows; i++)
AnArray[i] = new int[ncolumns];
for (int a = 0; a < nrows; a++)
{
for (int b = 0; b <= 7; b++)
AnArray[a][b] = 0;
}
Pos = 0;
Count = 0;
do
{
Pos += 1;
if ((CSTEBit(3, AnArray[Pos][7], 4) == 0) && (CSTEBit(3, AnArray[Pos][7], 5) == 0))
Count += 1;
} while (Count != nrows - 1);
AnArray[Pos][7] = CSTEBit(1, AnArray[Pos][7], 4);
}
int CSTEBit(int CSTE, int Byt, int Bit)
{
int tempCSTEBit = 0;
if (Bit < 8)
{
int Mask = (int)pow(2, Bit);
switch (CSTE)
{
case 0:
tempCSTEBit = (int)(Byt && ~Mask);
break;
case 1:
tempCSTEBit = (int)(Byt | Mask);
break;
case 2:
tempCSTEBit = (int)(Byt ^ Mask);
break;
case 3:
if ((Byt & Mask) > 0)
{
tempCSTEBit = 1;
}
else
{
tempCSTEBit = 0;
}
break;
default:
tempCSTEBit = Byt;
break;
}
}
else
{
tempCSTEBit = Byt;
}
return tempCSTEBit;
}
double Fact(int N)
{
double tempFact = 0;
if (N <= 1)
{
tempFact = 1;
}
else
{
tempFact = N * Fact(N - 1);
}
return tempFact;
}
double Perm(int N, int R)
{
double tempPerm = 0;
int a = 0;
double b;
b = 1;
if (N < R)
{
tempPerm = 0;
}
else
{
for (a = (N - (R - 1)); a <= N; a++)
{
b = b * a;
}
tempPerm = b;
}
return tempPerm;
}
int Comb(int N, int R)
{
int tempComb = 0;
if (N < R)
{
tempComb = 0;
}
else
{
tempComb = (int)(Perm(N, R) / Fact(R));
}
return tempComb;
}
The variable Pos will never be higher than what Comb function returns which is used to initialize the AnArray.变量 Pos 永远不会高于用于初始化 AnArray 的 Comb function 返回的值。 Thanks in advance for any answer.
提前感谢您的任何回答。
Well actually I insert "if (Pos < nrows)" inside the do loop after Pos += 1;好吧,实际上我在 Pos += 1 之后的 do 循环中插入了“if (Pos < nrows)”; and the warning was gone.
警告消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.