Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Buffered stderr on Linux



Bart Schaefer wrote:
> On Mar 12,  8:03pm, Zoltan Hidvegi wrote:
> } Subject: Buffered stderr on Linux
> }
> } Now I wander why is it
> } necessary to use malloc to allocate these buffers.  Is there anything
> } against using static char[BUFSIZ] buffers?
> 
> Stdio will call free() on the buffer at fclose().  That means you should
> always pass malloc'd buffers, and never call free() yourself on a buffer
> that's been passed to any of the setbuf() variants!

Well I've just checked it on Solaris and Linux and it seems that you are
wrong.

Try this:

#include <stdio.h>
#include <stdlib.h>

void
main(void)
{
    FILE *fp;
    char *buf;

    for (;;) {
	fp = fopen("/tmp/test_setvbuf", "w");
	buf = malloc(BUFSIZ);
	setvbuf(fp, buf, _IOFBF, BUFSIZ);
	fclose(fp);
    }
}

It will eat all memory you have.  But if you insert a free(buf); after the
fclose() it will run happily.

It means that static buffer can be used with setvbuf.

Zoltan




Messages sorted by: Reverse Date, Date, Thread, Author