fix: Segmentation fault in `merge_sort.c` (#1243)

* fix segmentation fault

* add a comments

* add print error message when can't malloc and exit program

* Update sorting/merge_sort.c

Co-authored-by: Sharon "Cass" Cassidy <monadicdiffusive@proton.me>

* Update sorting/merge_sort.c

Co-authored-by: Sharon "Cass" Cassidy <monadicdiffusive@proton.me>

---------

Co-authored-by: Sharon "Cass" Cassidy <monadicdiffusive@proton.me>
This commit is contained in:
Pongsakorn TIPPAYASOMDECH 2023-04-09 08:58:03 +07:00 committed by GitHub
parent 0c5eccc69e
commit 2698ad2d13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 1 deletions

View File

@ -33,6 +33,11 @@ void swap(int *a, int *b)
void merge(int *a, int l, int r, int n) void merge(int *a, int l, int r, int n)
{ {
int *b = (int *)malloc(n * sizeof(int)); /* dynamic memory must be freed */ int *b = (int *)malloc(n * sizeof(int)); /* dynamic memory must be freed */
if (b == NULL)
{
printf("Can't Malloc! Please try again.");
exit(EXIT_FAILURE);
}
int c = l; int c = l;
int p1, p2; int p1, p2;
p1 = l; p1 = l;
@ -101,18 +106,32 @@ void merge_sort(int *a, int n, int l, int r)
int main(void) int main(void)
{ {
int *a, n, i; int *a, n, i;
printf("Enter Array size: ");
scanf("%d", &n); scanf("%d", &n);
if (n <= 0) /* exit program if arraysize is not greater than 0 */
{
printf("Array size must be Greater than 0!\n");
return 1;
}
a = (int *)malloc(n * sizeof(int)); a = (int *)malloc(n * sizeof(int));
if (a == NULL) /* exit program if can't malloc memory */
{
printf("Can't Malloc! Please try again.");
return 1;
}
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
printf("Enter number[%d]: ", i);
scanf("%d", &a[i]); scanf("%d", &a[i]);
} }
merge_sort(a, n, 0, n - 1); merge_sort(a, n, 0, n - 1);
printf("Sorted Array: ");
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
printf(" %d", a[i]); printf("%d ", a[i]);
} }
printf("\n");
free(a); free(a);