cs50 / libcs50

This is CS50's Library for C.
https://cs50.readthedocs.io/libraries/cs50/c/
GNU General Public License v3.0
1.79k stars 861 forks source link

Small improvement in get_string() #244

Closed guidoreina closed 3 years ago

guidoreina commented 3 years ago

In the function get_string() there are two variables: capacity and size. It looks like the idea behind them is not to realloc() for every single character read, but in the while loop capacity is always incremented by 1 instead of by a bigger number or capacity * 2. This leads to realloc() being always called in each iteration, is this intentional?

dmalan commented 3 years ago

Indeed, we used to grow the buffer exponentially, but that felt like overkill, given the library's intended use case (human I/O), so we simplified to just growing by 1 on each iteration, the overhead of which is negligible for the use case. So, good instincts, but conscious choice ultimately!

guidoreina commented 3 years ago

Thank you for your quick answer. You are right, for human input the buffer won't grow that much anyway. Thank you also for the CS50 videos, they are just great.