I work in Ubuntu 14.04 x86, Sublime Text 2 + terminal

Strangeness was highlighted during the output of the line before a long operation. I used the allocation of a large amount of memory:

MyType* type; long int MyNumber = 3000000; cout << "Initialization... "; MyType* type = new MyType[MyNumber]; cout << "Done! << endl; 

By all logic, the terminal output should be as follows:

 Initialization...*пауза* Done! 

But for some reason, the pause passes at the beginning, and two lines are output simultaneously.

 *пауза* Initialization... Done! 

After adding to the output of the first line of the endl command endl first inscription works as it should.

 Initialization... *пауза* Done! 

What is so special about the terminal?

    1 answer 1

    This is not a terminal feature, but a property of all buffered output streams. Until the flush() method is called, or the stream is not received \n - data from the buffer will not be transferred to the device.

    • one
      @klopp: As far as I understand, \n does not produce flush, but std::endl produces. - VladD
    • And, something already began to forget - cout, is it line buffered or fully buffered? - user6550
    • @VladD, no. By \n real output is carried out. cout << "xaxa \ n ..."; sleep (2); cout << "xoxo \ n"; displays with a delay before ... hoho avp @ avp-xub11: hashcode $ ./a.out xaxa ... xoxo avp @ avp-xub11: hashcode $ (you can’t see it here) - avp
    • @avp: This is because stdout in your case is line-buffered. < en.cppreference.com/w/cpp/io/manip/endl >:> std::endl > Inserts a newline character into the output sequence os.put(os.widen('\n')) followed by os.flush() . >> ...>> In many implements, standard output is line-typing, std::cout.sync_with_stdio(false) was executed. - VladD
    • one
      @VladD, here's how even ... std :: endl turns out to be how fflush() works (i.e. the output will be pushed out not only with the character stream). That's why (too many subtleties, not to mention more fundamental flaws), I also do not advise writing anything more or less serious on crosses . - avp