Home > OS >  HEAP CORRUPTION DETECTED (dynamic array)
HEAP CORRUPTION DETECTED (dynamic array)

Time:11-11

I'm writing a program that dynamically allocates an array, on the heap, fills it with random numbers from 55-99 and prints them out in 10 rows. I'm using 9 more dynamic arrays to store each of these rows. But when running the program I get the error: HEAP CORRUPTION DETECTED after normal block. I'm sorry for the chunky code.

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

    }
*/

}```

CodePudding user response:

You have a typo in these statements

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

There is used the same array for different indices.

  • Related