Confused about the default buffer size of stdout
#include<iostream>
using namespace std;
int main()
{
for(int i = 0 ; i < 10; i++)
{
printf("1 ");
}
while(1);
}
Is stdout
use line buffered
in default? If so, we will not see ten 1
on the console when execute the code above. What confused me is:
On windows system with gcc: the 1
is printed immediately.
On ubuntu system with gcc: the 1
is not printed.
I use cout<<stdout->_bufsiz
to check the buffer size on windows, it is 0
, does it mean that the stdout on windows is unbuffered
in default?
cout<<stdout->_bufsiz
is not work on ubuntu, how can I get the buffer size of stdout ?
When I replace while(1);
with getchar();
, 1
is printed immediately both on windows and ubuntu, why? getchar();
flush the stdout buffer ?
Thanks.
c++ io buffer stdout
add a comment |
#include<iostream>
using namespace std;
int main()
{
for(int i = 0 ; i < 10; i++)
{
printf("1 ");
}
while(1);
}
Is stdout
use line buffered
in default? If so, we will not see ten 1
on the console when execute the code above. What confused me is:
On windows system with gcc: the 1
is printed immediately.
On ubuntu system with gcc: the 1
is not printed.
I use cout<<stdout->_bufsiz
to check the buffer size on windows, it is 0
, does it mean that the stdout on windows is unbuffered
in default?
cout<<stdout->_bufsiz
is not work on ubuntu, how can I get the buffer size of stdout ?
When I replace while(1);
with getchar();
, 1
is printed immediately both on windows and ubuntu, why? getchar();
flush the stdout buffer ?
Thanks.
c++ io buffer stdout
The variablestdout
have nothing to do withstd::cout
. Thestdout
variable is used by the old C functions likeprintf
, while the C++ streams uses their own buffers. There is no way of getting the buffer size of the underlyingbasic_streambuf
used by C++ streams.
– Some programmer dude
May 13 '14 at 13:19
I replacecin
withprintf
, it is still the same problem. @JoachimPileborg
– tenos
May 13 '14 at 13:32
add a comment |
#include<iostream>
using namespace std;
int main()
{
for(int i = 0 ; i < 10; i++)
{
printf("1 ");
}
while(1);
}
Is stdout
use line buffered
in default? If so, we will not see ten 1
on the console when execute the code above. What confused me is:
On windows system with gcc: the 1
is printed immediately.
On ubuntu system with gcc: the 1
is not printed.
I use cout<<stdout->_bufsiz
to check the buffer size on windows, it is 0
, does it mean that the stdout on windows is unbuffered
in default?
cout<<stdout->_bufsiz
is not work on ubuntu, how can I get the buffer size of stdout ?
When I replace while(1);
with getchar();
, 1
is printed immediately both on windows and ubuntu, why? getchar();
flush the stdout buffer ?
Thanks.
c++ io buffer stdout
#include<iostream>
using namespace std;
int main()
{
for(int i = 0 ; i < 10; i++)
{
printf("1 ");
}
while(1);
}
Is stdout
use line buffered
in default? If so, we will not see ten 1
on the console when execute the code above. What confused me is:
On windows system with gcc: the 1
is printed immediately.
On ubuntu system with gcc: the 1
is not printed.
I use cout<<stdout->_bufsiz
to check the buffer size on windows, it is 0
, does it mean that the stdout on windows is unbuffered
in default?
cout<<stdout->_bufsiz
is not work on ubuntu, how can I get the buffer size of stdout ?
When I replace while(1);
with getchar();
, 1
is printed immediately both on windows and ubuntu, why? getchar();
flush the stdout buffer ?
Thanks.
c++ io buffer stdout
c++ io buffer stdout
edited May 13 '14 at 13:38
tenos
asked May 13 '14 at 13:15
tenostenos
3961513
3961513
The variablestdout
have nothing to do withstd::cout
. Thestdout
variable is used by the old C functions likeprintf
, while the C++ streams uses their own buffers. There is no way of getting the buffer size of the underlyingbasic_streambuf
used by C++ streams.
– Some programmer dude
May 13 '14 at 13:19
I replacecin
withprintf
, it is still the same problem. @JoachimPileborg
– tenos
May 13 '14 at 13:32
add a comment |
The variablestdout
have nothing to do withstd::cout
. Thestdout
variable is used by the old C functions likeprintf
, while the C++ streams uses their own buffers. There is no way of getting the buffer size of the underlyingbasic_streambuf
used by C++ streams.
– Some programmer dude
May 13 '14 at 13:19
I replacecin
withprintf
, it is still the same problem. @JoachimPileborg
– tenos
May 13 '14 at 13:32
The variable
stdout
have nothing to do with std::cout
. The stdout
variable is used by the old C functions like printf
, while the C++ streams uses their own buffers. There is no way of getting the buffer size of the underlying basic_streambuf
used by C++ streams.– Some programmer dude
May 13 '14 at 13:19
The variable
stdout
have nothing to do with std::cout
. The stdout
variable is used by the old C functions like printf
, while the C++ streams uses their own buffers. There is no way of getting the buffer size of the underlying basic_streambuf
used by C++ streams.– Some programmer dude
May 13 '14 at 13:19
I replace
cin
with printf
, it is still the same problem. @JoachimPileborg– tenos
May 13 '14 at 13:32
I replace
cin
with printf
, it is still the same problem. @JoachimPileborg– tenos
May 13 '14 at 13:32
add a comment |
2 Answers
2
active
oldest
votes
The question is, which buffer do you mean?
- the ostream buffer?
- the buffer of the file stream under the STDOUT file descriptor?
- the buffer beneath that in whatever device STDOUT happens to be built on?
If it's 1 I think you're doing the right thing.
If it's 2, then you're out of luck. see the documentation of setbuf() here http://man7.org/linux/man-pages/man3/setbuf.3.html as far as I know there is no getbuf equivalent. (can someone correct me here?)
If it's 3 then I think you're really out of luck.
I mean the stdout buffer when I callprintf
– tenos
May 13 '14 at 13:48
add a comment |
On linux you can do these to flush the buffer :
cout << "1 " << "n";
or
cout << "1 " << flush;
1
that doesn't answer the question
– spiritwolfform
May 13 '14 at 13:26
The first comment on the question answer it. Here I add a work around for Linux.
– AMDG
May 13 '14 at 13:28
cout<<"n"
doesn't flush the buffer.cout<<endl
does. And you didn't answer my qustion @AMDG
– tenos
May 13 '14 at 13:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23632340%2fconfused-about-the-default-buffer-size-of-stdout%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The question is, which buffer do you mean?
- the ostream buffer?
- the buffer of the file stream under the STDOUT file descriptor?
- the buffer beneath that in whatever device STDOUT happens to be built on?
If it's 1 I think you're doing the right thing.
If it's 2, then you're out of luck. see the documentation of setbuf() here http://man7.org/linux/man-pages/man3/setbuf.3.html as far as I know there is no getbuf equivalent. (can someone correct me here?)
If it's 3 then I think you're really out of luck.
I mean the stdout buffer when I callprintf
– tenos
May 13 '14 at 13:48
add a comment |
The question is, which buffer do you mean?
- the ostream buffer?
- the buffer of the file stream under the STDOUT file descriptor?
- the buffer beneath that in whatever device STDOUT happens to be built on?
If it's 1 I think you're doing the right thing.
If it's 2, then you're out of luck. see the documentation of setbuf() here http://man7.org/linux/man-pages/man3/setbuf.3.html as far as I know there is no getbuf equivalent. (can someone correct me here?)
If it's 3 then I think you're really out of luck.
I mean the stdout buffer when I callprintf
– tenos
May 13 '14 at 13:48
add a comment |
The question is, which buffer do you mean?
- the ostream buffer?
- the buffer of the file stream under the STDOUT file descriptor?
- the buffer beneath that in whatever device STDOUT happens to be built on?
If it's 1 I think you're doing the right thing.
If it's 2, then you're out of luck. see the documentation of setbuf() here http://man7.org/linux/man-pages/man3/setbuf.3.html as far as I know there is no getbuf equivalent. (can someone correct me here?)
If it's 3 then I think you're really out of luck.
The question is, which buffer do you mean?
- the ostream buffer?
- the buffer of the file stream under the STDOUT file descriptor?
- the buffer beneath that in whatever device STDOUT happens to be built on?
If it's 1 I think you're doing the right thing.
If it's 2, then you're out of luck. see the documentation of setbuf() here http://man7.org/linux/man-pages/man3/setbuf.3.html as far as I know there is no getbuf equivalent. (can someone correct me here?)
If it's 3 then I think you're really out of luck.
answered May 13 '14 at 13:41
Richard HodgesRichard Hodges
56.7k658104
56.7k658104
I mean the stdout buffer when I callprintf
– tenos
May 13 '14 at 13:48
add a comment |
I mean the stdout buffer when I callprintf
– tenos
May 13 '14 at 13:48
I mean the stdout buffer when I call
printf
– tenos
May 13 '14 at 13:48
I mean the stdout buffer when I call
printf
– tenos
May 13 '14 at 13:48
add a comment |
On linux you can do these to flush the buffer :
cout << "1 " << "n";
or
cout << "1 " << flush;
1
that doesn't answer the question
– spiritwolfform
May 13 '14 at 13:26
The first comment on the question answer it. Here I add a work around for Linux.
– AMDG
May 13 '14 at 13:28
cout<<"n"
doesn't flush the buffer.cout<<endl
does. And you didn't answer my qustion @AMDG
– tenos
May 13 '14 at 13:43
add a comment |
On linux you can do these to flush the buffer :
cout << "1 " << "n";
or
cout << "1 " << flush;
1
that doesn't answer the question
– spiritwolfform
May 13 '14 at 13:26
The first comment on the question answer it. Here I add a work around for Linux.
– AMDG
May 13 '14 at 13:28
cout<<"n"
doesn't flush the buffer.cout<<endl
does. And you didn't answer my qustion @AMDG
– tenos
May 13 '14 at 13:43
add a comment |
On linux you can do these to flush the buffer :
cout << "1 " << "n";
or
cout << "1 " << flush;
On linux you can do these to flush the buffer :
cout << "1 " << "n";
or
cout << "1 " << flush;
answered May 13 '14 at 13:19
AMDGAMDG
783821
783821
1
that doesn't answer the question
– spiritwolfform
May 13 '14 at 13:26
The first comment on the question answer it. Here I add a work around for Linux.
– AMDG
May 13 '14 at 13:28
cout<<"n"
doesn't flush the buffer.cout<<endl
does. And you didn't answer my qustion @AMDG
– tenos
May 13 '14 at 13:43
add a comment |
1
that doesn't answer the question
– spiritwolfform
May 13 '14 at 13:26
The first comment on the question answer it. Here I add a work around for Linux.
– AMDG
May 13 '14 at 13:28
cout<<"n"
doesn't flush the buffer.cout<<endl
does. And you didn't answer my qustion @AMDG
– tenos
May 13 '14 at 13:43
1
1
that doesn't answer the question
– spiritwolfform
May 13 '14 at 13:26
that doesn't answer the question
– spiritwolfform
May 13 '14 at 13:26
The first comment on the question answer it. Here I add a work around for Linux.
– AMDG
May 13 '14 at 13:28
The first comment on the question answer it. Here I add a work around for Linux.
– AMDG
May 13 '14 at 13:28
cout<<"n"
doesn't flush the buffer. cout<<endl
does. And you didn't answer my qustion @AMDG– tenos
May 13 '14 at 13:43
cout<<"n"
doesn't flush the buffer. cout<<endl
does. And you didn't answer my qustion @AMDG– tenos
May 13 '14 at 13:43
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23632340%2fconfused-about-the-default-buffer-size-of-stdout%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The variable
stdout
have nothing to do withstd::cout
. Thestdout
variable is used by the old C functions likeprintf
, while the C++ streams uses their own buffers. There is no way of getting the buffer size of the underlyingbasic_streambuf
used by C++ streams.– Some programmer dude
May 13 '14 at 13:19
I replace
cin
withprintf
, it is still the same problem. @JoachimPileborg– tenos
May 13 '14 at 13:32