Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Buffered stderr on Linux
- X-seq: zsh-workers 816
- From: Zoltan Hidvegi <hzoli@xxxxxxxxxx>
- To: schaefer@xxxxxxx
- Subject: Re: Buffered stderr on Linux
- Date: Wed, 13 Mar 1996 00:17:17 +0100 (MET)
- Cc: zsh-workers@xxxxxxxxxxxxxxx
- In-reply-to: <9603121124.ZM2604@xxxxxxxxxxxxxxxxxxxxxxx> from "Bart Schaefer" at Mar 12, 96 11:24:25 am
- Organization: Dept. of Comp. Sci., Eotvos University, Budapest, Hungary
- Phone: (36 1)2669833 ext: 2667, home phone: (36 1) 2752368
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