-2

I wrote an application, which shows the time of particular sorting algorithms execution. After everything is done I get such an error:

"Heap corruption detected after normal block (#191) at 0x00C432B0. CRT detected that the application wrote to memory after end of hean buffer."

What can I do to avoid this kind of error? What is wrong in my code? Also, all warnings are marked.

#include <iostream>
#include <time.h>
#include <stdlib.h>

void Bubble_Sort (long int* array, int n)
{
    for (int i = 1; i < n; i++) 
    {
        for (int j = n - 1; j >= 1; j--) 
        {
            if (array [j] < array [j - 1])
            {
                int temporary;
                temporary = array [j - 1];
                array [j - 1] = array [j];
                array [j] = temporary;
            }
        }
    }
}

void Quicksort (long int* array, int L, int R)
{
    int pivot = array [(L + R) / 2];
    int i, j, x;
    i = L;
    j = R;

    do 
    {
        while (array[i] < pivot)
        {
            i++;
        }

        while (array[j] > pivot)
        {
            j--;
        }

       // To make this algorithm sort in descending order it is needed to change the "<" and ">" marks in the lines above.

        if (i <= j)
        {
            x = array[i];
            array [i] = array [j];
            array [j] = x;

            i++; j--;
        }
    } while (i <= j);

    if (j > L)
    {
        Quicksort(array, L, j);
    }

    if (i < R)
    {
        Quicksort(array, i, R);
    }
}

void swap(long int* X, long int* Y) 
{
    int Z = *X;
    *X = *Y;
    *Y = Z;
}

void Selection_Sort(long int* array, int n)
{
    int i, j, min;

    for (i = 0; i < n - 1; i++)
    {
        min = i;

        for (j = i + 1; j < n; j++)

            if (array[j] < array[min])
            {
                min = j;
            }

        swap (&array[min], &array[i]);
    }
}

int numbers;
clock_t start, stop;
double how_long;

int main()
{
    std::cout << "Let's check which of the known sorting algorithms is the fastest. \n";
    std::cout << "How many random numbers do you want to put in the test array? "; std::cin >> numbers;

    long int* array_1;
    array_1 = new long int[numbers];

    long int* array_2;
    array_2 = new long int[numbers];

    long int* array_3;
    array_3 = new long int[numbers];

    srand(time(NULL));

    for (int i = 0; i <= numbers; i++)
    {
        array_1[i] = rand()%100; // Here is a warning.
    }

    for (int i = 0; i <= numbers; i++)
    {
        array_2[i] = array_1[i]; // Here is a second warning.
    }

    for (int i = 0; i <= numbers; i++)
    {
        array_3[i] = array_2[i]; // Another one goes here.
    }

    std::cout << "I start sorting the given array using diffrent algorithms. Please wait. \n";

    start = clock();
    Bubble_Sort(array_1, numbers);
    stop = clock();

    how_long = (double) (stop - start) / CLOCKS_PER_SEC; // Here is a warning.

    std::cout << "Bubble sort sorted the given array in " << how_long << " second(s). \n";

    start = clock();
    Quicksort(array_2, array_2[0], array_2[numbers - 1]);
    stop = clock();

    how_long = (double) (stop - start) / CLOCKS_PER_SEC; // Another one here.

    std::cout << "Quicksort sorted the given array in " << how_long << " second(s). \n";

    start = clock();
    Selection_Sort(array_3, numbers);
    stop = clock();

    how_long = (double) (stop - start) / CLOCKS_PER_SEC; // And here.

    std::cout << "Selection sort sorted the given array in " << how_long << " second(s). \n";

    std::cout << "That's all! "; 

    delete[] array_1;
    delete[] array_2;
    delete[] array_3;

    system("pause");

    return 0;
}

// The debug error says: "Heap corruption detected after normal block (#191) at 0x00C432B0."
// And the next line is: "CRT detected that the application wrote to memory after end of heap buffer."
Hyziu
  • 27
  • 5
  • "*// Here is a warning.*": And what does the warning tell you? Also please [don't use `new`](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) like this. Use `std::vector` instead. – walnut Feb 28 '20 at 14:50
  • 3
    First, run each one of the three separatedly: bubble, quicksort or selection. Find out which of the three causes the issue. Then update the question to remove 2/3 of the cases. – Jeffrey Feb 28 '20 at 14:52
  • ***wrote to memory after end of hean buffer*** The debugger gave you a hint. It's telling you that you wrote past the end of one your heap allocated arrays. When you get this error its about code that ran at some time in the past not necessarily the last thing that ran. Heap corruption checks are not after every heap access. – drescherjm Feb 28 '20 at 14:52
  • I am pretty sure all of the warnings are telling you exactly what is wrong. Please read them and if you don't understand them, ask specifically about the warning message with a [repro] for it. – walnut Feb 28 '20 at 14:53
  • 1: You're writing outside the arrays. 2: You're passing array elements to `Quicksort` where it wants indices. 3: There's probably more problems. – molbdnilo Feb 28 '20 at 14:57
  • your bubble sort does not use the `i` variable. – valarMorghulis Feb 28 '20 at 15:01

1 Answers1

1
array_1 = new long int[numbers];

for (int i = 0; i <= numbers; i++)
array_1[i] = rand()%100

That will access out of bounds, on array_1

You need the condition i < numbers instead.

Jeffrey
  • 7,814
  • 1
  • 16
  • 32