Non-Subspace Related Coding - Is there any MAJOR difference between \n and endl? Quan Chi2 - Sun Aug 27, 2006 5:01 pm Post subject: Is there any MAJOR difference between \n and endl?
Is there any difference between \n and endl? (Im talking about C++ btw.)
I don't really know if there is a difference that I should know about.
Anyone know if they are pretty much the same thing or not?
Solo Ace - Sun Aug 27, 2006 5:19 pm Post subject:
An std::endl would probably be printed as \n (or \r\n, no idea).
The major difference between those is (as I recall) that the endl causes the output stream to be flushed.
Simply explained, the output from your program is 'stored' in a buffer. This buffer is used to store the data temporarily until a specific ammount of data is collected. Then it is sent to its next destination.
The endl causes this buffer to be flushed.
It might be useful to use output buffering, it's efficient for larger pieces of data. But it only wastes time and memory if you want to print a bunch of lines and flush the output buffer every time.
I think that's it. And if someone thinks I'm wrong, please correct me. My C++ book doesn't say anything about this, they simply use \n and say endl is the same.
Quan Chi2 - Sun Aug 27, 2006 6:00 pm Post subject:
Oh I see.
My book is the same way.
Thanks for the info.
Dr Brain - Sun Aug 27, 2006 11:30 pm Post subject:
Not when you're just printing strings (Solo may be correct about the buffering, I don't personally know).
The use of endl is more apparent when you're printing things other than strings, and a \n can't easily be added to the end (and endl will be more efficient in those cases than a new string).
Bak - Mon Aug 28, 2006 12:33 am Post subject:
solo ace is correct about the buffering, but i've only noticed it when i was debugging someone's program in linux that was crashing. We added a bunch of print statements and if we didn't use endl or cout.flush(); (?), it wouldn't print them.
Quan Chi2 - Mon Aug 28, 2006 4:44 am Post subject:
That sounds about right. does cout.flush have something to do with prevention of buffer overflows? (im taking a wild guess here. dont be too brutal)
Solo Ace - Mon Aug 28, 2006 5:02 am Post subject:
The only thing flushing the buffer does is make it pass the data to the next destination (the next library or whatever). Then the temporary buffer is cleared.
Uhm, this doesn't really have anything to do with a buffer overflow or the prevention of buffer overflows. For more information on buffer overflows check Wikipedia:Buffer overflow.