cout flushes buffer without endl , ends or flush

i know that cout doesn't flush its buffer unless the function terminates or we use cin or we use manipulator like endl , ends , unitbuf or flush.

but when i write this code in visual studio 2015 cout flushes the buffer
but using g++ it doesn't flush the buffer
how to make cout in visual c++ not flush the buffer?
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>

using namespace std;

int main() {
	cout << "test1";
	cout << "test2";
	for (int i = 0; i < 100000000; i++);

	system("pause");
	return 0;
}
Last edited on
I know that std::cout doesn't flush its buffer...
Not so -- std::cout can flush its buffer at any time.

Using g++ it doesn't flush the buffer

Since your program produces output, std::cout is obviously flushed at some point (it happens when your program terminates, at least).

The stream manipulators std::flush, and std::endl cause the stream to flush; std::ends does not. std::unitbuf enables automatic flushes after every output operation.

Why do you want to avoid flushing? What do you expect as output?
Last edited on
thanks for replying mbozzi;

i read in c++ primer book :

There are two other similar manipulators: flush and ends. flush flushes the stream but adds no characters to the output; ends inserts a null character into the buffer and then flushes it:
1
2
3
cout << "hi!" << endl; // writes hi and a newline, then flushes the buffer 
cout << "hi!" << flush; // writes hi, then flushes the buffer; adds no data
cout << "hi!" << ends; // writes hi and a null, then flushes the buffer 



and i tried "cout << "hi!" << ends;" using g++ and it doesn't flush as you said.

iam now confused;

and i read also in the same book :
 
cout << "please enter a value: ";


the literal string might be printed immediately, or the operating system might store the data in a buffer to be printed later. There are several conditions that cause the buffer to be flushed—that is, to be written—to the actual output device or file:
• The program completes normally. All output buffers are flushed as part of the return from main.
• At some indeterminate time, the buffer can become full, in which case it will be flushed before writing the next value.
• We can flush the buffer explicitly using a manipulator such as endl (§ 1.2, p. 7).
• We can use the unitbuf manipulator to set the stream’s internal state to empty the buffer after each output operation. By default, unitbuf is set for cerr, so that writes to cerr are flushed immediately.
• An output stream might be tied to another stream.


he didn't say that cout may flush the buffer at any time.

are these mistakes by the author ?
Are these mistakes by the author ?

The only error I see is that ends doesn't unconditionally flush the stream buffer:
http://eel.is/c++draft/ostream.manip#3
http://en.cppreference.com/w/cpp/io/manip/ends#Notes

Instead, the stream will be flushed only if another criteria is met, i.e., according to the normal rules.

I wrote the stream could be flushed "at any time", but Lippman et al. was more precise:
• At some indeterminate time, the buffer can become full, in which case it will be flushed before writing the next value.
Last edited on
thanks for your efforts to help me.

i want just to know why visual c++ compiler flushes the buffer immediately but g++ doesn't
the buffer isn't full;it's the first line in my code and the function didn't terminate or i used any manipulator.

thanks again.
Have you tried Debug vs. Release mode? Maybe it makes a difference.
i want just to know why visual c++ compiler flushes the buffer immediately but g++ doesn't
the buffer isn't full;it's the first line in my code and the function didn't terminate or i used any manipulator.

IIRC, the default buffering behavior of cout depends on the underlying behavior for the C stream stdout to which it is tied.

1
2
3
4
5
6
7
8
9
10
11
#include <chrono>
#include <cstdio>
#include <iostream>
#include <thread>

int main() {
    std::setvbuf(stdout, nullptr, _IOFBF, BUFSIZ);
    std::cout << "test1";
    std::cout << "test2";
    std::this_thread::sleep_for(std::chrono::seconds(10));
}


http://en.cppreference.com/w/cpp/io/c/setvbuf

Thanks to @cire, I found this link, which claims:
... The standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device. Since we don't have line buffering, we must default to no buffering.

https://connect.microsoft.com/VisualStudio/feedback/details/642876/std-wcout-is-ten-times-slower-than-wprintf-performance-bug-in-c-library

Edit:
Referring to MS Windows
Last edited on
Topic archived. No new replies allowed.