Okay, this time, i have to say it's a weird error. This code runs fine in Code::Blocks 8.02 but fails to run in my college compiler.
I think my college compiler is the GCC Compiler (same as my home compiler) but in LINUX environment.
Something like
vi sort.c
cc sort.c -o sort
./sort
Here is the code ...
# include <stdio.h>
# include <stdlib.h>
void array_input ( int *, int );
void array_display ( int *, int );
void array_merge ( int *, int , int , int );
void sort_merge ( int *, int , int );
void pause ( void );
int main()
{
int option;
int size_of_array = 0;
int *array = NULL;
do
{
printf("\n\t=========================================");
printf("\n\t| SORTING TECHNIQUES |");
printf("\n\t=========================================");
printf("\n\n\tEstablish The Size Of Array [1]");
printf("\n\n\tEnter The Elements Into The Array [2]");
printf("\n\n\tUse Sorting Method : Merge Sort [5]");
printf("\n\n\tDisplay The Array [8]");
printf("\n\n\tExit [0]");
printf("\n\n\t\tEnter Choice : ");
scanf("%d", &option);
switch (option)
{
case 1 :
if(array != NULL)
free(array);
printf("\n\n\t\tEnter The Size Of Array : ");
scanf("%d", &size_of_array);
array = (int *)calloc(size_of_array,sizeof(int));
pause();
break;
case 2 :
if (size_of_array < 1)
{
printf("\n\n\t\tInvalid Size Of Array!");
}
else
{
free(array);
printf("\n\n\t\tEnter The Elements Into The Array : ");
array_input(array,size_of_array);
}
pause();
break;
case 5 :
if (array == NULL)
{
printf("\n\n\t\tEmpty Array!");
}
else
{
sort_merge(array,0,size_of_array);
printf("\n\n\t\tYour Array Has Been Sorted ... ");
}
pause();
break;
case 8 :
if (array == NULL)
{
printf("\n\n\t\tEmpty Array!");
}
else
{
printf("\n\n\t\tYour Array : ");
array_display(array,size_of_array);
}
pause();
break;
case 0 :
printf("\n\n\t\tThank You!");
break;
default:
printf("\n\n\t\tWrong Option!\n\n\t\tTry Again");
pause();
break;
}
}
while (option != 0);
free(array);
return EXIT_SUCCESS;
}
void array_input (int * array, int size_of_array)
{
int i;
for (i = 0; i < size_of_array; i++)
{
printf("\n\t\t\t%d.\t", (i + 1));
getchar();
scanf("%d", (array + i));
}
}
void array_display (int * array, int size_of_array)
{
int i;
for (i = 0; i < size_of_array; i++)
{
printf("\n\t\t\t%d.\t%d", (i + 1), array[i]);
}
}
void array_merge (int * array, int low, int high, int mid)
{
int i = low;
int j = mid + 1;
int k = low;
int * array_temp = (int *)malloc(sizeof(array));
while (i <= mid && j <= high)
{
if (array[i] < array[j])
{
array_temp[k++] = array[i++];
}
else
{
array_temp[k++] = array[j++];
}
}
while (i <= mid)
{
array_temp[k++] = array[i++];
}
while (j <= high)
{
array_temp[k++] = array[j++];
}
for (i = low; i < k; i++)
{
array[i] = array_temp[i];
}
free(array_temp);
}
void sort_merge (int * array, int low, int high)
{
if (low < high)
{
int mid = (low + high) / 2;
sort_merge (array, low, mid);
sort_merge (array, mid + 1, high);
array_merge (array, low, high, mid);
}
}
void pause()
{
getchar();
printf("\n\n\t\tPress Enter To Continue ... ");
getchar();
}
Here is the sample input
[sumitro_bhaumik@radix xvr]$ cc sort.c -o sort
[sumitro_bhaumik@radix xvr]$ ./sort
=========================================
| SORTING TECHNIQUES |
=========================================
Establish The Size Of Array [1]
Enter The Elements Into The Array [2]
Use Sorting Method : Merge Sort [5]
Display The Array [8]
Exit [0]
Enter Choice : 1
Enter The Size Of Array : 5
Press Enter To Continue ...
=========================================
| SORTING TECHNIQUES |
=========================================
Establish The Size Of Array [1]
Enter The Elements Into The Array [2]
Use Sorting Method : Merge Sort [5]
Display The Array [8]
Exit [0]
Enter Choice : 2
Enter The Elements Into The Array :
1. 5
2. 1
3. 4
4. 2
5. 3
Press Enter To Continue ...
=========================================
| SORTING TECHNIQUES |
=========================================
Establish The Size Of Array [1]
Enter The Elements Into The Array [2]
Use Sorting Method : Merge Sort [5]
Display The Array [8]
Exit [0]
Enter Choice : 5
*** glibc detected *** free(): invalid next size (fast): 0x08156020 ***
Aborted
[sumitro_bhaumik@radix xvr]$
I have tried Google about the "glibc detected" error but i could not come to any specific conclusion
Is this error same as segmentation fault?
Thanks in advance