Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the result struct of localtime allocated in C?

I was playing with the time.h file in C that helps us with time/day functions.

I came across:

struct tm * _Cdecl localtime(const time_t *__timer); 

...which seems to return a pointer to tm struct. I have found that return by address is mostly used to return new memory allocations.

If this is so, how does the above return actually work (the return address of a struct tm). Is the returned object defined somewhere?

Thanks

like image 987
Akash Avatar asked Jan 01 '12 17:01

Akash


People also ask

What does Localtime return in C?

The localtime( ) function return the local time of the user i.e time present at the task bar in computer.

Does Localtime allocate memory?

No, you shouldn't. This structure is statically allocated and shared by the functions gmtime and localtime . Each time either one of these functions is called the content of this structure is overwritten.

Why does Localtime return a pointer?

The pointer returned by localtime (and some other functions) are actually pointers to statically allocated memory. So you do not need to free it, and you should not free it. This structure is statically allocated and shared by the functions gmtime and localtime.


2 Answers

The pointer returned by localtime (and some other functions) are actually pointers to statically allocated memory. So you do not need to free it, and you should not free it.

http://www.cplusplus.com/reference/clibrary/ctime/localtime/

This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the content of this structure is overwritten.

EDIT : Appending a few things mentioned in the comments.

A direct result of this shared data-structure is that localtime and similar functions are not thread-safe. The thread-safe solution varies with different platforms. localtime_r for POSIX and localtime_s for MSVC.

like image 79
Mysticial Avatar answered Sep 18 '22 16:09

Mysticial


It returns a pointer to a piece of statically allocated memory (probably either a static variable defined inside localtime or a global defined somewhere in the C runtime library). You must not free such memory.

Obviously this function is not reentrant (but can be thread-safe if TLS is used).

You must be careful when using this pointer: never make any function calls that could call localtime/gmtime/... before you finished using that pointer, otherwise the content of the memory referenced by your pointer could change (in response to the new call to localtime) and you will be reading values relative to another time_t.

In general the design of the date/time library is quite outdated, this kind of optimization was worthwhile when the C language was designed, nowadays it only gives problems.

To address these problems there are at least two different improved versions of these functions: localtime_r (SUSv2, r stays for "reentrant") and localtime_s (Microsoft, s stays for "safe"). The sad fact for portability is that these do almost the same thing (they require the destination struct tm to be passed as a parameter), but differ in name and ordering of the parameters.

like image 39
Matteo Italia Avatar answered Sep 17 '22 16:09

Matteo Italia