[PATCH] glibc/manual/examples: add missing free to open_memstream example
Florian Weimer
fweimer@redhat.com
Tue Jun 24 09:16:18 GMT 2025
* Sam James:
> Collin Funk <collin.funk1@gmail.com> writes:
>
>> 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.
>
> We should fix it because:
> a) nobody will file a bug for the unnecessary free in an example, but someone will surely
> file a bug or send a patch again to add the free;
> b) examples are supposed to be exemplar and form part of larger
> programs.
I agree with Sam.
Ravina, would you resend please with the formatting issue fixed?
Thanks,
Florian
More information about the Libc-alpha
mailing list