printf, stdout and setvbuf

V

vippstar

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.
 
S

santosh

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.
 
V

vippstar

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top