无法解决警告C6386

时间:2020-04-22 18:32:10

标签: c++ visual-studio warnings

拜托,我需要一些帮助。我在写入“ AnArray”时收到警告C6386缓冲区溢出:可写大小为“ rows * 8”字节,但可能写入了“ 16”字节。在以下代码上

#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;
}

变量Pos永远不会高于Comb函数返回的用于初始化AnArray的函数。预先感谢您的回答。

1 个答案:

答案 0 :(得分:0)

实际上我在Pos + = 1之后在do循环中插入了“ if(Pos