-1

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]);
    }
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 3
    You've got `arr[5]`, but then loop through `arr[i]` such that `i` goes as high as `9`. That can't be good. – Christian Gibbons Apr 12 '18 at 14:31
  • Oh my gosh. Thank you very much. – Zerobell Lee Apr 12 '18 at 14:33
  • That's one of the reasons 'magic numbers' are considered dangerous. If you set a macro or something to define the size of the array and also used it for the loop, you wouldn't have to worry about mismatches if you ever go back and change the size of the array or loop iterations. – Christian Gibbons Apr 12 '18 at 14:35

2 Answers2

2

You exceed array bounds in that you write to arr[i] with i being up to 9, whereas the array is declared as int* arr[5]. Maybe there are other issues as well, yet this is the most obvious one to me.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

int* arr[5] gives you 5 pointers (actually, an array of 5 pointers), but you are looping up to 9. So, arr[9] (and, arr[5] and onwards) is an error.

You must take care of that first.

Additinally:

  1. Don't cast the result of malloc(). malloc() returns a void pointer anyway, so the casting is needless. This answer here is my personal favorite to reason it. Take a look!
  2. Change void main() to int main() or int main(void). The C standard suggests the same, see!.
  3. You call malloc(), but this code seems to assume that it will succeed no matter what. You MUST check the retutn value of malloc(), if it is NULL, handle the error.
WedaPashi
  • 3,561
  • 26
  • 42