add check to malloc calls

This commit is contained in:
Chris Cromer 2018-11-14 16:21:26 -03:00
parent fa78806865
commit 1024e6f8dc
Signed by: cromer
GPG Key ID: 39CC813FF3C8708A
3 changed files with 32 additions and 0 deletions

View File

@ -25,7 +25,15 @@ void count_sort(int *array, int n) {
int i; int i;
int j; int j;
int *temp = malloc(sizeof(int) * n); int *temp = malloc(sizeof(int) * n);
if (temp == NULL) {
fprintf("Error: Out of heap space!\n");
exit(5);
}
int *count = malloc(sizeof(int) * n); int *count = malloc(sizeof(int) * n);
if (count == NULL) {
fprintf("Error: Out of heap space!\n");
exit(5);
}
memcpy(temp, array, sizeof(int) * n); memcpy(temp, array, sizeof(int) * n);
for (i = 0; i < n - 1; i++) { for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) { for (j = i + 1; j < n; j++) {

View File

@ -81,6 +81,10 @@ int read_buffer(int *variable) {
} }
} }
char **check = malloc(sizeof(char**)); char **check = malloc(sizeof(char**));
if (check == NULL) {
fprintf("Error: Out of heap space!\n");
exit(5);
}
long input = strtol(buffer, check, 10); long input = strtol(buffer, check, 10);
if (*check[0] == '\0') { if (*check[0] == '\0') {
free(check); free(check);
@ -197,7 +201,15 @@ int main (int argc, char **argv) {
} }
unordered_array = malloc(sizeof(int) * n); 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); work_array = malloc(sizeof(int) * n);
if (work_array == NULL) {
fprintf("Error: Out of heap space!\n");
exit(5);
}
atexit(cleanup); atexit(cleanup);
// Llenar el array con valores para ordenar después // Llenar el array con valores para ordenar después

View File

@ -57,8 +57,20 @@ int main(int argc, char **argv) {
int failed = 0; int failed = 0;
test_case = malloc(sizeof(int) * n); test_case = malloc(sizeof(int) * n);
if (test_case == NULL) {
fprintf("Error: Out of heap space!\n");
exit(1);
}
qarray = malloc(sizeof(int) * n); qarray = malloc(sizeof(int) * n);
if (qarray == NULL) {
fprintf("Error: Out of heap space!\n");
exit(1);
}
test_array = malloc(sizeof(int) * n); test_array = malloc(sizeof(int) * n);
if (test_array == NULL) {
fprintf("Error: Out of heap space!\n");
exit(1);
}
atexit(cleanup); atexit(cleanup);