473,549 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accelerated C++ - Chapter 1, confusion in understanding "flushing the buffer"

this is from mentioned section. i did not understand some things here:

To avoid the overhead of writing in response to each output request, the library
uses the buffer to accumulate the characters to be written, and flushes the buffer,
by writing its contents to the output device, only when necessary"
it means "flushing the buffer" and "writing to output device" are SAME
thing, these are just 2 different names for the same thing.

?

When our program writes its prompt to cout, that output goes into the buffer
associated with the standard output stream. Next, we attempt to read from
cin. This read flushes the cout buffer, so we are assured that our user will
see the prompt"
1st, author is saying that our program writes its /prompt/ to cout and
then he says that /cin/ flushes the buffer and that is why user sees
the prompt.

what does he mean by "our program writes the prompt" and /cin/ helps
in the user to see the prompt ?

i think i can see the prompt every time, whether i use /cout/, cin/ or
whatever.

Our next statement, which generates the output, explicitly instructs the
library to flush the buffer. That statement is only slightly more complicated
than the one that wrote the prompt. Here we write the string literal "Hello, "
followed by the value of the string variable name, and finally by std::endl.
Writing the value of std::endl ends the line of output, and then flushes the
buffer, which forces the system to write to the output stream immediately.
both /cin/ and /cout/ write prompt to output device. i am utterly
confused.

Mar 18 '07 #1
5 2845
* arnuld:
this is from mentioned section. i did not understand some things here:

>To avoid the overhead of writing in response to each output request, the library
uses the buffer to accumulate the characters to be written, and flushes the buffer,
by writing its contents to the output device, only when necessary"

it means "flushing the buffer" and "writing to output device" are SAME
thing, these are just 2 different names for the same thing.

?
Almost. Flushing the buffer involves (1) writing the characters in the
buffer to the output devices, so that they appear visually on your
screen or paper, and (2) emptying the buffer so that these characters
aren't written more times and to make room for later output.

>When our program writes its prompt to cout, that output goes into the buffer
associated with the standard output stream. Next, we attempt to read from
cin. This read flushes the cout buffer, so we are assured that our user will
see the prompt"

1st, author is saying that our program writes its /prompt/ to cout and
then he says that /cin/ flushes the buffer and that is why user sees
the prompt.
Yes.

what does he mean by "our program writes the prompt" and /cin/ helps
in the user to see the prompt ?
Your source code has some statement like

std::cout << "Gimme a number!";

That's "our program writes the prompt".

The effect is just to place the text "Gimme a number!" in the output
buffer. If there had been a newline at the end, or a <<std::endl, then
the buffer would have been flushed at this point. But there isn't, so
the effect is just to accumulate text in the output buffer.

When subsequently the program executes

std::cin >number;

std::cin first tells std::cout to flush its buffer. std::cout flushes
its buffer, and the prompt appears on your screen. std::cin inputs the
number.
i think i can see the prompt every time, whether i use /cout/, cin/ or
whatever.
Nope.

>Our next statement, which generates the output, explicitly instructs the
library to flush the buffer. That statement is only slightly more complicated
than the one that wrote the prompt. Here we write the string literal "Hello, "
followed by the value of the string variable name, and finally by std::endl.
Writing the value of std::endl ends the line of output, and then flushes the
buffer, which forces the system to write to the output stream immediately.

both /cin/ and /cout/ write prompt to output device. i am utterly
confused.
It's std::cout that does the work. But std::cout does it in a lazy way,
accumulating text in its buffer, sort of like std::cout sits on the
toilet accumulating excrement there. Then std::cin knocks on the door
and says, hey, /I/ need to use the toilet now, flush it and get out!

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 18 '07 #2
On Mar 18, 11:59 am, "Alf P. Steinbach" <a...@start.now rote:
* arnuld:
Almost. Flushing the buffer involves (1) writing the characters in the
buffer to the output devices, so that they appear visually on your
screen or paper, and (2) emptying the buffer so that these characters
aren't written more times and to make room for later output.
ok , thanks for *clear* explanation.

what does he mean by "our program writes the prompt" and /cin/ helps
in the user to see the prompt ?

Your source code has some statement like

std::cout << "Gimme a number!";

That's "our program writes the prompt".
now i got it. by "prompt" you mean "Gimme a number: ".

i was thinking, "prompt" means "blinking cursor".
The effect is just to place the text "Gimme a number!" in the output
buffer. If there had been a newline at the end, or a <<std::endl, then
the buffer would have been flushed at this point. But there isn't, so
the effect is just to accumulate text in the output buffer.

When subsequently the program executes

std::cin >number;

std::cin first tells std::cout to flush its buffer. std::cout flushes
its buffer, and the prompt appears on your screen. std::cin inputs the
number.
author should have said the "text" appears on the screen, using
"prompt" as a synonym for "text" confused me.
i think i can see the prompt every time, whether i use /cout/, cin/ or
whatever.

Nope.
ok, ok, you are right. as i said, i can always see the "blinking
cursor" on my screen.
It's std::cout that does the work. But std::cout does it in a lazy way,
accumulating text in its buffer, sort of like std::cout sits on the
toilet accumulating excrement there. Then std::cin knocks on the door
and says, hey, /I/ need to use the toilet now, flush it and get out!
dirty boy

;-)

just for fun
BTW, buffer is an internal data structures which holds characters, so
is it an array ?

2nd, buffer concept is used only for "efficiency " reasons ?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Mar 18 '07 #3
* arnuld:
BTW, buffer is an internal data structures which holds characters, so
is it an array ?
In practice yes.

2nd, buffer concept is used only for "efficiency " reasons ?
Yes, but...

The "but": if you are std::cout, when you write characters to a screen
that's part of the same computer you could just draw each one on the
screen as it's received for output, and then forget about it, no buffer.
But let's say the screen is an old-fashioned terminal connected to the
computer via a serial line. Displaying a character on the screen then
involves transmitting it to the terminal via the serial line, and this
is a complicated copying and conversion process, involving at least a
one-character buffer on each end of the line (in practice those buffers
are larger) -- in that situation the buffers are logically required by
the task to be accomplished, and not just optimization.

Generally, when we say "buffer" we mean a buffer that can hold more than
one character (or byte, or item), but for some problems it's important
to realize that even "un-buffered" input or output can involve
1-character buffers that cannot be avoided, because the i/o mechanism in
question isn't direct but has e.g. a transmission line.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 18 '07 #4
On Mar 18, 12:26 pm, "Alf P. Steinbach" <a...@start.now rote:

The "but": if you are std::cout, when you write characters to a screen
that's part of the same computer you could just draw each one on the
screen as it's received for output, and then forget about it, no buffer.
But let's say the screen is an old-fashioned terminal connected to the
computer via a serial line. Displaying a character on the screen then
involves transmitting it to the terminal via the serial line,
are you talking about "source code on my computer" and output on "some
other computer" connected to mine with internet ?

and that "other" is trying to compile "my programme" on his computer ?

is this "buffer" concept inherited from C ?
and this
is a complicated copying and conversion process, involving at least a
one-character buffer on each end of the line (in practice those buffers
are larger) -- in that situation the buffers are logically required by
the task to be accomplished, and not just optimization.

Generally, when we say "buffer" we mean a buffer that can hold more than
one character (or byte, or item), but for some problems it's important
to realize that even "un-buffered" input or output can involve
1-character buffers that cannot be avoided, because the i/o mechanism in
question isn't direct but has e.g. a transmission line.
i really did not understand anything here but it seems interesting

:-)

Mar 18 '07 #5
On 2007-03-18 08:41, arnuld wrote:
>On Mar 18, 12:26 pm, "Alf P. Steinbach" <a...@start.now rote:

>The "but": if you are std::cout, when you write characters to a screen
that's part of the same computer you could just draw each one on the
screen as it's received for output, and then forget about it, no buffer.
> But let's say the screen is an old-fashioned terminal connected to the
computer via a serial line. Displaying a character on the screen then
involves transmitting it to the terminal via the serial line,

are you talking about "source code on my computer" and output on "some
other computer" connected to mine with internet ?

and that "other" is trying to compile "my programme" on his computer ?
Think of it like this, your application is running on one computer and
the person interacting with it is sitting on another computer connected
via the internet (perhaps over Telnet or SSH). Now imagine that you are
going to output 100 characters on the screen.

If you send each character on their own that would mean that you would
have to send 100 network packages, each package would need some data to
tell it where to go and so on. I think that this data can take up to 40
bytes. So sending each character alone would take 100*(40+1) bytes
(assuming one byter per character).

If you instead put all output in a buffer and wait to send until all
characters are in the buffer you only need to send 40+100 bytes.

On modern coputers it does not matter if you output the characters one
and one or all at once as long as the output is to the local monitor
since the hardware is so fast, but as you saw in the example above if a
network is involved it can be important.
is this "buffer" concept inherited from C ?
Kind of yes, it's origionally from UNIX but C was developed on UNIX (or
rather it's standard library was a part of UNIX). On UNIX-machines of
old IO was done using terminals connected with a serial line, which were
slow so using buffering provided a noticable speedup. But you can notice
the difference even on modern hardware if you are writing large data to
files, this is almost never done without buffering for performance reasons.

--
Erik Wikström
Mar 18 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3981
by: habdalla | last post by:
When loading crystal reports in my windows applications(created using Visual C#), I get an error "Buffer too small for string or missing null byte". I tried various things i.e. increasing the field sizes in crystal reports, but still get the same error. I create a new crystal report using Visual C# and then added a Windows Form Viewer in...
81
7236
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
3
2975
by: Leo Nunez | last post by:
Hello! I need copy from structure "A" to "B" that contains "strings" in a one line code. Me problem like this : typedef struct tHeader{ char field1; char field2; char field3;
7
1924
by: Eric Laberge | last post by:
Aloha! This question is meant to be about C99 and unnamed compound objects. As I read, if such a construct as int *p = (int){0}; is used within a function, then it has "automatic storage duration associated with the enclosing block". So I tried the annexed code, and it compiles without a warning, and works as I expected.
3
1310
by: metaperl | last post by:
For this program: def reverse(data): for index in range(len(data)-1, -1, -1): yield data r = reverse("golf") for char in r: print char
0
1582
by: tarlino | last post by:
Hi! I'm play with the sound generation (c++ and directX). Now I would like to manage same filter on my sample. But now I must to convert my audio buffer "BYTE" (from DirectX) to an arry of "short" and viceversa. I found this source: ************************************************************** double dPowI = ceil( log10( (double)iLen / (...
9
3349
by: andrew.smith.cpp | last post by:
hi, whts the difference between the std::endl or "\n" ? because both do the same work Thanks
49
2858
by: aarklon | last post by:
Hi all, See:- http://www.cs.princeton.edu/introcs/faq/c2java.html for C vs Java in number crunching http://husnusensoy.blogspot.com/2006/06/c-vs-java-in-number-crunching.html
0
7532
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7971
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7491
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.