Search code examples
cmemory-managementfunction-prototypes

Does returned struct of localtime() need to be freed?


struct tm *localtime(const time_t *timep);

I checked man localtime but there's no words on whether it's my duty to clean it after using.

And in fact I have many similar doubts on functions returning a pointer, how do you determine it should be freed or not ?


Solution

  • This information should be in the man page - my localtime man page says:

    The return value points to a statically allocated struct...

    Statically allocated objects should not be passed to free(), so this is your answer - no, you should not free the return value of localtime().

    The only way to tell in the general case is to consult the documentation or implementation of the function in question.