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 Worldn";
My questions are:
- Is it more appropriate to use the stream manipulator (endl) in a certain
situation and an escape character in a different one? - Are there drawbacks efficiency wise to using one of the two?
- Are they completely interchangeable?
- 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?
- 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.
1
o << std::endl
is equivalent to the following code:
o.put(o.widen('n'));
o.flush();
In other words, you should only use std::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 std::endl
. The extra calls to flush
can hurt performance (sometimes significantly).
For more details, see cppreference.com.
Regarding memory usage: Worrying about n
versus std::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 std::endl
is translated by the compiler into (probably inlined) function calls to put
and flush
.
Platform-specific differences in line endings (Windows rn
versus Linux and OS X n
) are handled at a lower level than std::endl
and 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 ton
. - If a stream is opened in binary mode, then they pass any line endings through verbatim, and it’s up to you.
- For
std::cout
andstd::cin
in particular, they’re treated as if they’re text mode.
1) For portability, use Edit: As per comments, line system specific endings are handled at a lower level.endl
. Windows newlines are rn
, 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
. However, it’ll decrease performance.
Edit: Clear up some mistakes.
3