Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 740795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:33:26+00:00 2026-05-14T08:33:26+00:00

So I’ve been digging into how the stdio portion of libc is implemented and

  • 0

So I’ve been digging into how the stdio portion of libc is implemented and I’ve come across another question. Looking at man setvbuf I see the following:

When the first I/O operation occurs on
a file, malloc(3) is called, and a
buffer is obtained.

This makes sense, your program should not have a malloc in it for I/O unless you actually use it. My gut reaction to this is that libc will clean up its own mess here. Which I can only assume it does because valgrind reports no memory leaks (they could of course do something dirty and not allocate it via malloc directly… but we’ll assume that it literally uses malloc for now).

But, you can specify your own buffer too…

int main() {
    char *p = malloc(100);
    setvbuf(stdio, p, _IOFBF, 100);
    puts("hello world");
}

Oh no, memory leak! valgrind confirms it. So it seems that whenever stdio allocates a buffer on its own, it will get deleted automatically (at the latest on program exit, but perhaps on stream close). But if you specify the buffer explicitly, then you must clean it up yourself.

There is a catch though. The man page also says this:

You must make sure that the space that
buf points to still exists by the time
stream is closed, which also happens
at program termination. For example,
the following is invalid:

Now this is getting interesting for the standard streams. How would one properly clean up a manually allocated buffer for them, since they are closed in program termination? I could imagine a “clean this up when I close flag” inside the file struct, but it get hairy because if I read this right doing something like this:

setvbuf(stdout, 0, _IOFBF, 0);
printf("hello ");
setvbuf(stdout, 0, _IOLBF, 0);
printf("world\n");

would cause 2 allocations by the standard library because of this sentence:

If the argument buf is NULL, only the
mode is affected; a new buffer will be
allocated on the next read or write
operation.

EDIT: an addendum to my question. Since it is clear that I must free any buffers I pass to setvbuf, if I do in fact use it on stdout is there any practical way to free it? It must live to program end. The best I can think of is to fclose(stdout) then free it or use a static buffer as some people have mentioned. I ask because it does seem like an awkward design decision.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:33:27+00:00Added an answer on May 14, 2026 at 8:33 am

    Also from the man page (at least, on my system):

    If buf is not NULL, it is the caller’s responsibility to free(3) this buffer after closing the stream.

    That is, you malloc-ed it, you free it.

    Before exit, you could close the streams yourself, thus allowing you to free the buffer. Alternatively, you could flush the streams and call setvbuf again with a NULL buffer argument to switch back to a library managed buffer or unbuffered I/O.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.