It seems that when I run the following code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv)
{
int i=0;
setvbuf(stdout, NULL, _IOLBF,0);
while (1)
printf("%d ",i++);
return 0;
}
it prints in chunks of 1024 chars, no matter the size I define for setvbuf(). The question is is if size affects somehow in this case and where is the definition for 1024 chars is coming from.
I don't know how you identified
1024
but it's probablyBUFSIZ
.BUFSIZ
is defined instdio.h
.EDIT
Here's something
glibc
says:EDIT 2
@larsmans is right. I looked at how
setvbuf
is implemented and it ignores a call when asking for line buffering and presenting a NULL buf. Now,stdout
is no ordinary file, it's attached to a terminal. So, heading over to pixelbeatAccording to the (draft) C standard,
So, assuming you measured correctly, it seems like Glibc is free to do as it pleases when
buf
is null, and it gives you a 1kB buffer. Since you never write a newline, line buffering has no effect and the behavior is similar to full buffering.The size probably doesn't have much effect for a NULL buf pointer.
However, you are still requesting buffered output with
_IOLBF
Try_IONBF
instead.http://en.wikipedia.org/wiki/Setvbuf