473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

printf, stdout and setvbuf

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 '08 #1
2 4254
vi******@gmail.com 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.

Jul 13 '08 #2
On Jul 13, 4:41 pm, santosh <santosh....@gmail.comwrote:
vipps...@gmail.com 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 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Philip Hölzenspies | last post by:
Hi All, I have been crunching my brains on this one all day. I use this library that creates output I want, but it can only write it to a file or the stdout. The problem is that I don't want to...
1
by: bine | last post by:
Sorry, I found a lot of old stuff, even kind of flamewars about good+bad style whatsoever.. I simply want any hint that works! I try to migrate older stuff from NT and newer stuff from...
2
by: bine | last post by:
Sorry, I wanted to post a followup to my own question, but google was "Unable to retrieve message 003lv0h2n87mc764odt16i56btesds6dm9@4ax.com" ... I found a solution for printf directly, without...
7
by: Mathias Herrmann | last post by:
Hi. I have the following problem: Using popen() to execute a program and read its stdout works usually fine. Now I try to do this with a program called xsupplicant (maybe one knows), but I dont...
2
by: Nadav | last post by:
Hi, Introduction: *************** I am trying to redirect stdout to a RichEdit control, this is done by initiating a StringWriter, associated it with a StringBuilder and setting the...
1
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
7
by: Cell | last post by:
when both are connected to screen and the anything written to these two constant file pointers will go onto the screen ?
10
by: Rahul | last post by:
Hi Everyone, I had a query reg printf(). I heard that it can't be used in ISR's as it is non-re-enterant. I didn't get as to why printf is non-re-enterant and why non-re-enterant library can't...
4
by: lovecreatesbea... | last post by:
For example, in Bourne Shell both stdout and stderr can be re-directed to /dev/null, $ ./a.out 2>&1 /dev/null then is there any difference still?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.