What Is the C++ Iostream Endl Fiasco

What is the C++ iostream endl fiasco?

(I assume) He just means that many, especially new, C++ programmers use std::endl blindly instead of '\n' for newline, flushing unnecessarily frequently and potentially making the performance of their program abysmal.

I.e., most people are taught that std::endl is the canonical way to insert a newline into a stream even though it is very rarely necessary or appropriate to flush it.

It is some people's opinion (*cough*) that std::endl shouldn't even be in the standard, as it is so rarely appropriate and not a significant typing savings over '\n' << std::flush anyway.


TL;DR(s):

  • In the end, std::endl buys you nothing except usually worse performance and usually more typing.
  • It has its uses, but they are considerably rarer than its frequency of use in most large codebases would suggest, therefore...
  • Its utility is highly questionable and its ubiquity is absurd – a fiasco indeed!

Why some examples are using endl instead of \n?

Oftentimes, direct usage of std::cout is something not meant for production code, where it's usually preferred to use some kind of logging facility (for writing to a file instead of standard stream etc.). Within such logging library, you would want to take care you only flush when desired, but for some ad hoc, debugging-like output of stuff, the performance implications of std::endl don't really matter and it's more important to immediately see the desired output.

Why use endl when I can use a newline character?

endl appends '\n' to the stream and calls flush() on the stream. So

cout << x << endl;

is equivalent to

cout << x << '\n';
cout.flush();

A stream may use an internal buffer which gets actually streamed when the stream is flushed. In case of cout you may not notice the difference since it's somehow synchronized (tied) with cin, but for an arbitrary stream, such as file stream, you'll notice a difference in a multithreaded program, for example.

Here's an interesting discussion on why flushing may be necessary.

Is it necessary for endl to flush buffer?

if a new line is inserted, then the buffer will be flushed automatically

Not necessarily, and not for all streams. It's common for std::cout (and the standard output stream it sends data to) to be line-buffered, but not universal. Some implementations, for example, only line-buffer stdout if output is going to a terminal, and fully-buffer it otherwise (if output is redirected to a pipe, for example).

And that's just for standard output. Other streams like file I/O are almost always fully-buffered and the buffer will only be flushed when it either fills up or its flushed explicitly.

Using '\n' instead of endl affects output, why?

Your assumption that an newly created array on the stack is initialized with zero does not hold. Therefore your output is random. Changing the program reorders the memory layout and so you see different results.

int main() {
int i[5];
for (int k = 0; k < 5; ++k)
i[k] = 0;
cout << i[4] << '\n';
}

should give you always the expected result regardless of using '\n' or endl

C++ Vector giving incredibly unexpected value

You have another cout without an endl or \n somewhere in your code that you forgot to remove. That one prints the 700, while the 70 is the correct output of the last line in your code sample.

Change your last line to std::cout<< " and loc[3] is: " << loc[3] << std::endl; to see that my wild guess is right, then go hunting for that other cout.

Just starting off with C++, not registering command

The fact that iostream has a red squiggle underneath it is a near certainty that you have something wrong with your environment (such as compiling with a C compiler rather than a C++ one, for example).

You need to fix that, since cout and endl are defined in that header. I'd start by hovering the mouse over the iostream text and see what the tooltip shows you.

If it cannot find the file iostream then you're either not using a C++ compiler, or your environment is severely damaged.

Either way, it's not a correct C++ environment.

Things to look in to are (to start with):

  • Examine the file extension. Using *.c instead of *.cpp may use a C compiler rather than a C++ one, for example).
  • Examine the output of your compilation, if available. You will hopefully be able to tell which compiler is being used.

If you are sure you're using a C++ compiler:

  • You might have a funny character in your iostream string. You could totally delete that line and retype it (don't edit, it may not get rid of the funny character).
  • Try a different header (like cstdlib) to see if it has the same problem.
  • Last-straw solution would be re-installation of your development environment, in case things are so damaged it's unrecoverable.


Related Topics



Leave a reply



Submit