C++ localtime vs localtime_s 和适当的输入参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14386923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:18:23  来源:igfitidea点击:

localtime vs localtime_s and appropriate input arguments

c++localtime

提问by ProGirlXOXO

time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );

This returns: warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead.

这将返回:警告 C4996:'localtime':此函数或变量可能不安全。考虑使用 localtime_s 代替。

time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime_s ( &rawtime );

When I change localtime to localtime_s I get: error C2660: 'localtime_s' : function does not take 1 arguments

当我将 localtime 更改为 localtime_s 时,我得到: error C2660: 'localtime_s' : function does not take 1 arguments

Here is what I think is going on in the first block of code:

这是我认为在第一个代码块中发生的事情:

  • create an empty time_t variable.
  • create a pointer to timeinfo which is defined in ctime
  • write the rawtime into a rawtime reference
  • convert the rawtime into something meaningful to pedestrians

    1. Am I right?
    2. What second input parameter does localtime_s need?
    3. What's the worst that could happen if I just ignore the whole localtime safety issue.
  • 创建一个空的 time_t 变量。
  • 创建一个指向 ctime 中定义的 timeinfo 的指针
  • 将 rawtime 写入 rawtime 引用
  • 将原始时间转换为对行人有意义的东西

    1. 我对吗?
    2. localtime_s 需要哪个第二个输入参数?
    3. 如果我忽略整个本地时间安全问题,可能发生的最坏情况是什么?

回答by Jerry Coffin

localtimereturns a pointer to a statically allocated struct tm.

localtime返回一个指向静态分配的指针struct tm

With localtime_s, you pass in a pointer to a struct tm, and localtime_swrites its result data into that, so your code would change from:

使用 localtime_s,您传入一个指向 struct tm 的指针,并将localtime_s其结果数据写入其中,因此您的代码将从:

struct tm *timeinfo;
timeinfo = localtime(&rawtime);

to something like:

类似于:

struct tm timeinfo;
localtime_s(&timeinfo, &rawtime);

This way, it's writing to yourbuffer instead of having a buffer of its own.

这样,它会写入您的缓冲区,而不是拥有自己的缓冲区。

回答by ProGirlXOXO

localtime_s is just a microsoft implementation of the localtime functon, you can safely keep using locatimebecaue it's C++ ISO compliant and ony microsoft marked it as "deprecated". The localtime function itself isn't deprecated at all in the C++ world.

localtime_s 只是 localtime functon 的微软实现,您可以安全地继续使用,locatime因为它符合 C++ ISO 标准,并且任何微软都将其标记为“已弃用”。localtime 函数本身在 C++ 世界中根本没有被弃用。

The localtime_sreferencesays that these parameters should be passed to it:

localtime_s参考指出,应将这些参数传递给它:

_tm 
Pointer to the time structure to be filled in.
 time 
Pointer to the stored time.

回答by ForceMagic

As Lightness Races in Orbitpointed out, localtimeis not thread safe as well as several other time function. I wanted to know more about the subject and I found a relevant blog postwith a thoughrough explanation about that.

正如Orbit 中的 Lightness Races指出的那样,localtime它不像其他几个时间函数那样是线程安全的。我想了解更多关于这个主题的信息,我找到了一篇相关的博客文章,对此进行了详尽的解释。

The quote below explains why localtimeis not thread-safe:

下面的引用解释了为什么localtime不是线程安全的:

[...] localtime returns a pointer to a static buffer (std::tm*). Another thread can call the function and the static buffer could be overwritten before the first thread has finished reading the content of the struct std::tm*.

[...] localtime 返回一个指向静态缓冲区的指针 (std::tm*)。另一个线程可以调用该函数,并且可以在第一个线程完成读取 struct std::tm* 的内容之前覆盖静态缓冲区。