aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sort.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/sort.c b/sort.c
index 4c0f50b..c2c55fb 100644
--- a/sort.c
+++ b/sort.c
@@ -2,32 +2,35 @@
#include <stdlib.h>
#include <string.h>
-#define MAX_LINE_LENGTH 32
+#define MAX_LINE_LENGTH (2 << 8)
+#define LINES_VECTOR_SIZE_INITIAL (2 << 6)
+#define LINES_VECTOR_SIZE_INCREMENT (2 << 4)
static int qsort_strcmp(const void *p1, const void *p2);
int main(void)
{
char **lv;
- unsigned long lc;
- unsigned int i;
+ unsigned long la, lc, li;
+
+ la = LINES_VECTOR_SIZE_INITIAL;
+ lv = (char **) malloc(la * sizeof(char *));
- lv = malloc(sizeof(char *));
for (lc = 0 ; ; lc++) {
- fprintf(stderr, "Current char pointers allocated to lv: %lu\n", sizeof(lv) / sizeof(char *));
- if (lc >= sizeof(lv) / sizeof(char *)) {
- lv = realloc(lv, (sizeof(lv) + 32) * sizeof(char *));
+ if (lc == la) {
+ la += LINES_VECTOR_SIZE_INCREMENT;
+ lv = (char **) realloc(lv, la * sizeof(char *));
}
- lv[lc] = calloc(MAX_LINE_LENGTH, sizeof(char));
+ lv[lc] = (char *) malloc(MAX_LINE_LENGTH * sizeof(char));
if ((fgets(lv[lc], MAX_LINE_LENGTH, stdin)) == NULL) {
break;
}
}
- qsort(&lv, lc, sizeof(char *), qsort_strcmp);
+ qsort(lv, lc, sizeof(char *), qsort_strcmp);
- for (i = 0; i < lc; i++) {
- fputs(lv[i], stdout);
+ for (li = 0; li < lc; li++) {
+ fputs(lv[li], stdout);
}
exit(EXIT_SUCCESS);