get_next_line
C project · file I/O, memory
Reads a file descriptor one line at a time with careful dynamic memory handling.
- 100/100grade
- 1 to 10MBUFFER_SIZE, same output
- 0bytes leaked
The problem
Return one line at a time from a file descriptor, whatever the buffer size.
What I built
- A static leftover buffer carried between calls.
- The same output byte for byte from BUFFER_SIZE=1 to 10,000,000, on files and standard input, with only read, malloc and free.
The hard part
Who owns the leftover buffer. The one static pointer is reassigned on every way out of the function: a failed read frees it and leaves it NULL, a last line without a newline empties it, and end of file frees it before returning NULL. So nothing stays allocated once a file is read to the end or a read fails: 0 bytes leaked at BUFFER_SIZE 1, 42 and 9999.
Built with
- C
- read(2)
- valgrind
More projects
- metro · Lisbon metro sim in pixel art: watch the four lines run, step into any station, board a train and ride it.
- pac-man · Pac-Man in Python, built as a duo with sylvzzz: a new maze every level and four ghosts that each hunt you differently.
- call_me_maybe · Turns a sentence into a JSON function call with a 0.6B model using constrained decoding — parseable by construction, not by luck.
- codexion · Multithreaded twist on the Dining Philosophers: coders compete for shared dongles, with FIFO/EDF scheduling.
- dbaltaza.pt · A game-console portfolio where every project is a pre-rendered disc: Vite + TypeScript, no framework, and a paint budget the tests measure.
- fly-in · Drone fleet routing: Dijkstra pathfinding, turn-based scheduling and a pygame replay.
- a-maze-ing · Maze generator with multiple algorithms, pathfinding solvers and terminal visualization.
- push_swap · Highly optimized sorting with two stacks and a tiny set of allowed operations.
- ft_printf · printf rebuilt from scratch: chars, strings, pointers, decimals, unsigned and hex.
- Libft · My own C standard library — the foundation every later 42 project is built on.