I am looking at an example given in Chapter 5 of "C Traps and Pitfalls" (PDF):
#include <stdio.h>
main()
{
int c;
char buf[BUFSIZ];
setbuf(stdout, buf);
while ((c = getchar()) != EOF)
putchar(c);
}
Unfortunately, this program is wrong, for a subtle reason. To see where the trouble lies, ask when the buffer is flushed for the last time.
Answer: after the main program has finished, as part of the cleaning up that the library does before handing control back to the operating system.But by that time, the buffer has already been freed!
main is just a function that must clean up stacks and variables. but what does it mean: the buffer has already been freed?
i find it's difficult to understand. could anyone explain it detaily? thanks in advance.