cout 和 printf 的缓冲区和输出序列
我今天知道cout和printf有缓冲区,据说缓冲区有点像堆栈,从右到左获取cout和printf的输出,然后从上到下将它们输出(到控制台或文件)。 像这样,
a = 1; b = 2; c = 3;
cout<<a<<b<<c<<endl;
buffer:|3|2|1|<- (take “<-” as a poniter)
output:|3|2|<- (output 1)
|3|<- (output 2)
|<- (output 3)
然后我在下面写一段代码,
#include <iostream>
using namespace std;
int c = 6;
int f()
{
c+=1;
return c;
}
int main()
{
int i = 0;
cout <<"i="<<i<<" i++="<<i++<<" i--="<<i--<<endl;
i = 0;
printf("i=%d i++=%d i--=%d\n" , i , i++ ,i-- );
cout<<f()<<" "<<f()<<" "<<f()<<endl;
c = 6;
printf("%d %d %d\n" , f() , f() ,f() );
system("pause");
return 0;
}
在VS2005下,输出是
i=0 i++=-1 i--=0
i=0 i++=-1 i--=0
9 8 7
9 8 7
在g++((GCC)3.4.2(mingw-special))下,输出是,
i=0 i++=0 i--=1
i=0 i++=-1 i--=0
9 8 7
9 8 7
看来缓冲区就像一个堆栈。 然而,我今天读了C++ Primer Plus,据说cout从左到右工作,每次返回一个对象(cout),所以“这就是让你通过使用连接输出的功能插入”。 但从左到右的方式无法解释cout< 输出 9 8 7 现在我对 cout 的缓冲区如何工作感到困惑,有人可以帮助我吗?
I know cout and printf have buffer today, and it is said that the buffer is some like a stack and get the output of cout and printf from right to left, then put them out(to the console or file)from top to bottem. Like this,
a = 1; b = 2; c = 3;
cout<<a<<b<<c<<endl;
buffer:|3|2|1|<- (take “<-” as a poniter)
output:|3|2|<- (output 1)
|3|<- (output 2)
|<- (output 3)
Then I write a code below,
#include <iostream>
using namespace std;
int c = 6;
int f()
{
c+=1;
return c;
}
int main()
{
int i = 0;
cout <<"i="<<i<<" i++="<<i++<<" i--="<<i--<<endl;
i = 0;
printf("i=%d i++=%d i--=%d\n" , i , i++ ,i-- );
cout<<f()<<" "<<f()<<" "<<f()<<endl;
c = 6;
printf("%d %d %d\n" , f() , f() ,f() );
system("pause");
return 0;
}
Under VS2005, the output is
i=0 i++=-1 i--=0
i=0 i++=-1 i--=0
9 8 7
9 8 7
Under g++( (GCC) 3.4.2 (mingw-special)), the output is,
i=0 i++=0 i--=1
i=0 i++=-1 i--=0
9 8 7
9 8 7
It seems that the buffer is like a stack. However, I read C++ Primer Plus today, and it is said that the cout work from left to right, every time return an object(cout), so "That’s the feature that lets you concatenate output by using insertion". But the from left to right way can not explain cout< output 9 8 7
Now I'm confused about how cout's buffer work, can somebody help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
的输出:
未指定。 这是 C++ 的一个常见陷阱:参数求值顺序未指定。
cout 情况并非如此:它使用链式调用(序列点),而不是单个函数的参数,因此评估顺序从左到右定义良好。
编辑: David Thornley 指出上述代码的行为实际上是 未定义。
The output of:
is unspecified. This is a common pitfall of C++: argument evaluation order is unspecified.
Not so with the cout case: it uses chained calls (sequence points), not arguments to a single function, so evaluation order is well defined from left to right.
Edit: David Thornley points out that the behavior of the above code is in fact undefined.
这不是一个错误,也与输出缓冲无关。
当
i--
和i++
操作作为同一函数调用的参数被多次调用时,它们的执行顺序未定义。为了详细说明(并可能纠正)Iraimbilanja 提到的“序列点”,
cout
版本相当于:实际上,它实际上是三个独立的函数调用,每个函数的参数都按顺序求值,即使它是写得像一个单一的声明。
<<
运算符实际上是ostream& 操作符<<(ostream& os, int)
,所以另一种编写方式是:由于对于外部调用,它没有(据我所知)定义了两个参数的评估顺序,因此完全有可能是正确的-手“c”参数(或在您的情况下为“
i--
”)在评估左手参数之前发生。This is not a bug, nor is it anything to do with output buffering.
The order of execution of the
i--
andi++
operations is not defined when they're invoked more than once as parameters to the same function call.To elaborate (and possibly correct) Iraimbilanja's mention of "sequence points", the
cout
version is equivalent to:Effectively, it's actually three separate function calls, each of whose parameters are evaluated in order, even though it's written like a single statement.
The
<<
operator is reallyostream& operator<<(ostream& os, int)
, so another way of writing this is:Since for the outer call it's not (AFAIK) defined which order the two parameters are evaluated, it's perfectly possible for the right-hand "c" parameter (or in your case "
i--
") to happen before the left hand parameter is evaluated.如果可能的话尝试更新到 gcc >= 4。我刚刚在 4.0.1 上运行了这个,它执行得很好。
if possible try updating to gcc >= 4. i just ran this on 4.0.1 and it executes just dandy.