简体   繁体   中英

should I free pointer returned by getpwuid() in Linux?

After I call getpwuid(uid) , I have a reference to a pointer. Should I free that pointer when I don't use it anymore? Reading the man pages, it says that it makes reference to some static area, that may be overwritten by subsequent calls to the same functions, so I'm not sure if I should touch that memory area.

Thanks.

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

Static data is part of a program's data or bss segments and will persist until the process exits (or is overwritten by exec(2)).

Use the *_r functions ( getpwuid_r() ) for thread-safe (reentrant) functions that allow you to supply the buffer space to place the returned information in. Be sure check errno for success or failure. If you do not use reentrant 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.

实际上,它返回一个指向一个已经存在的结构,所以你应该释放它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM