Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I don't have a specific context in which I'm asking the question, but while I was reading a beginner book on C++ I noticed the use of both an endl stream manipulator and a newline escape character when dealing with a stream object.

The exmaple is as follows:

cout << "Hello World" << endl;
cout << "Hello World\n";

My questions are:

  1. Is it more appropriate to use the stream manipulator (endl) in a certain situation and an escape character in a different one?
  2. Are there drawbacks efficiency wise to using one of the two?
  3. Are they completely interchangeable?
  4. I read that an escape sequence is stored in memory as a single character. Does that mean it is more appropriate to use endl if you're going for low memory consumption?
  5. Does the stream manipulator endl use up memory in any way, if so is it more than the escape sequence?

Thanks, StackExchange Apologies if I posted this in the wrong section, I thought it counted as data structures.

share|improve this question
1  
endl also causes a flush on some streams, '\n' does not. –  James Jan 28 '13 at 15:39
add comment

2 Answers

up vote 7 down vote accepted

o << endl is equivalent to the following code:

o.put(o.widen('\n'));
o.flush();

In other words, you should only use o << endl when you need to flush the stream. For example:

  • You're about to perform a time-consuming operating and want to make sure you display a message before doing so.
  • Another process is waiting on your output.
  • If more than one thread or process is running, you may need to flush output so that the output from each thread or process is displayed correctly. (Otherwise, you may get seemingly random blocks of output interleaved at awkward intervals.)

If you don't need to flush the stream, then use << '\n' instead of endl. The extra calls to flush can hurt performance (sometimes significantly).

For more details, see cppreference.com.

Regarding memory usage: Worrying about \n versus endl is almost certainly an unnecessary micro-optimization, but in general, I'd expect \n to take less memory. \n is just one more byte at the end of a string literal, while writing endl is translated by the compiler into (probably inlined) function calls to put and flush.

Platform-specific differences in line endings (Windows \r\n versus Linux and OS X \n) are handled at a lower level than endl and o << '\n':

  • If a stream is opened in text mode, then if you write \n, it automatically translates it to the appropriate platform-specific line ending. If you read a platform-specific line ending, the stream automatically translates it to \n.
  • If a stream is opened in binary mode, then they pass any line endings through verbatim, and it's up to you.
  • For cout and cin in particular, they're treated as if they're text mode.
share|improve this answer
add comment

1) For portability, use endl. Windows newlines are \r\n, Linux \n and Mac \r.

2) endl flushes the stream, "\n" does not.

3) Depends on portability.

As to memory usage, you can minimize it by flushing to other storage as often as possible with endl.

share|improve this answer
    
Thanks! Just what I was looking for. –  Net Shift Jan 28 '13 at 15:43
2  
Mac has been unix based since OS X and thus is also \n on any modern machine. –  MichaelT Jan 28 '13 at 15:50
4  
Sorry, but this is incorrect. endl and \n are equivalent with regards to platform-specific line endings; platform differences are handled at a lower level. –  Josh Kelley Jan 28 '13 at 16:19
1  
Your first point is wrong. endl() sends '\n' to the stream (in addition to flushing it). The '\n' character is converted to the platform specific End of line sequence (assuming text mode). The End of line sequence is converted back to '\n` when read from a file. Point 3) is dubious. And the last paragraph is again wrong: Flushing will not save you space and flushing more than required will slow your code (the point of the buffer is to improve write efficiency to a slow device). –  Loki Astari Jan 29 '13 at 0:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.