I have noticed that in my implementation of printf
for C
. One buffer of 1024 bytes is allocated and used for all subsequent calls and not explicitly freed (with the quoted justification being that the Linux kernel will do so upon program termination).
I have read about buffered I/O; however, I do not quite understand why this buffer is reused instead of allocating a buffer of necessary size for each call. I have considered the following reasons:
- reduce number of system calls
malloc
may take uncertain amount of time or have variable results- the amount of space necessary may not be available (unlikely on my system)
I do not quite understand the motivation. Can anyone provide a clear cut reason? Obviously this is simply speculation, unless the person who wrote the source for printf
were to see this question.