endl or \n ?

This is a discussion on endl or \n ? within the C++ Programming forums, part of the General Programming Boards category; Hi All I have searched quite a few FAQ`s, C/C++ Reference site and the FAQ`s here and I`m still sort ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    23

    endl or \n ?

    Hi All

    I have searched quite a few FAQ`s, C/C++ Reference site and the FAQ`s here and I`m still sort of lost about endl and \n. I know that both are used to end a line. That \n is quicker. That (from what some suggest) endl flushes the buffer and sticks a \n at the end of the line so as to start a new one. However, I can`t grasp flushing the buffer. I know it is important to keep from hogging up memory but I can`t get my mind around the buffer and its importance in helping memory or giving extra space to the memory (as I think the term suggests) in case something doesn`t fit like it should.

    Can someone lay it out for me?

    And having answered that question could you tell me generally, the circumstances under which I should be posting endl to my code as opposed to \n?

    Thanks All, ..Plain

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    endl - \n

    Number 6 is a good reference as to when one should use endl

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Use of a buffer speeds things up because it is generally a slow operation to send data to a console window, or file, or any other output device. Instead of sending one character at a time, the characters are stored up in a buffer and sent as a group. Flushing the buffer merely ensures that anything you are attempting to write to your output device is actually written instead of being saved for later. It really isn't about memory savings, it is about not running an expensive operation more than necessary.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can someone lay it out for me?
    It's faster to copy to an array in memory than to read or write from a physical device. The point of a buffer is to avoid the expensive read or write until the last possible moment (ie. the user wants to flush the buffer, or the buffer becomes full). That's all there is to it.

    >could you tell me generally, the circumstances under which I should be posting endl to my code as opposed to \n?
    Use endl when you don't expect the buffer to be flushed automatically and need it to be. User prompts don't count most of the time because you're going to request input anyway and cin is tied to cout; any request for input flushes the tied output buffer. Error messages don't count as long as you use cerr, which is unit buffered and flushes automatically every time.

    endl is typically most useful with non-user viewable streams, such as a file stream. One example would be error handling. You're doing some kind of processing and then writing to a file stream. An error occurs in the processing that requires termination. This is a case where you should flush the buffer before terminating. Though in that case an explicit call to flush would be better than endl unless you need to write a newline for some reason. Another example would be a protected write situation. You know the write will work, but the next operation, while unrelated, is highly suspect for causing program meltdown. It would be wisest to make sure that the write succeeds and the buffer is flushed, just in case.

    In my experience, endl is rarely needed and terribly overused.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    Thank you all. This has helped quite a bit. I appreciate the time you took to reply.
    ..Plain

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In the case of protected write (usually used for log files), it's more comfortable to just set the unitbuf flag and have the stream flush itself after every operation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. endl - \n
    By Hugo716 in forum C++ Programming
    Replies: 8
    Last Post: 05-25-2006, 03:33 PM
  2. endl
    By Mick D in forum C++ Programming
    Replies: 7
    Last Post: 10-20-2005, 01:54 AM
  3. Endl - Undeclared identifier.
    By MipZhaP in forum C++ Programming
    Replies: 9
    Last Post: 03-03-2005, 11:01 AM
  4. endl vs \n
    By Chewbacca in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2002, 01:42 PM
  5. "\n" or endl
    By itld in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2001, 01:05 AM

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