Are there any memory gaps in this code?

I have this code:

int tim=10000; // some random number tm *now=localtime(&tim); printf("Date is %d/%02d/%02d\n", now->tm_year+1900, now->tm_mon+1, now->tm_mday); printf("Time is %02d:%02d\n", now->tm_hour, now->tm_min); 

The reason I'm wondering if there is a memory leak is because localtime returns a pointer to the structure, which means memory allocation. but no one lets him out.

Is there a memory leak in this code?

+5
source share
1 answer

You should not (and should not) explicitly release anything, since localtime returns a pointer to a static object.

C Standard says:

(C11, 7.27.3 Time conversion functions p1) "Except for the strftime function, each of these functions returns a pointer to one of two types of static objects: a broken time structure or a char array."

And from POSIX.1-2008 ,

The functions asctime (), ctime (), gmtime (), and localtime () must return values ​​in one of two static objects: a broken time structure and an array of type char. Performing any of the functions may overwrite the information returned in any of these objects, any of the other functions.

+9
source

Source: https://habr.com/ru/post/1201233/


All Articles