Closed brycereitano closed 10 years ago
Isnt this using much more ram with realloc
?
As far as I understand, feel free to correct me if I'm wrong. As long as I'm freeing up the memory when I'm done with it. It will only use the memory required. For example when it expands to 2048 it should only be using 2024*sizeof(char).
However, I think you do have a point. I am using more memory storing and applying it to the buffer. At most, I should only be using double the buffer size.
Continuous malloc/realloc is definitely not good, also there's the FIONREAD ioctl to avoid reading one char a time
Simple change to allow input of an unknown line length. The input will start at 1024 characters, if the buffer is not large enough it will expand by 2x. Example: 1024->2048->4096->etc..