检测到堆损坏(动态数组)

Posted

技术标签:

【中文标题】检测到堆损坏(动态数组)【英文标题】:HEAP CORRUPTION DETECTED (dynamic array) 【发布时间】:2021-12-23 06:58:50 【问题描述】:

我正在编写一个程序,它在堆上动态分配一个数组,用 55-99 的随机数填充它,并将它们打印成 10 行。我使用了另外 9 个动态数组来存储这些行中的每一行。但是在运行程序时出现错误:正常块后检测到堆损坏。很抱歉代码太粗了。

#include <iostream>
#include <algorithm>
#include <ctime>

using namespace std;

int numScores = 199;

int* randomTestScores = new int[numScores];

void fillArray() 
    
    for (int i = 0; i < 199; i++) 

        const int min_Value = 55;
        const int max_Value = 99;

        randomTestScores[i] = (rand() % (max_Value - min_Value + 1)) + min_Value;

    



void sortArray()  std::sort(randomTestScores, randomTestScores + numScores); 

// Created better varibale names than a, b, c, etc.

int s5559 = 0, l5559 = 0;
int s6064 = 0, l6064 = 0;
int s6569 = 0, l6569 = 0;
int s7074 = 0, l7074 = 0;
int s7579 = 0, l7579 = 0;
int s8084 = 0, l8084 = 0;
int s8589 = 0, l8589 = 0;
int s9094 = 0, l9094 = 0;
int s9599 = 0, l9599 = 0;


int main() 

    srand(time(0));

    fillArray();
    sortArray();

    // Delete this later
    
    for (int i = 0; i < numScores; i++) 

        cout << randomTestScores[i] << endl;

    

    //

    for (int i = 0; i < numScores;  i++) 

        if (randomTestScores[i] >= 55 && randomTestScores[i] <= 59)  s5559++; 
        if (randomTestScores[i] >= 60 && randomTestScores[i] <= 64)  s6064++; 
        if (randomTestScores[i] >= 65 && randomTestScores[i] <= 69)  s6569++; 
        if (randomTestScores[i] >= 70 && randomTestScores[i] <= 74)  s7074++; 
        if (randomTestScores[i] >= 75 && randomTestScores[i] <= 79)  s7579++; 
        if (randomTestScores[i] >= 80 && randomTestScores[i] <= 84)  s8084++; 
        if (randomTestScores[i] >= 85 && randomTestScores[i] <= 89)  s8589++; 
        if (randomTestScores[i] >= 90 && randomTestScores[i] <= 94)  s9094++; 
        if (randomTestScores[i] >= 95 && randomTestScores[i] <= 99)  s9599++; 

    

    int* testScores5559 = new int[s5559];
    int* testScores6064 = new int[s6064];
    int* testScores6569 = new int[s6569];
    int* testScores7074 = new int[s7074];
    int* testScores7579 = new int[s7579];
    int* testScores8084 = new int[s8084];
    int* testScores8589 = new int[s8589];
    int* testScores9094 = new int[s9094];
    int* testScores9599 = new int[s9599];

    for (int i = 0; i < numScores; i++) 

        if (randomTestScores[i] >= 55 && randomTestScores[i] <= 59) *(testScores5559 + l5559) = randomTestScores[i]; l5559++; 
        if (randomTestScores[i] >= 60 && randomTestScores[i] <= 64) *(testScores6064 + l6064) = randomTestScores[i]; l6064++;
        if (randomTestScores[i] >= 65 && randomTestScores[i] <= 69) *(testScores6569 + l6569) = randomTestScores[i]; l6569++;
        if (randomTestScores[i] >= 70 && randomTestScores[i] <= 74) *(testScores7074 + l7074) = randomTestScores[i]; l7074++;
        if (randomTestScores[i] >= 75 && randomTestScores[i] <= 79) *(testScores7579 + l7579) = randomTestScores[i]; l7579++;
        if (randomTestScores[i] >= 80 && randomTestScores[i] <= 84) *(testScores7579 + l8084) = randomTestScores[i]; l8084++;
        if (randomTestScores[i] >= 85 && randomTestScores[i] <= 89) *(testScores7579 + l8589) = randomTestScores[i]; l8589++;
        if (randomTestScores[i] >= 90 && randomTestScores[i] <= 94) *(testScores7579 + l9094) = randomTestScores[i]; l9094++;
        if (randomTestScores[i] >= 95 && randomTestScores[i] <= 99) *(testScores7579 + l9599) = randomTestScores[i]; l9599++;

    

    delete []randomTestScores;
    delete []testScores5559;
    delete []testScores6064;
    delete []testScores6569;
    delete []testScores7074;
    delete []testScores7579;
    delete []testScores8084;
    delete []testScores8589;
    delete []testScores9094;
    delete []testScores9599;

    return 0;

/*
    if (randomTestScores[i] >= 55 && randomTestScores[i] <= 59) 

        *(testScores5559 + l5559) = randomTestScores[i];
        cout << *(testScores5559 + l5559) << endl;
        l5559++;

    
*/

```

【问题讨论】:

为什么不用std::vector&lt;int&gt; 而不是new[]delete[] 堆损坏通常表明newdelete 之间存在一些不匹配。 我不知道如何使用向量,但如果使用向量可以解决我的问题,我很想学习。 你可以看这里:en.cppreference.com/w/cpp/container/vector页面底部有一个简单的例子。 它可能无法解决您的问题,但 vector 有更多可用的工具(例如 vector::at())可以帮助您找出问题所在。原始指针和new[] 没有这样的设施——你只是被错误的指针使用、段错误等留在了野外。 【参考方案1】:

您在这些陈述中有错字

if (randomTestScores[i] >= 80 && randomTestScores[i] <= 84) *(testScores7579 + l8084) = randomTestScores[i]; l8084++;
if (randomTestScores[i] >= 85 && randomTestScores[i] <= 89) *(testScores7579 + l8589) = randomTestScores[i]; l8589++;
if (randomTestScores[i] >= 90 && randomTestScores[i] <= 94) *(testScores7579 + l9094) = randomTestScores[i]; l9094++;
if (randomTestScores[i] >= 95 && randomTestScores[i] <= 99) *(testScores7579 + l9599) = randomTestScores[i]; l9599++;

不同的索引使用相同的数组。

【讨论】:

@1201ProgramAlarm 已经提到了这一点,但感谢您更进一步告诉我在哪里! :) @Sherixn 完全没有。不客气。:)

以上是关于检测到堆损坏(动态数组)的主要内容,如果未能解决你的问题,请参考以下文章

释放 2D 数组 - 检测到堆损坏

在 dll 导出期间块之前检测到堆损坏

在 C++ 中检测到堆损坏错误

检测到堆损坏 - 仅限 iPhone 5S

在 vs2008 中升级后检测到堆损坏

将项目从 vc6 升级到 vc9 后检测到堆损坏