排序程序的错误(检测到堆损坏)

Error of sorting program (HEAP CORRUPTION DETECTED)

本文关键字:损坏 检测 程序 错误 排序      更新时间:2023-10-16

此程序与Bubblesort,Insertionsort和Qsort Time有关。

我运行程序并获得了调试错误,

HEAP CORRUPTION DETECTED:after Normal block(#152)at 0x006613C0 CRT Detected 

应用程序在堆缓冲区结束后写入内存。

然后,我在 void part_1(int n)中删除最后3行代码(delete [] a; delete [] b; delete [] c(以使其正常工作。我的教授告诉我:"您的程序应该还有其他错误,这些错误导致删除语句中的故障。"我不应该删除最后3行代码。我找不到。请帮助。

// Part-1 : --------------------------- sorting algorithms
void bubbleSort(double *x, int n)
{
// Implement the sorting function using the bubble sort algorithm.
double temp;
for (int j = 0;j < n;j++) {
    for (int i = 0;i < n;i++) {
        if (x[i] > x[i + 1]) {
            temp = x[i];
            x[i + 1] = x[i];
            x[i + 1] = temp;
        }
    }
  }
}

void insertionSort(double *x, int n)
   {
// Implement the sorting function using the insertion sort algorithm.
for (int i = 1;i < n;i++) {
    double temp = x[i];
    int j = 0;
    for (j = i - 1;j >= 0 && x[j]>temp;j--) {
        x[j + 1] = x[j];
    }
    x[j + 1] = temp;
  }
}

int compare_1(const void *a, const void *b) {
double *X = (double *)a;
double *Y = (double *)b;
if (*X > *Y) {
    return 1;
}
else if (*X < *Y)
    return -1;
return 0;
}
void part_1(int n)
{
srand((unsigned)time(NULL));  // set the seed of the random number generator
double *a = new double[n];  // create 3 arrays with identical contents
double *b = new double[n];
double *c = new double[n];
for (int i = 0; i < n; i++)
    a[i] = b[i] = c[i] = rand() / 10000.0;
clock_t begin, end;
double elapsedTime;
cout << "Bubble sort: sorting an array of size " << n << endl;
begin = clock();
bubbleSort(a, n);
end = clock();
elapsedTime = (double)(end - begin) / CLOCKS_PER_SEC;
cout << "Elapsed time = " << elapsedTime << " seconds" << endl << endl;
for (int i = 0; i < n - 1; i++)
    if (a[i] > a[i + 1])
    {
        cout << "Bubble sort : Incorrect resultsnn";
        break;
    }
cout << "Insertion sort: sorting an array of size " << n << endl;
begin = clock();
insertionSort(b, n);
end = clock();
elapsedTime = (double)(end - begin) / CLOCKS_PER_SEC;
cout << "Elapsed time = " << elapsedTime << " seconds" << endl << endl;
for (int i = 0; i < n - 1; i++)
    if (b[i] > b[i + 1])
    {
        cout << "Insertion sort : Incorrect resultsnn";
        break;
    }
cout << "Write your statements to sort array c[] using qsort()n";
cout << "qsort: sorting an array of size " << n << endl;
begin = clock();
// #### write your statements to sort array c[] using qsort().
//      Define your own compare function.
qsort(c, n, sizeof(double), compare_1);
end = clock();
elapsedTime = (double)(end - begin) / CLOCKS_PER_SEC;
cout << "Elapsed time = " << elapsedTime << " seconds" << endl << endl;
for (int i = 0; i < n - 1; i++)
    if (c[i] > c[i + 1])
    {
        cout << "qsort : Incorrect resultsnn";
        break;
    }
delete[] a;
delete[] b;
delete[] c;
}
int main()
{
part_1(50000);
system("pause");
return 0;
}

由于内存腐败而发生此错误。在这里,发生内存损坏是因为您写了超过阵列限制。例如:如果有5个整数的数组,例如

int array[5];

您不应该做

之类的事情
int n=4;
array[n+1] = 10; //array out of bound memory write operation

c/c 不会在界限操作中检查数组。它允许这种操作无编译错误。结果是,运行程序时可能会发生任何事情。因此,程序员有责任检查此类错误。

在您的代码中找到一个这样的实例。

void bubbleSort(double *x, int n)
{
// Implement the sorting function using the bubble sort algorithm.
double temp;
for (int j = 0;j < n;j++) {
    for (int i = 0;i < n;i++) {
        if (x[i] > x[i + 1]) {
            temp = x[i];
            x[i + 1] = x[i]; // out of bound write when i=n-1.
            x[i + 1] = temp;
        }
    }
  }
}

bubbleSort中,您的数组索引不在范围内。修改如下所示的函数,您将看到会发生什么:

void bubbleSort(double *x, int n)
{
  // Implement the sorting function using the bubble sort algorithm.
  double temp;
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < n; i++) {
      if (i + 1 >= n)
      { // index is out of range
        cout << "Bummern";
        exit(1);
      }
      if (x[i] > x[i + 1]) {
        temp = x[i];
        x[i + 1] = x[i];
        x[i + 1] = temp;
      }
    }
  }
}

在您的另一种功能中,很可能有类似的问题。