Buffering

Hey guys,

so 2nd question of the day and sticking to the common theme of I/O, why does cout and cin use output and input buffers?

so I understand the benefits of using buffers, lets say we want to write to disk it may take 1ms, and to write to memory it may take 0.5 ms, so lets say we had a character that is 50 bytes in size( or 50 chars ) if we were to write that character to disk every time lets say we received a character from the user this would take 50*1 ms or 50ms, but if we were to have a buffer in ram and only write to disk let say when the buffer reaches 50 characters this would save us a lot of time, so essentially it would be 50*0.5ms +1 = 26ms almost half the time.

but why would cout and cin be buffered? I can't see the benefits, I have read that it is due to context switching and a high amount of system calls would that be right to say??


and this brings me on to my next question and one that I have experimented with, so the c++ standard library's I/O and C's stdlib is different, maybe C++ uses a larger buffer? anyway ios::sync_with_stdio(false); allows you to turn of syncing the std I/O with the cstdlib, I did this and this was my results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    //fprintf(stdout, "hi\n");

    ios::sync_with_stdio(false);

    cout << "hey";
    cout << "hahaha ";
    cout << "hahaha ";
    cout << "hahaha ";
    cout << "hahaha ";
    cout << "hahaha ";
    cout << "hahaha ";
    cout << "hahaha ";
    fprintf(stdout, " \n hello from stdio\n");
    cout << "hahaha ";
    cout << "hahaha " << endl;

    return 0;
}




 hello from stdio
heyhahaha hahaha hahaha hahaha hahaha hahaha hahaha hahaha hahaha



as you can see endl flushes the c++ I/O buffer hence hello from stdio is printed before anything from cout, but we could turn this on ( by default it is ) and it will print the following in order,

so does C and C++ or should I say the compilers use their own in built data buffers separate from one another?

does allowing the syncing between the two actually cause them to share the same buffer?

does "\n" flush the buffer also or does C not buffer output at all? ( since it was written first)

thanks
https://stackoverflow.com/questions/10518608/buffered-and-unbuffered-stream

(Most times your questions are already answered somewhere on the internet, so doing a search might be more worthwhile.)

My DDG meta-search ("c++ why are streams buffered"):

https://duckduckgo.com/?q=c%2B%2B+why+are+streams+buffered&t=ffsb&ia=web
True FurryGuy, I always do research and look for the answer before posting but I like to expand on them, if there is something I don't understand from the sources I've read online , I ask to further cement and also discuss why something is, or the smallest nuances, in some cases the sources don't actually explain or go into detail on certain aspects of topics.

but yes indeed some of those sub questions are probably answered. I feel sometimes also discussing and opening a thread allows people to join in and also discuss their opinions etc.
Last edited on
Topic archived. No new replies allowed.