I'm doing assignments that compares efficiency of several sortings. I'm coding on Visual Studio. Since VS doesn't allow making array with variable size, I had to make array by using malloc().
Program does work. but, at the end it shows a message like following.
Debug Error!
Program:
_ergence\project\git\git_test\sort\Project1\Debug\Project1.exe
HEAP CORRUPTION DETECTED: after Normal block (#80) at 0x008FD038.
CRT detected that the application wrote to memory after end of heap buffer.
As I searched, sbd said "it occurs when you try to free pointers which point bigger size than itself" but I have no idea about another ways to free them.
I know that if I don't free them, OS will free them instead. But I'm doing assignment in College, so I have to check it out.
Here's my source code. and sorry for my poor English.
#include <stdio.h>
int change = 0;
int compare = 0;
void main() {
int* arr[10];
for (int i = 0; i < 10; i++)
{
int size = (i + 1) * 1000;
arr[i] = (int *)malloc(size * sizeof(int));
}
for (int i = 0; i < 6; i++) {
switch (i) {
case 0:
printf("SelectionSort \n");
break;
case 1:
printf("BubbleSort \n");
break;
case 2:
printf("InsertionSort \n");
break;
case 3:
printf("ShellSort \n");
break;
case 4:
printf("MergeSort \n");
break;
case 5:
printf("QuickSort \n");
break;
}
for (int j = 0; j < 10; j++) {
int size = (j + 1) * 1000;
arraySetup(arr[j], size);
switch (i) {
case 0:
selectionSort(arr[j], size);
break;
case 1:
bubbleSort(arr[j], size);
break;
case 2:
insertionSort(arr[j], size);
break;
case 3:
shellSort(arr[j], size);
break;
case 4:
mergeSort(arr[j], 0, size - 1);
break;
case 5:
quickSort(arr[j], 0, size - 1);
break;
}
printf("size=%d ", size);
printf("compare = %d move = %d\n", compare, change);
compare = 0; change = 0;
}
printf("\n");
}
for(int i = 0; i < 10; i++) {
free(arr[i]);
}
}