localtime返回值造成的一个bug
2013-10-09 08:45
387 查看
/article/8561720.html
最近写了下面的代码
上面代码出现的bug就是 if( day>=1 )这个条件从来不为真,然后我就调试跟踪了下发现day的值每次是0,再仔细一瞧发现 tmptime和tptime的地址竟然是一样的,所以这两个从来都是相等的,然后网上查了下资料关于localtime的返回值说明
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 specified by SUSv2, and available since libc 5.2.5.
原来localtime返回的是静态数据段,难怪了,要安全点还是用 localtime_r为好,就是麻烦点
The localtime_r() function does the same, but stores the data in a user-supplied struct
需要自己申请和释放内存了
其实仔细想想localtime的用法应该就能猜测出来,返回一个指针,而且不需要自己管理内存,基本上是静态数据差不离了,有些还不是不要想当然的好
最近写了下面的代码
time_t _time = (time_t)lastUpdTime;;//lastUpdTime从某处加载的 tm *tptime = localtime(&_time); time_t now; time(&now); tm* tmptime = localtime(&now); int32 day = tmptime->tm_yday - tptime->tm_yday; if(day >= 1) { ... }
上面代码出现的bug就是 if( day>=1 )这个条件从来不为真,然后我就调试跟踪了下发现day的值每次是0,再仔细一瞧发现 tmptime和tptime的地址竟然是一样的,所以这两个从来都是相等的,然后网上查了下资料关于localtime的返回值说明
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 specified by SUSv2, and available since libc 5.2.5.
原来localtime返回的是静态数据段,难怪了,要安全点还是用 localtime_r为好,就是麻烦点
The localtime_r() function does the same, but stores the data in a user-supplied struct
需要自己申请和释放内存了
其实仔细想想localtime的用法应该就能猜测出来,返回一个指针,而且不需要自己管理内存,基本上是静态数据差不离了,有些还不是不要想当然的好
相关文章推荐
- 挨踢项目求生法则-需求篇、战略篇以及设计篇
- 排序算法(1):主流排序算法概述
- 使用Apache POI精确设置Excel的行高和列宽
- 便于理解mysql内幕的各种逻辑图组
- unknown software exception(0x80000003)
- pair work-Elevator Schedule附加题
- Deep Learning Tutorials
- C++重载输入和输出操作符以及IO标准库中的刷新输入缓冲区残留字符问题
- 有了malloc/free为什么还要new/delete ?
- Android自定义组件开发之onMeasure使用
- ARM裸机程序开发12指令条件码的使用
- Caused by: org.apache.catalina.LifecycleException:eclipse中的tomcat启动不了解决之一。
- seo推广对企业的好处
- 一些纯css3写的公司logo
- SVN强制解锁操作
- Android各种访问权限Permission详解
- Word Break II
- C#中使用反射获取结构体实例
- 一些纯css3写的公司logo
- a