localtime vs localtime_s和适当的输入参数 [英] localtime vs localtime_s and appropriate input arguments

查看:2668
本文介绍了localtime vs localtime_s和适当的输入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这会返回:warning C4996:'localtime':此函数或变量可能不安全。请考虑使用localtime_s。

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

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

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

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

这是我认为在第一个代码块:

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


  • 创建一个空的time_t变量

  • 创建一个指向在ctime中定义的timeinfo的指针

  • 将rawtime写入rawtime参考

  • 将原始时间转换为对行人有意义的东西

  • 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. localtime_s需要什么第二个输入参数?

  2. 如果我忽略整个本地时间安全问题,最糟糕的情况是什么。


推荐答案

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

localtime returns a pointer to a statically allocated struct tm.

通过localtime_s, tm和 localtime_s 将其结果数据写入其中,因此您的代码将更改为:

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

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

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

这样,它正在写入 缓冲区,它自己的。

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

这篇关于localtime vs localtime_s和适当的输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆