Я написал приложение, которое показывает время выполнения определенных алгоритмов сортировки. После того, как все сделано, я получаю такую ошибку:
"Обнаружено повреждение кучи после нормального блока (# 191) в 0x00C432B0. CRT обнаружил, что приложение записало в память после завершения буфера hean."
Что я могу сделать, чтобы избежать такого рода ошибок? Что не так в моем коде? Также все предупреждения помечены.
#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."