printf, stdout and setvbuf

Discussion in 'C Programming' started by vippstar@gmail.com, Jul 13, 2008.

  1. Guest

    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");
    return EXIT_FAILURE;
    }
    return 0;
    }

    I imagine the case that printf() provides stdout a malloc'ed buffer.
    I know implementations do this, but I can't find the wording in the
    standard that mentions this.
    , Jul 13, 2008
    #1
    1. Advertising

  2. santosh Guest

    wrote:

    > 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");
    > return EXIT_FAILURE;
    > }
    > return 0;
    > }
    >
    > I imagine the case that printf() provides stdout a malloc'ed buffer.
    > I know implementations do this, but I can't find the wording in the
    > standard that mentions this.


    Well, your program exhibits undefined behaviour because you are calling
    setvbuf after an I/O operation has already been done on stdout.
    santosh, Jul 13, 2008
    #2
    1. Advertising

  3. Guest

    On Jul 13, 4:41 pm, santosh <> wrote:
    > wrote:
    > > 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");
    > > return EXIT_FAILURE;
    > > }
    > > return 0;
    > > }

    >
    > > I imagine the case that printf() provides stdout a malloc'ed buffer.
    > > I know implementations do this, but I can't find the wording in the
    > > standard that mentions this.

    >
    > Well, your program exhibits undefined behaviour because you are calling
    > setvbuf after an I/O operation has already been done on stdout.


    Oh! Then nevermind. Now it makes sense.
    , Jul 13, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith Doyle

    Undefined side-effect of setvbuf?

    Keith Doyle, Aug 23, 2003, in forum: C Programming
    Replies:
    2
    Views:
    387
    Alan Balmer
    Aug 25, 2003
  2. grunes
    Replies:
    6
    Views:
    525
    grunes
    Jul 9, 2004
  3. j0mbolar

    setvbuf

    j0mbolar, Aug 20, 2004, in forum: C Programming
    Replies:
    2
    Views:
    463
    Dan Pop
    Aug 20, 2004
  4. santosh

    Uses of setvbuf()

    santosh, Jan 5, 2007, in forum: C Programming
    Replies:
    10
    Views:
    837
    kyle york
    Jan 15, 2007
  5. James Kuyper

    Re: setvbuf() usage

    James Kuyper, Jun 27, 2009, in forum: C Programming
    Replies:
    2
    Views:
    810
Loading...

Share This Page