Author |
Message |
Quan Chi2 Member of "Sexy Teenagers that Code" Group
Age:33 Gender: Joined: Mar 25 2005 Posts: 860 Location: NYC Offline
|
Posted: 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? |
|
Back to top |
|
|
Solo Ace Yeah, I'm in touch with reality...we correspond from time to time.
Age:36 Gender: Joined: Feb 06 2004 Posts: 2583 Location: The Netherlands Offline
|
Posted: 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. |
|
Back to top |
|
|
Quan Chi2 Member of "Sexy Teenagers that Code" Group
Age:33 Gender: Joined: Mar 25 2005 Posts: 860 Location: NYC Offline
|
Posted: Sun Aug 27, 2006 6:00 pm Post subject: |
|
|
|
|
Oh I see.
My book is the same way.
Thanks for the info. |
|
Back to top |
|
|
Dr Brain Flip-flopping like a wind surfer
Age:38 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: 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). _________________ Hyperspace Owner
Smong> so long as 99% deaths feel lame it will always be hyperspace to me |
|
Back to top |
|
|
Bak ?ls -s 0 in
Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: 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. _________________ SubSpace Discretion: A Third Generation SubSpace Client |
|
Back to top |
|
|
Quan Chi2 Member of "Sexy Teenagers that Code" Group
Age:33 Gender: Joined: Mar 25 2005 Posts: 860 Location: NYC Offline
|
Posted: 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) |
|
Back to top |
|
|
Solo Ace Yeah, I'm in touch with reality...we correspond from time to time.
Age:36 Gender: Joined: Feb 06 2004 Posts: 2583 Location: The Netherlands Offline
|
Posted: 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. |
|
Back to top |
|
|
|