Thread: Bufsiz

  1. #1
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26

    Bufsiz

    Running Windows, I'm using gcc as provided by Strawberry Perl.

    If I #define BUFSIZ 1024, I get a warning that in stdio.h it is already
    defined as 512.

    No problem, really. I can still redefine it or just use another word such as BUFSIZE.

    But I wonder why it is already defined. Is it because this is an
    optimal buffer size for disk I/O?

    My program is a small one, but it might read numerous files successively (by means of wildcard expansion), including large files. I can easily use a larger buffer if it would improve performance. But perhaps there is no point.

    What do you recommend?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    BUFSIZ is defined in stdio.h and is equal to the default size passed to setbuf.

    Unless you're expecting excessively long lines in any of your files, just declaring char buff[BUFSIZ] should be fine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26
    Quote Originally Posted by Salem View Post
    BUFSIZ is defined in stdio.h and is equal to the default size passed to setbuf.

    Unless you're expecting excessively long lines in any of your files, just declaring char buff[BUFSIZ] should be fine.
    Thanks! My program reads the files in binary mode. It makes no difference whether they are binary files or not.

    From what I can grasp, if I don't call setbuf, then the system uses its own buffer and copies data to the buffer my program has allocated; but
    with setbuf, disk input goes directly to my buffer. Am I correct? Setbuf would then be advisable. If the size is a multiple of BUFSIZ, it probably wouldn't hurt, but might not provide a benefit, either.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No, you don't use the buffer passed to setbuf for your own purposes directly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BUFSIZ value
    By Laserve in forum C Programming
    Replies: 4
    Last Post: 10-18-2006, 12:15 AM
  2. BUFSIZ question
    By Ash1981 in forum C Programming
    Replies: 2
    Last Post: 02-05-2006, 10:11 AM
  3. What size exactly is the constant BUFSIZ
    By Zahl in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2003, 11:20 AM
  4. Bufsiz
    By steviecrawf in forum C Programming
    Replies: 1
    Last Post: 11-14-2001, 04:18 PM

Tags for this Thread