diff --git a/src/count_sort.c b/src/count_sort.c index 43d121c..ea4de45 100644 --- a/src/count_sort.c +++ b/src/count_sort.c @@ -25,7 +25,15 @@ void count_sort(int *array, int n) { int i; int j; int *temp = malloc(sizeof(int) * n); + if (temp == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(5); + } int *count = malloc(sizeof(int) * n); + if (count == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(5); + } memcpy(temp, array, sizeof(int) * n); for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { diff --git a/src/sort.c b/src/sort.c index 59b6c42..6adff95 100644 --- a/src/sort.c +++ b/src/sort.c @@ -81,6 +81,10 @@ int read_buffer(int *variable) { } } char **check = malloc(sizeof(char**)); + if (check == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(5); + } long input = strtol(buffer, check, 10); if (*check[0] == '\0') { free(check); @@ -197,7 +201,15 @@ int main (int argc, char **argv) { } unordered_array = malloc(sizeof(int) * n); + if (unordered_array == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(5); + } work_array = malloc(sizeof(int) * n); + if (work_array == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(5); + } atexit(cleanup); // Llenar el array con valores para ordenar después diff --git a/test/test.c b/test/test.c index 048c9f5..e874328 100644 --- a/test/test.c +++ b/test/test.c @@ -57,8 +57,20 @@ int main(int argc, char **argv) { int failed = 0; test_case = malloc(sizeof(int) * n); + if (test_case == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(1); + } qarray = malloc(sizeof(int) * n); + if (qarray == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(1); + } test_array = malloc(sizeof(int) * n); + if (test_array == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(1); + } atexit(cleanup);