简体   繁体   中英

How time.h handles memory

Sorry, this is probably a stupid question, but I couldn't find answer in the documentation for time.h .

So when I call, for example, gmtime

time_t today;
struct tm *info;
time(&today);
info = gmtime(&today);

It returns pointer to the tm structure. I have assumed that it returns pointer to a part of memory allocated with malloc, but if I call free for info now - free returns error. So how does library time.h handles memory allocation and should I be worried about "freeing" it?

that it returns pointer to a part of memory allocated with malloc, but if I call free for info now

No, gmtime returns a pointer to a static object.

From C99 7.23.3p1 :

Except for the strftime function, these functions each return a pointer to one of two types of static objects: a broken-down time structure or an array of char. Execution of any of the functions that return a pointer to one of these object types may overwrite the information in any object of the same type pointed to by the value returned from any previous call to any of them. The implementation shall behave as if no other library functions call these functions.

how does library time.h handles memory allocation

It uses memory allocated with static storage duration valid for the whole execution of your program.

should I be worried about "freeing" it?

No.

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