-1

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?

Demiler
  • 67
  • 3
  • 9
  • 4
    https://en.cppreference.com/w/c/chrono/gmtime – Mat Apr 28 '20 at 09:18
  • 1
    Does this answer your question? [How is the result struct of localtime allocated in C?](https://stackoverflow.com/questions/8694365/how-is-the-result-struct-of-localtime-allocated-in-c) – KamilCuk Apr 28 '20 at 09:28

1 Answers1

1

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.

Community
  • 1
  • 1
KamilCuk
  • 120,984
  • 8
  • 59
  • 111