error C4996: 'ctime': This function or variable may be unsafe

后端 未结 5 1082
灰色年华
灰色年华 2020-12-09 21:45

I have a large project about static source code analysis, and everything compiles successfully, except for one thing. I have provided the error message in the title. The poi

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 22:04

    std::ctime is not thread safe for two reasons:

    • It can modify a global object of type std::tm that is shared by multiple functions.
    • It modifies a global char array and returns a pointer to that array.

    There is a potential for collisions if you have other threads that call std::gmtime, std::localtime, or std::ctime.

    The best thing to do is to convert that call to std::ctime to a call to std::strftime. This is consistent with POSIX, which deems ctime to be obsolete and recommends usage of strftime in its stead.

提交回复
热议问题