Browse Source

add check to malloc calls

tags/1.0.0
Chris Cromer 11 months ago
parent
commit
1024e6f8dc
Signed by: Chris Cromer <chris@cromer.cl> GPG Key ID: 39CC813FF3C8708A
3 changed files with 32 additions and 0 deletions
  1. 8
    0
      src/count_sort.c
  2. 12
    0
      src/sort.c
  3. 12
    0
      test/test.c

+ 8
- 0
src/count_sort.c View File

@@ -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++) {

+ 12
- 0
src/sort.c View File

@@ -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

+ 12
- 0
test/test.c View File

@@ -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);


Loading…
Cancel
Save