代码之家  ›  专栏  ›  技术社区  ›  MathEnthusiastThatsBadAtMath

警告C6386(使用数组,而非fstream)

  •  0
  • MathEnthusiastThatsBadAtMath  · 技术社区  · 2 年前

    我正在开发一个二进制类,它使用三态布尔值来创建一个带有清晰分隔符的数组(我不想使用vector,因为我是stuborn,我使用的代码将被传输到Arduino,我不知道它是否有std库)。以下是我使用的枚举:

    enum tribool { DELIM = -1, FALSE, TRUE };

    我正在开发二进制类的复制构造函数(或移动构造函数,我忘记了区别),在“tribolList[I]=(const-tribol)copyBinary.tribolList[I];”上我收到了警告C6386。

    我看了Microsoft Learn,仍然不明白这个问题。我还查看了同一问题的堆栈溢出;除了另一个人有这个警告和std::fstream的问题。

    Image of the code because I was having issues with text formatting (Sorry in advance)

    编辑:我应该澄清一下,我还没有发现Copy Constructor的任何运行时问题,只是这个警告有点让我担心

    编辑2:没关系,我发现了一些问题。以下是错误窗口的一个片段: Error snippet

    Binary::Binary(Binary& copyBinary, bool alias) { //I had to send a snippet because text formatting wasn't working
        if (alias) {
            triboolList = copyBinary.triboolList; //tribool List is defined as "tribool* triboolList;"
        }
        else {
            triboolList = new tribool[triLength(copyBinary.triboolList)];
            for (int i = 0; i < triLength(copyBinary.triboolList); i++) {
                triboolList[i] = (const tribool)copyBinary.triboolList[i];
            }
            triboolList[triLength(copyBinary.triboolList)] = DELIM;
        }
    }
    
    1 回复  |  直到 2 年前
        1
  •  0
  •   Botje    2 年前

    替换表达式 triLength(copyBinary.triboolList) X:

    triboolList = new tribool[X];
    for (int i = 0; i < X; i++) {
        triboolList[i] = (const tribool)copyBinary.triboolList[i];
    }
    triboolList[X] = DELIM;
    

    您现在可以清楚地看到错误:在索引X处访问大小为X的数组是非法的,因为最后一个有效索引是X-1。