How is the result struct of localtime allocated in C?

后端 未结 6 1063
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 04:47

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          


        
6条回答
  •  春和景丽
    2020-11-30 05:21

    They return a pointer to a static struct local to the library. From the man page:

    NOTES
    
    The  four functions asctime(), ctime(), gmtime() and localtime() return
    a pointer to static data and hence are  not  thread-safe.   Thread-safe
    versions asctime_r(), ctime_r(), gmtime_r() and localtime_r() are spec‐
    ified by SUSv2, and available since libc 5.2.5.
    
    POSIX.1-2001 says: "The asctime(), ctime(), gmtime(),  and  localtime()
    functions  shall  return values in one of two static objects: a broken-
    down time structure and an array of type char.  Execution of any of the
    functions  may  overwrite  the  information returned in either of these
    objects by any of the other functions."  This can occur  in  the  glibc
    implementation.

提交回复
热议问题