473,791 Members | 3,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Uses of setvbuf()

Which situations call for the use of setvbuf(), specifically when you
supply the buffer yourself? What is the advantage of an user specified
buffer over an automatically allocated one? Can we call setvbuf() on
the predefined streams, immediatly upon start of main()? How often is
this function used in typical C programs?

Thanks.

Jan 5 '07 #1
10 6947
santosh wrote On 01/05/07 11:46,:
Which situations call for the use of setvbuf(), specifically when you
supply the buffer yourself? What is the advantage of an user specified
buffer over an automatically allocated one? Can we call setvbuf() on
the predefined streams, immediatly upon start of main()? How often is
this function used in typical C programs?
Probably the commonest use of setvbuf() or setbuf() is
to make an output stream line-buffered or unbuffered. The
size of the supplied buffer (if any) isn't much of an issue
in this scenario.

For input or output streams that pass "a lot" of data
and can operate fully-buffered, specifying a "large" buffer
may improve the I/O throughput. How much improvement you
get depends on the system, the C implementation, the program's
I/O behavior, and a lot of other situation-specific details;
the meanings of "a lot" and "large" are also hard to pin down.
Bigger is not always better; "too large" a buffer may actually
hurt performance in some circumstances.

You can apply setvbuf() or setbuf() to any stream, if
you do it soon enough. There's nothing special about the
predefined streams in this regard.

Finally, these functions are really only "suggestion s"
to the I/O library, and the library is at liberty to modify
them or even to ignore them. Implementations differ, and
the implementor (for some strange reason) often believes he
has more knowledge of the underpinnings of the implementation
than does the programmer ...

--
Er*********@sun .com
Jan 5 '07 #2
Eric Sosman wrote:
santosh wrote On 01/05/07 11:46,:
Which situations call for the use of setvbuf(), specifically when you
supply the buffer yourself? What is the advantage of an user specified
buffer over an automatically allocated one? Can we call setvbuf() on
the predefined streams, immediatly upon start of main()? How often is
this function used in typical C programs?
<snip explanation>
Finally, these functions are really only "suggestion s"
to the I/O library, and the library is at liberty to modify
them or even to ignore them.
I can understand ignoring a call by returning a non-zero value but
won't modifying silently the semantics of a call mislead the calling
code? It seems more in keeping with the spirit of C to either honour a
request or to fail.

Anway thanks for your explanation.
Implementations differ, and
the implementor (for some strange reason) often believes he
has more knowledge of the underpinnings of the implementation
than does the programmer ...
In most cases they probably do.

Jan 5 '07 #3
santosh wrote:
Which situations call for the use of setvbuf(), specifically when you
supply the buffer yourself? What is the advantage of an user specified
buffer over an automatically allocated one? Can we call setvbuf() on
the predefined streams, immediatly upon start of main()? How often is
this function used in typical C programs?
Usually, when I call setvbuf() it is to turn off buffering for stdin
and/or stdout. An example of when this is needed is for Winboard
protocol chess engines which exchange text command streams with a chess
protocol server. If the I/O is buffered, a command may not get flushed
and the engine can stall for no apparent reason.

Sometimes, I want to increase the size of the buffer. Sequentially
scanning a file is an example where this can be handy. However, the
buffer size allowed is so tiny that I usually resort to OS specific
routines instead, because the tiny buffers allowed by setvbuf() do not
afford any big boost.

Jan 5 '07 #4
user923005 wrote:
santosh wrote:
Which situations call for the use of setvbuf(), specifically when you
supply the buffer yourself? What is the advantage of an user specified
buffer over an automatically allocated one? Can we call setvbuf() on
the predefined streams, immediatly upon start of main()? How often is
this function used in typical C programs?

Usually, when I call setvbuf() it is to turn off buffering for stdin
and/or stdout.
Turning off buffering for stdin in setvbuf() seems to accomplish little
since it's overruled by the OS's buffering. But I can see that it might
be useful for output and non-interactive input streams.

<snip>
Sometimes, I want to increase the size of the buffer. Sequentially
scanning a file is an example where this can be handy. However, the
buffer size allowed is so tiny that I usually resort to OS specific
routines instead, because the tiny buffers allowed by setvbuf() do not
afford any big boost.
Correct me if I'm wrong but I don't see any limit to the buffer's
allowed size. The value of BUFSIZ is recommended but I don't see why
only tiny buffers are allowed.

Jan 5 '07 #5
santosh wrote On 01/05/07 12:26,:
Eric Sosman wrote:
>>santosh wrote On 01/05/07 11:46,:
>>>Which situations call for the use of setvbuf(), [...]

Finally, these functions are really only "suggestion s"
to the I/O library, and the library is at liberty to modify
them or even to ignore them.

I can understand ignoring a call by returning a non-zero value but
won't modifying silently the semantics of a call mislead the calling
code? It seems more in keeping with the spirit of C to either honour a
request or to fail.
The older setbuf() function has no way to "fail," since
it is void and can return no value.

setvbuf() returns a success/failure indication, but what
constitutes "failure" seems not to be very well defined. For
example, is it a "failure" if you provide a 32K buffer for an
output stream, and the I/O library happily uses the buffer but
always drains it when 1K characters have accumulated?

I/O systems must bridge a speed gap of seven or so decimal
orders of magnitude, a gap that continues to widen. They also
deal with an ever-changing suite of device characteristics :
streaming devices, multi-pathed devices, devices that appear
and disappear dynamically, devices that require cryptographic
challenge-and-response, ... The thicket of problems faced by
an I/O system is dense, and the tools developed to deal with
it (by cutting, burning, spraying herbicide, going around the
edge, ...) are diverse to the point of being baroque. Atop
this Babel of complexity sits the C library with its simple
model of stream I/O -- another impedance mismatch to be dealt
with. The implementor needs all the freedom the Standard can
grant.

--
Er*********@sun .com
Jan 5 '07 #6

santosh wrote:
user923005 wrote:
santosh wrote:
Which situations call for the use of setvbuf(), specifically when you
supply the buffer yourself? What is the advantage of an user specified
buffer over an automatically allocated one? Can we call setvbuf() on
the predefined streams, immediatly upon start of main()? How often is
this function used in typical C programs?
Usually, when I call setvbuf() it is to turn off buffering for stdin
and/or stdout.

Turning off buffering for stdin in setvbuf() seems to accomplish little
since it's overruled by the OS's buffering. But I can see that it might
be useful for output and non-interactive input streams.
If you don't call it, then the chess engines don't work. If you do
call it then they do. So I would not call that useless. If there were
some compiler that did not honor my buffering request, then I would
switch compilers.

7.19.5.6 The setvbuf function
Synopsis
1 #include <stdio.h>
int setvbuf(FILE * restrict stream,
char * restrict buf,
int mode, size_t size);
Description
2 The setvbuf function may be used only after the stream pointed to by
stream has
been associated with an open file and before any other operation (other
than an
unsuccessful call to setvbuf) is performed on the stream. The argument
mode
determines how stream will be buffered, as follows: _IOFBF causes
input/output to be
fully buffered; _IOLBF causes input/output to be line buffered; _IONBF
causes
input/output to be unbuffered. If buf is not a null pointer, the array
it points to may be
used instead of a buffer allocated by the setvbuf function (see
footnote 230) and the argument size
specifies the size of the array; otherwise, size may determine the size
of a buffer
allocated by the setvbuf function. The contents of the array at any
time are
indeterminate.
Returns
3 The setvbuf function returns zero on success, or nonzero if an
invalid value is given
for mode or if the request cannot be honored.

footnote 230) The buffer has to have a lifetime at least as great as
the open stream, so the stream should be closed
before a buffer that has automatic storage duration is deallocated upon
block exit.

<snip>
Sometimes, I want to increase the size of the buffer. Sequentially
scanning a file is an example where this can be handy. However, the
buffer size allowed is so tiny that I usually resort to OS specific
routines instead, because the tiny buffers allowed by setvbuf() do not
afford any big boost.

Correct me if I'm wrong but I don't see any limit to the buffer's
allowed size. The value of BUFSIZ is recommended but I don't see why
only tiny buffers are allowed.
The thing could theoretically be huge. But Microsoft set a tiny limit
on it (16K or something nearly as absurd if I recall correctly) so I
just write system specific routines when I want to buffer since I am
going to have to do it anyway.

Jan 5 '07 #7
user923005 wrote:
The thing could theoretically be huge. But Microsoft set a tiny limit
on it (16K or something nearly as absurd if I recall correctly) so I
just write system specific routines when I want to buffer since I am
going to have to do it anyway.
From:
http://msdn.microsoft.com/library/de...rt_setvbuf.asp
We have this:

"size
Buffer size in bytes. Allowable range: 2 < size < 32768. Internally,
the value supplied for size is rounded down to the nearest multiple of
2."

Which has caused me a wee bit of grief from time to time.

Jan 5 '07 #8

santosh wrote:
Which situations call for the use of setvbuf(), specifically when you
supply the buffer yourself? What is the advantage of an user specified
buffer over an automatically allocated one? Can we call setvbuf() on
the predefined streams, immediatly upon start of main()? How often is
this function used in typical C programs?

Thanks.
Any situation that calls for change in the characteristics of streams.
Keep in mind though, there are different levels of input/output
typically.

--
aegis

Jan 5 '07 #9
On 5 Jan 2007 08:46:43 -0800, "santosh" <sa*********@gm ail.comwrote:
>Which situations call for the use of setvbuf(), specifically when you
supply the buffer yourself? What is the advantage of an user specified
buffer over an automatically allocated one? Can we call setvbuf() on
the predefined streams, immediatly upon start of main()? How often is
this function used in typical C programs?

Thanks.
As Eric says, controlling the buffering is probably the most common
use. I've found (and here we go off-topic for the group) that in some
implementations , specifying a buffer can give better performance if
you are opening and closing many files with the same file pointer,
apparently because it saves the system allocating a new buffer every
time. In fact, this was the workaround for a bug in one version of
HP-UX, which allocated buffers much faster than it released them,
causing a program to run out of memory. A fun one to track down, since
the error was at the system level, and the program hadn't a clue as to
why it was suddenly kicked out :-)

--
Al Balmer
Sun City, AZ
Jan 6 '07 #10

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

Similar topics

2
3069
by: Keith Doyle | last post by:
I'm curious if setvbuf behavior is known to be undefined with regards to effects it may have on the file pointer. I've found that the following code works different on AIX 4.3.3 xlc and FreeBSD gcc 2.95.2 19990314 than it does on SCO 5.0.4 cc or Linux gcc egcs-1.1.2 19991024. This is a bit curious as here's a gcc example that works one way and one that works the other, and I would think the relevant code is in libc/glibc. This is what...
6
4726
by: grunes | last post by:
I wish to fread a few thousand bytes at a time in turn from several very large files from a DVD data disk, under Redhat Fedora Linux. When this is done the DVD drive wastes a lot of time and almost shakes itself to pieces. I tried using setvbuf, with large buffers, e.g. (example only, not checked): #include <stdio.h>
2
2037
by: j0mbolar | last post by:
is using _IONBF with setvbf the general way of getting an unbuffered stream in a standardized way? or are system specific functions generally favored over c89's setvbuf?
5
2010
by: | last post by:
We have this situation: We have a unmanaged c++ executable located in a certain folder (a.exe) It loads a mixed mode managed/unmanaged dll using MFC dynamically from a completely different path (b.dll) This last dll, uses other pure managed and mixed managed/unmanaged dll's that uses MFC. (c.dll, d.dll,...) What works is a.exe loading b.dll. But once I want to use a functions in c.dll I get an exception indicating that c.dll is not...
2
12358
by: ChasW | last post by:
Greetings, I have a form that uses a query as its record source. In the form I have a text box that uses this as its control source: =DCount("", "qry_Search_by_Name") The DCount function works properly with the query until I add a criteria to the query like this:
4
2974
by: Rob Dob | last post by:
Hi, I have both a Customer and a Orders Table, the key field is CustomerID. I also have a DataGrid that uses a BindingSource which uses a dataset which uses a join between the Customers table and the Orders table. Everything displays correctly and when I double click on a row within the datagrid I then open up a tabbed form that contains a detailed view of the record selected. The user at this point is able to make changes to the...
2
1295
by: Serg | last post by:
Hi, all I have a trouble. I have a WebService that uses MCpp assembly that uses some MFC classes. Everything perfectly works on my machine. But when i install web site on clients machine it does not work. Trying to open an aspx page i get error "could not load assembly or one of its dependencies". I think this is because of using MFC. What should i do to make it work?
2
4626
by: sitko | last post by:
Hi, I'm creating a quick utility which will copy one file into another, but I need it to have the ability to select out certain lines not to copy. I figured I'd use fread/fwrite but set the buffering size to a line with "setvbuf", here is the code I was going to use as a skeleton for this: #include <stddef.h> #include <stdio.h> #define FAIL 0 #define SUCCESS 1
2
4272
by: vippstar | last post by:
Is there a memory leak in this particular program: #include <stdio.h> #include <stdlib.h> int main(void) { printf("Hello, world\n"); if(setvbuf(stdout, NULL, _IONBF, 0) != 0) { perror("setvbuf");
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
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
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.