473,804 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(stdo ut, 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 4272
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(stdo ut, 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....@gm ail.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(stdo ut, 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
2482
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 use the filesystem (permissions, latency through NFS, etc, etc), but a memory buffer. I've been looking for a way to redirect stdout to a buffer that I manage myself. Obviously I considered setbuf (from stdio.h), but then I'ld still have to...
1
2454
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 Linux/AIX to run on Windows2003 as well. I use the Open Watcom C32 Optimizing Compiler Version 1.3 Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
2
1586
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 fflush or setvbuf or other workarounds/addons. It was my fault, because I was using some of the samples\*..\makefile which had system nt_win inside, instead of nt (or 386) only. So in fact I created a win-compilation to use from a DOS-box or...
7
15608
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 get the output of it while it is running. This is probably a problem of stdout being buffered, because if I use fflush() after a printf() in the xsupplicant then I can read the output.
2
3609
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 Console.Out to the String Writer, when ever the RichEdit is to be updated text from the StringBuilder is being copied. The Problem:
1
8525
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
7174
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
4577
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 be used in an ISR? Thanks in advance ! ! !
4
2297
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
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10308
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5513
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.