[PATCH] glibc/manual/examples: add missing free to open_memstream example

Collin Funk collin.funk1@gmail.com
Tue Jun 24 00:47:48 GMT 2025


Hi Ravina,

Ravina Jain <rajulravina123@gmail.com> writes:

>    printf ("buf = `%s', size = %zu\n", bp, size);
> +  free(bp);

It should be formatted as "free (bp)".

However, I don't think this is necessary. It is common to not free
memory when the program is going to just exit anyways. Here is what the
GNU Coding Standards say [1]:

    Memory analysis tools such as valgrind can be useful, but don’t
    complicate a program merely to avoid their false alarms. For
    example, if memory is used until just before a process exits, don’t
    free it simply to silence such a tool.

We can see this behavior in 'ls' for example:

    $ valgrind ls -la --human-readable 2>&1 | grep 'still reachable'
    ==167809==    still reachable: 33,792 bytes in 217 blocks

On modern operating systems the memory will be reclaimed once the
program exits, so calling 'free' is just extra time spent by the
program.

Collin

[1]  https://www.gnu.org/prep/standards/standards.html#Memory-Usage


More information about the Libc-alpha mailing list