Should I free the pointer returned by getpwuid () on Linux?

After calling getpwuid (uid), I have a link to a pointer. Should I release this when I no longer use it? While reading the manual pages, he says that he refers to some static area that can be overwritten by subsequent calls to the same functions, so I’m sure that if I touch this area of ​​memory.

Thanks.

+6
c linux getpwuid
source share
3 answers

Not. You do not need to free the result. You can call only for free (3) according to the pointers allocated on the heap using malloc (3), calloc (3) or realloc (3).

Static data is part of the bss program data or segments and will be stored until the process terminates (or exec (2) is overwritten).

+9
source share

Use the *_r ( getpwuid_r() ) functions for thread-safe (reentrant) functions that allow you to provide buffer space to hold the returned information. Be sure to check errno for success or failure. If you do not use repeated functions, you can safely assume that the function returns data that does not need to be freed, but will also be overwritten by successive calls to the same function.

+5
source share

In fact, it returns a pointer to an existing structure, so you should not free it.

+1
source share

All Articles