关于手册页中无效使用的 setbuf 示例的混淆
Confusion about setbuf example of invalid use from man page
从 man setbuf
到 Ubuntu:
You must make sure that the space that buf points to still exists by the time stream is closed, which also happens at program termination. For example, the following is invalid:
联机帮助页中的示例代码无效:
#include <stdio.h>
int main(void)
{
char buf[BUFSIZ];
setbuf(stdin, buf);
printf("Hello, world!\n");
return 0;
}
为什么无效?局部变量 buf
在程序 returns 之后通过 return 0;
.
销毁
一旦 return
被执行,buf
不再存在,但程序继续执行,调用退出处理程序并关闭流。因此,当流关闭时,缓冲区不存在。
从 man setbuf
到 Ubuntu:
You must make sure that the space that buf points to still exists by the time stream is closed, which also happens at program termination. For example, the following is invalid:
联机帮助页中的示例代码无效:
#include <stdio.h>
int main(void)
{
char buf[BUFSIZ];
setbuf(stdin, buf);
printf("Hello, world!\n");
return 0;
}
为什么无效?局部变量 buf
在程序 returns 之后通过 return 0;
.
一旦 return
被执行,buf
不再存在,但程序继续执行,调用退出处理程序并关闭流。因此,当流关闭时,缓冲区不存在。