Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf, stdout and setvbuf

Reply
Thread Tools

printf, stdout and setvbuf

 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      07-13-2008
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.
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      07-13-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      07-13-2008
On Jul 13, 4:41 pm, santosh <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: setvbuf() usage James Kuyper C Programming 2 06-29-2009 08:16 PM
Uses of setvbuf() santosh C Programming 10 01-15-2007 07:37 PM
setvbuf j0mbolar C Programming 2 08-20-2004 11:45 AM
Having trouble with setvbuf with large buffer size under linux grunes C Programming 6 07-09-2004 04:18 AM
Undefined side-effect of setvbuf? Keith Doyle C Programming 2 08-25-2003 03:54 PM



Advertisments