diff --git a/cv10/program.c b/cv10/program.c index f81a7a1..a3c994a 100644 --- a/cv10/program.c +++ b/cv10/program.c @@ -1,4 +1,4 @@ -#include +~~#include #include #include @@ -19,13 +19,13 @@ int main() { names = (char **)malloc(capacity * sizeof(char *)); if (names == NULL) { puts("Memory allocation error"); - return 1; + return 0; } if (scanf("%d\n", &max_students) != 1 || max_students <= 0) { puts("Nespravny vstup"); free(names); - return 1; + return 0; } while (fgets(name, MAX_NAME_LENGTH, stdin) != NULL) { @@ -43,17 +43,22 @@ int main() { if (size == capacity) { capacity *= 2; - names = (char **)realloc(names, capacity * sizeof(char *)); - if (names == NULL) { + char **new_names = (char **)realloc(names, capacity * sizeof(char *)); + if (new_names == NULL) { puts("Memory allocation error"); - return 1; + for (int j = 0; j < size; j++) free(names[j]); + free(names); + return 0; } + names = new_names; } names[size] = strdup(name); if (names[size] == NULL) { puts("Memory allocation error"); - return 1; + for (int j = 0; j < size; j++) free(names[j]); + free(names); + return 0; } size++; } @@ -61,7 +66,7 @@ int main() { if (size == 0) { puts("Ziadne prihlasky"); free(names); - return 1; + return 0; } qsort(names, size, sizeof(char *), compare); @@ -85,3 +90,5 @@ int main() { free(names); return 0; } + +