←back to thread

237 points ekr____ | 3 comments | | HN request time: 0.603s | source
1. writebetterc ◴[] No.42732148[source]
This post caused me to create an account. This C code is not good. Writing C is absolutely harder than Python, but you're making it so much harder than it has to be. Your program is buggy as heck, has very finicky cleanup code, and so on.

Here's a much easier way to write the program:

1. Dump whole file into buffer as one string

2. Find newlines in buffer, replace with NULs. This also let's you find each line and save them in another buffer

3. Sort the buffer of all the lines you found

4. qsort the buffer

5. Print everything

6. Free both buffers

Or, as a C program: https://godbolt.org/z/38nq1MorM

replies(1): >>42743364 #
2. commandlinefan ◴[] No.42743364[source]
> Dump whole file into buffer as one string

... unless the file is too big to fit into memory?

replies(1): >>42778800 #
3. writebetterc ◴[] No.42778800[source]
Memory will always be the limiting factor here. A strong adversary can ensure that you must know the whole list in order to sort it.

You can always mmap the file instead and let the OS page in and out parts of the file, however :-).