Confused about the default buffer size of stdout












2















#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.










share|improve this question

























  • 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


















2















#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.










share|improve this question

























  • 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
















2












2








2








#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.










share|improve this question
















#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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 13 '14 at 13:38







tenos

















asked May 13 '14 at 13:15









tenostenos

3961513




3961513













  • 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





















  • 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



















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














2 Answers
2






active

oldest

votes


















2














The question is, which buffer do you mean?




  1. the ostream buffer?

  2. the buffer of the file stream under the STDOUT file descriptor?

  3. 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.






share|improve this answer
























  • I mean the stdout buffer when I call printf

    – tenos
    May 13 '14 at 13:48



















1














On linux you can do these to flush the buffer :



cout << "1 " << "n";


or



cout << "1 " << flush;





share|improve this answer



















  • 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












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
});


}
});














draft saved

draft discarded


















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









2














The question is, which buffer do you mean?




  1. the ostream buffer?

  2. the buffer of the file stream under the STDOUT file descriptor?

  3. 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.






share|improve this answer
























  • I mean the stdout buffer when I call printf

    – tenos
    May 13 '14 at 13:48
















2














The question is, which buffer do you mean?




  1. the ostream buffer?

  2. the buffer of the file stream under the STDOUT file descriptor?

  3. 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.






share|improve this answer
























  • I mean the stdout buffer when I call printf

    – tenos
    May 13 '14 at 13:48














2












2








2







The question is, which buffer do you mean?




  1. the ostream buffer?

  2. the buffer of the file stream under the STDOUT file descriptor?

  3. 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.






share|improve this answer













The question is, which buffer do you mean?




  1. the ostream buffer?

  2. the buffer of the file stream under the STDOUT file descriptor?

  3. 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.







share|improve this answer












share|improve this answer



share|improve this answer










answered May 13 '14 at 13:41









Richard HodgesRichard Hodges

56.8k658104




56.8k658104













  • 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

















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













1














On linux you can do these to flush the buffer :



cout << "1 " << "n";


or



cout << "1 " << flush;





share|improve this answer



















  • 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














On linux you can do these to flush the buffer :



cout << "1 " << "n";


or



cout << "1 " << flush;





share|improve this answer



















  • 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








1







On linux you can do these to flush the buffer :



cout << "1 " << "n";


or



cout << "1 " << flush;





share|improve this answer













On linux you can do these to flush the buffer :



cout << "1 " << "n";


or



cout << "1 " << flush;






share|improve this answer












share|improve this answer



share|improve this answer










answered May 13 '14 at 13:19









AMDGAMDG

783822




783822








  • 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





    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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma