检测到堆损坏

Heap Corruption Detected?

本文关键字:损坏 检测      更新时间:2023-10-16
void allocateTriangle(int ** & twoDArray, int numRows);
void printTriangle(int ** twoDArray, int numRows);
void growToSquare(int ** & twoDArray, int numRows);
void printSquare(int ** & twoDArray, int numRows);
using namespace std;
int main(int argc, char ** argv){
    srand((unsigned int)time(NULL));
    int ** twoDArray;
    int numRows = 10;
    allocateTriangle(twoDArray, numRows);
    cout << "******* Triangle array *******" << endl;
    printTriangle(twoDArray, numRows);
    growToSquare(twoDArray, numRows);
    cout << "******* Sqaure array *******" << endl;
    printSquare(twoDArray, numRows);
    return 0;
}    
void allocateTriangle(int ** & twoDArray, int numRows) {
    twoDArray = new int*[numRows];
    for (int x = 0; x < numRows; x++){
        twoDArray[x] = new int[x];
        for (int y = 0; y <= x; y++){
            twoDArray[x][y] = rand() % 100;
        }
    }
}
void printTriangle(int ** twoDArray, int numRows){
    for (int x = 0; x < numRows; x++){
        for (int y = 0; y <= x; y++){
            cout << setw(5);
            cout << twoDArray[x][y];
        }
    cout << endl;
    }
}
void growToSquare(int ** & twoDArray, int numRows) {
    for (int x = 0; x < numRows; x++){
        int *tempArray = new int[x];
        *tempArray = *twoDArray[x];
-->     delete [] twoDArray[x];
        twoDArray[x] = new int[numRows];
        for (int y = 0; y < numRows; y++){
            if (y <= x)
                twoDArray[x][y] = tempArray[y];
            else
                twoDArray[x][y] = rand() % 100;
        }
    }
}
void printSquare(int ** & twoDArray, int numRows){
    for (int x = 0; x < numRows; x++){
        for (int y = 0; y <= numRows; y++){
            cout << setw(5);
            if (y <= x)
                cout << twoDArray[x][y];
            else if (y - 1 == x) 
                cout << " ";
            else
                cout << twoDArray[x][y - 1];
        }
        cout << endl;
    }
}

箭头指向程序可能崩溃的地方。给我一个堆腐败,我花了6个小时看这个和其他帖子,我什么也没得到。

main得到growToSquare(),当我试图通过删除和分配再次"调整"2D数组时,它在删除时停止。我已经注释掉了其他行代码,看看是否有其他东西触发,没有。

有什么建议吗?(我使用的是Visual studio 2013)

WhozCraig在上面的评论中发现了它。

  • y <= x应该是y < x,在这个代码的多个地方。——WhozCraig

为了简单起见,假设numRows在这里是1。在这个简单的例子中遵循代码逻辑。

void allocateTriangle(int ** & twoDArray, int numRows) {
    twoDArray = new int*[numRows];
    for (int x = 0; x < numRows; x++){
        twoDArray[x] = new int[x];
        for (int y = 0; y <= x; y++){
            twoDArray[x][y] = rand() % 100;
        }
    }
}

下面是将要执行的代码,它赋值给一个零长度数组。

    twoDArray = new int*[1];
        twoDArray[0] = new int[0];
            twoDArray[0][0] = rand() % 100;

内存已被破坏。


三角形分配的合理解

虽然有大约六种不同的方法来计算索引,但这种方法可能是最容易理解的。

void allocateTriangle(int ** & twoDArray, int numRows)
{
    twoDArray = new int*[numRows];
    for (int x = 0; x < numRows; x++)
    {
        twoDArray[x] = new int[x+1]; // NOTE: size
        for (int y = 0; y <= x; y++)
            twoDArray[x][y] = rand() % 100;
    }
}
void printTriangle(int ** twoDArray, int numRows)
{
    for (int x = 0; x < numRows; cout << endl, ++x)
        for (int y = 0; y <= x; y++)
            cout << setw(5) << twoDArray[x][y];
}

这允许你保持你的索引(大部分),同时调整分配到适当的长度。


把三角形变成正方形

同样,下面的代码将把三角形扩展成一个正方形。注意:你可以调用已经扩展到一个正方形的东西。当这样做时,它将简单地重新随机生成右半对角线(并且相当昂贵)。

void growToSquare(int ** & twoDArray, int numRows)
{
    for (int x = 0; x < numRows; x++)
    {
        int *tempArray = new int[numRows];
        for (int y=0; y<=x; ++y)
            tempArray[y] = twoDArray[x][y];
        for (int y=x+1; y<numRows; ++y)
            tempArray[y] = rand() % 100;
        delete [] twoDArray[x];
        twoDArray[x] = tempArray;
    }
}
void printSquare(int ** & twoDArray, int numRows)
{
    for (int x = 0; x < numRows; cout << endl, ++x)
        for (int y = 0; y < numRows; y++)
            cout << setw(5) << twoDArray[x][y];
}

使用上述方法,您的输出将如下所示。显然,随机性质会导致不同的值,但重要的是,正方形的左下角部分保留了原始三角形。

******* Triangle array *******
   80
   80    1
   45   93   28
   19   96   90    7
   38   70   23   26   98
   97   26   98   48   37   97
   77   25   43    0   28   84   90
   95   78   48   16   23   30   14   64
   14   29   83   60    7   83   14   77   94
   79    1   43   55   22   14   80   34   40   53
******* Sqaure array *******
   80   10   62   17   62   94   62   47   87    3
   80    1   56   67   56   91   85   51   25    8
   45   93   28    9   42   57   95   56   19   42
   19   96   90    7   46   67   42   77   53   73
   38   70   23   26   98   53   22   34   69    3
   97   26   98   48   37   97    8   18   53   55
   77   25   43    0   28   84   90    7   82   43
   95   78   48   16   23   30   14   64   94   33
   14   29   83   60    7   83   14   77   94   75
   79    1   43   55   22   14   80   34   40   53