Thread: You know... I still don't get buffers.

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079

    You know... I still don't get buffers.

    Call me dense, because I just don't get this.

    Don't get me wrong, I understand the concept of buffered and unbuffered output completely. Conceptually, it's simple. When will I ever see it in practice though? To put it another way... when is cout not flushed?

    For a long time I've been looking for a visual example that depicts the difference between cout and cerr. I've written some test programs trying to show the difference, but to no avail.

    So does anyone have a good example of cout not outputting where cerr would? Right now the only thing I can think of is writing multiple threads and hope one of the threads fails, crashes the program, right in the middle of a cout out statement. I don't know. My head hurts.
    Last edited by SlyMaelstrom; 06-14-2006 at 04:53 PM.
    Sent from my iPadŽ

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No need for multiple threads. Simply write to cout without a flush or endl, then crash the program immediately afterwards. The stuff you wrote to cout might not be displayed.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    cout and cerr are bound by default to the same output device, the console. However, while cout writes to the standard output, cerr writes to the standard error.

    Maybe it helps to remember that cout and cerr are objects. They both are of ostream type. But cerr is meant as an abstraction of an error message being output to some stream, while cout is meant as an abstraction of a normal output.

    A common usage of the cerr object is probably to bind it to an ofstream for in-file logging of error message. However, you can do the exact same thing with a cout object. However cerr is a more... moral choice.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I had a problem that illustrated this. I posted it here. Just add a selection_sort function that produces a segmentation fault and you should see the behavior you're looking for.
    There is a difference between tedious and difficult.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Oh... there's another (big) difference between cerr and cout. When you redirect your output to a file, cerr is still sent to ostream, despite being also sent to the ofstream. Which means cerr is still sent to your console.

    But they behave exactly the same way otherwise.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Code:
    #include <iostream>
    using namespace std;
    
    int main ( ) {
      cout << "hello ";
      cerr << "this is an error"; // appears before hello if cout isn't flushed
      cout << "world" << endl;
      return 0;
    }
    If run without any redirection, we get
    $ ./a.exe
    this is an errorhello world

    Redirecting stdout, we only see stderr on console
    $ a.exe 1> f1.txt
    this is an error

    Redirecting stderr, we only see stdout on console
    $ a.exe 2> f2.txt
    hello world
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I can't manage to get that result. Compiled under MinGW and VS2005's compiler, I get "hello this is an errorworld", which ironically describes exactly what I'm feeling right now.

    I get the idea of what *should* happen, but I just can't manage to see it happen.
    Sent from my iPadŽ

  8. #8
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by SlyMaelstrom
    I can't manage to get that result. Compiled under MinGW and VS2005's compiler, I get "hello this is an errorworld", which ironically describes exactly what I'm feeling right now.

    I get the idea of what *should* happen, but I just can't manage to see it happen.
    After you have compiled and linked program, go to console (Run->Cmd in windows) go to directory where your .exe file is and the type:
    name.exe
    name.exe > output.txt
    name.exe 2> output.txt

    and you'll see the difference.

    Salem, can you explain what "2>" means in redirection and why this behaves in this way?
    Last edited by Micko; 06-15-2006 at 04:40 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I give up
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ok, I see it now. Thanks

    ...and I'd imagine 2 is referring to the file descriptor. 2 I believe is stderr. So when you pipe with 2> you pipe stderr to the file and the rest prints to the console. Though it appears Windows also lets you pipe to 0 and 1, which I would imagine one of those is stdin. :|

    Anyway, I was almost having trouble figuring this out because of the response the program was giving me and tricked me into thinking the command prompt was having a problem

    Quote Originally Posted by cmd.exe
    C:\> coutblah > output.txt
    this is an error // What the hell kind of error!!! Arg!!!
    Then I checked the file to see the rest.

    I still don't get the output, "this is an errorhello world", which displeases me because I assume it means for some reason something in my PC is set wrong and decided that stdout is just as important as stderr and either doesn't buffer either or buffers both. Or maybe it just means the operator<<(&cerr, "this is an error") flushes the stdout.... whatever.

    Anyway, thanks everyone. I can see it now.
    Last edited by SlyMaelstrom; 06-15-2006 at 04:55 AM.
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Strictly speaking, cout does not need to be physically buffered. As described in the standard, the difference between cout and cerr (apart from the fact they are different streams) is that cerr.flags() & unitbuf is non-zero while cout.flags() & unitbuf is zero. The literal meaning of the unitbuf flag being set is that the stream will be flushed on each output operation. However, if the unitbuf flag is zero, it is up to the implementer as to when (or if) flushing is necessary. This means that cout is not necessarily physically buffered.

    One other characteristic, of some libraries, is that cout is buffered, but flushed whenever output to cerr (and sometimes clog) occurs --- not just when something is read from cin. This is a nicety by some library implementers, and the intent is to ensure that messages to a console are coherent.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    When you include iostream, cerr, cin and cout are automatically created and cout and cerr are tied to cin. So your cout and cerr are only flushed automatically one one of these 3 situations

    1. function normal termination
    2. buffer is full
    3. cin is used (exactly because it is tied to cout and cerr)

    You can control the 3rd situation with the use of the tie() member function.
    Check on streambuf class for ways to deal with the 2nd
    Pass a reference to a buffer or return a reference to a buffer to avoid the 1st
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Salem, can you explain what "2>" means in redirection and why this behaves in this way?
    Exactly as SlyMaelstrom deduced, it's the descriptor of the stream you want to redirect.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I understand now, thanks
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buffers , heaps , stacks ...
    By BlaX in forum Tech Board
    Replies: 9
    Last Post: 02-17-2009, 03:09 PM
  2. Buffers , heaps , stacks ...
    By BlaX in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 01:11 PM
  3. Reading in 16 and 24-bit audio data into (32-bit) integer buffers
    By theblindwatchma in forum C Programming
    Replies: 2
    Last Post: 04-13-2008, 11:12 PM
  4. winsock internal buffers
    By X PaYnE X in forum Networking/Device Communication
    Replies: 7
    Last Post: 05-16-2005, 04:25 AM
  5. Copy to Buffers
    By egomaster69 in forum C Programming
    Replies: 3
    Last Post: 12-05-2004, 06:50 PM