排序程序错误(HEAP CORRUPTION DETECTED)

时间:2018-02-27 13:33:24

标签: c++ sorting

这个程序是关于bubbleSort,insertionSort和qsort的时间。

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

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

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

然后,我删除void part_1(int n)中的最后3行代码(删除[] a;删除[] b;删除[] 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 results\n\n";
        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 results\n\n";
        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 results\n\n";
        break;
    }
delete[] a;
delete[] b;
delete[] c;
}
int main()
{
part_1(50000);

system("pause");
return 0;
}

2 个答案:

答案 0 :(得分:0)

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 << "Bummer\n";
        exit(1);
      }
      if (x[i] > x[i + 1]) {
        temp = x[i];
        x[i + 1] = x[i];
        x[i + 1] = temp;
      }
    }
  }
}

您的其他排序函数中很可能存在类似的问题。

答案 1 :(得分: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;
        }
    }
  }
}
相关问题