illegal use of setbuf

This is a discussion on illegal use of setbuf within the C Programming forums, part of the General Programming Boards category; hello, from the man page setbuf() always uses a suboptimal buffer size and should be avoided. You must make sure ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    16

    illegal use of setbuf

    hello, from the man page
    setbuf() always uses a suboptimal buffer size and should be avoided. You must make sure that both buf and the space it points to still exist by the time stream is closed, which also happens at program termination. For example, the following is illegal:
    Code:
    #include <stdio.h>
    int main()
    {
        char buf[BUFSIZ];
        setbuf(stdin, buf);
        printf("Hello, world!\n");
        return 0;
    }
    I don't quite understand why that is illegal,
    can anyone explain more about it please.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Because the buffer may still be used by the stdio routines after main has returned, then you're into the whole "pointer to a local variable which doesn't exist" scenario.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    I still doesn't quite understand it, (yeah slowww),

    can you give a simple program example that create the error?

    and is it not true that each program have it own stdin and stdout ?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    This involves understanding two main concepts.

    First important thing to understand is that local variables are destroyed around the time the function exits. Depending upon the calling routine they are destroyed right before the function returns or right after, but either way they should be considered gone. If you learn a little bit more about assembly and how to call functions in that language, you'll see that what I'm saying isn't completely true in the sense that the variables aren't destroyed, but you'll understand what we're getting at.

    The next thing to understand is that stdin and stdout are special FILE *'s that are a part of the C standard library. When you return from main(), you don't really just return to the command line application that started your program. The function exit() is called with the value that you returned from main(). exit() calls all functions registered via the atexit() function, if I remember correctly. At this time, there is some cleanup going on, like all output buffers opened with fopen() are flushed, etc. etc..

    From what Salem and the man pages are saying, main() has apparently returned at this point (apparently main() returns and then exit() is called), which means that your local array cannot be guarenteed to still be in existance and not be altered. If anything is done with stdin during the cleanup of main(), well.... dunno. It's probably undefined.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > can you give a simple program example that create the error?
    No, because such behaviour is undefined, which means there's no telling what might actually happen.

    Leaving the \n off the printf() might cause something to happen.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    thank you MacGyver and Salem.

    And I do manage to get a error :-D
    like this
    *** stack smahing detected ***: ./test terminated
    Aborted (core dumped)
    what I have done is setbuf(stdin,10) in main and use atexit to register a function call to get a very long input string when the program exit. Oh it even fail if I enter a short input string.
    Last edited by taisao; 05-28-2007 at 10:50 AM.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    I have another question :-)

    after modify some "settings" with the file pointer stdin, like setting buffersize (and what more you could do with it ...). Is there a way to change it back like when the OS have given the program at the start of the program?

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by taisao View Post
    I have another question :-)

    after modify some "settings" with the file pointer stdin, like setting buffersize (and what more you could do with it ...). Is there a way to change it back like when the OS have given the program at the start of the program?
    What!?!?

    Um yes... Just get and store the defaults before you change them, then set them back when you want to use the defaults...?

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    (is my question difficult to understand or so? (well, I'm not good with English))


    but after setvbuf for stdin, the old buffer of stdin should already be freed right? Or is the buffer for stdin a global static "object" like stdin is a global file pointer?

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > Or is the buffer for stdin a global static "object" like stdin is a global file pointer?
    There isn't enough information to decide how the implementation chose to allocate the first buffer.

    As for what size the original buffer was, it's likely to have been BUFSIZ in size (a constant in stdio.h).
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-24-2009, 02:42 AM
  2. Detecting illegal characters
    By paul_harris77 in forum C Programming
    Replies: 14
    Last Post: 02-28-2009, 01:58 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Illegal Characters
    By Echidna in forum Windows Programming
    Replies: 3
    Last Post: 12-08-2002, 03:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21