再次深入分析不可重入函数---请小心使用localtime函数

本文通过多个示例深入分析了不可重入函数的特点及潜在问题,并以localtime函数为例探讨了其在多线程环境下的风险。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

再次深入分析不可重入函数---请小心使用localtime函数

  2792人阅读  评论(2)  收藏  举报
  分类:
 

        之前, 我们讲过不可重入函数, 现在, 我们继续深挖一下。 我说过,大道至简, 能把复杂的问题简化, 那是一种可贵的能力。

        

        我们先看一个超级简单的可重入函数, 上菜:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int fun()  
  5. {  
  6.     int a = 0;  
  7.     a++;  
  8.   
  9.     return a;  
  10. }  
  11.   
  12. int main()  
  13. {  
  14.     cout << fun() << endl;  
  15.     cout << fun() << endl;  
  16.   
  17.     return 0;  
  18. }  
      我相信, 只要你能找到我的博客,会读到该博文,那么你肯定知道上述的结果是

1

1

      上面的fun函数就是可重入函数, 下面我们看看不可重入的fun函数;

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int fun()  
  5. {  
  6.     static int a = 0;  
  7.     a++;  
  8.   
  9.     return a;  
  10. }  
  11.   
  12. int main()  
  13. {  
  14.     cout << fun() << endl;  
  15.     cout << fun() << endl;  
  16.   
  17.     return 0;  
  18. }  
     只要你不是特别菜, 那么, 你肯定知道上述的结果是

1

2


    好, 继续上菜,如果你水平还可以, 你肯定不会知道下面程序的结果:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int *fun(int x)  
  5. {  
  6.     int a = x;  
  7.     return &a;  
  8. }  
  9.   
  10. int main()  
  11. {  
  12.     int *p = fun(10);  
  13.     int *q = fun(20);  
  14.     cout << p << endl;  
  15.     cout << q << endl;  
  16.     cout << *p << endl;  
  17.     cout << *q << endl;  
  18.   
  19.     return 0;  
  20. }  
    结果是(我在VC++6.0中运行):

0013FF1C
0013FF1C
4200781
4200781


      为什么是这么多呢? 因为返回栈指针后, 栈指针指向的值被淹没了。


       继续看程序:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int *fun(int x)  
  5. {  
  6.     static int a = x;  
  7.     return &a;  
  8. }  
  9.   
  10. int main()  
  11. {  
  12.     int *p = fun(10);  
  13.     int *q = fun(20);  
  14.     cout << p << endl;  
  15.     cout << q << endl;  
  16.     cout << *p << endl;  
  17.     cout << *q << endl;  
  18.   
  19.     return 0;  
  20. }  
     结果为:

0047BDEC
0047BDEC
10
10
    为什么呢? 因为static int形式的a只会被初始化一次。


    继续上菜:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int *fun(int x)  
  5. {  
  6.     static int a = 0;  
  7.     a = x;  
  8.     return &a;  
  9. }  
  10.   
  11. int main()  
  12. {  
  13.     int *p = fun(10);  
  14.     int *q = fun(20);  
  15.     cout << p << endl;  
  16.     cout << q << endl;  
  17.     cout << *p << endl;  
  18.     cout << *q << endl;  
  19.   
  20.     return 0;  
  21. }  
     结果为:

0047BDEC
0047BDEC
20
20

    为什么都为20呢? 因为第一次的值被替换了。


     再次上菜, 爱吃不吃:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int *fun(int x)  
  5. {  
  6.     static int a = 0;  
  7.     a = x;  
  8.     return &a;  
  9. }  
  10.   
  11. int main()  
  12. {  
  13.     int *p = fun(10);  
  14.     cout << p << endl;  
  15.     cout << *p << endl;  
  16.       
  17.     int *q = fun(20);  
  18.     cout << q << endl;    
  19.     cout << *q << endl;  
  20.   
  21.     return 0;  
  22. }  

0047BDEC
10
0047BDEC
20

    这才对呀。 可见, 不可重入函数, 十恶不赦。



      下面,我们来看看不可重入函数localtime:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <ctime>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     time_t tNow =time(NULL);  
  8.     time_t tEnd = tNow + 3600;  
  9.   
  10.     struct tm* ptm = localtime(&tNow);  
  11.     struct tm* ptmEnd = localtime(&tEnd);  
  12.   
  13.     char szTmp[100] = {0};  
  14.     strftime(szTmp, 100, "%H:%M:%S",ptm);  
  15.   
  16.     char szEnd[100] = {0};  
  17.     strftime(szEnd, 100, "%H:%M:%S",ptmEnd);  
  18.       
  19.     printf("%s\n", szTmp);  
  20.     printf("%s\n", szEnd);  
  21.       
  22.     return 0;  
  23. }  
     结果:

00:39:24
00:39:24

    哈哈, 一样的。


     再上佳肴:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <ctime>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     time_t tNow =time(NULL);  
  8.     time_t tEnd = tNow + 3600;  
  9.   
  10.     struct tm* ptm = localtime(&tNow);  
  11.     char szTmp[100] = {0};  
  12.     strftime(szTmp, 100, "%H:%M:%S",ptm);  
  13.   
  14.     struct tm* ptmEnd = localtime(&tEnd);  
  15.     char szEnd[100] = {0};  
  16.     strftime(szEnd, 100, "%H:%M:%S",ptmEnd);  
  17.       
  18.     printf("%s\n", szTmp);  
  19.     printf("%s\n", szEnd);  
  20.       
  21.     return 0;  
  22. }  

     结果为:

23:40:37
00:40:37

     这回算是正确了, 可重入函数, 确实是个孬种啊。


      下面, 我来引用http://blog.csdn.NET/aican_yu/article/details/7012019中的一段话, 我认为分析得相当好:

     localtime函数实现的问题:
     该函数返回的是一个指针,表示某一个地址。大家知道,如果是一个非静态的局部变量,返回它的地址是错误的做法,因为非静态的局部变量在函数返回时,已经被销毁了,它的地址成为无用的地址。因此localtime函数返回的指针只有以下三种可能:要么是一个静态变量的地址,要么是一个全局变量的地址,或者是使用malloc等函数在堆上分配的空间。

     对于最后一种情况,因为标准并没有规定可以对localtime返回的地址进行free,所以如果localtime函数是使用malloc函数分配空间的话,程序员不会使用free函数去释放它,因此造成内存泄露,这是不好的做法。
      前两种情况其实是非常相似的,这里仅以第一种情况来做说明。如果localtime函数使用静态变量,则它大致像下面这个样子:
struct tm* localtime(const time_t* ptr)
{
    static struct tm ret;
    // 在这里计算并得到ret的值
    return &ret;
}
    如果真是这样的话,不论调用多少次localtime,则它返回的地址都是一样的。只是地址中保存的内容可能不同而已。

    比较正确的做法是:
struct tm* temp;
struct tm* temp1;
struct tm tm1;
struct tm tm2;
temp = get_local_time();
tm1 = *temp;
sleep(3);
temp1 = get_local_time();
tm2 = *temp1;
     则虽然可能有temp == temp1,但是tm1和tm2则会不同。静态变量在进行多线程编程时是危险的,因此微软搞出了一个新的localtime_s函数来取代localtime函数(新版本的VC有这个函数,VC 6.0则似乎没有,Linux系统我并不清楚)。这两个函数在功能上一致,只是localtime返回地址,而localtime_s是传入一个地址,让函数填充其内容。后者不需要使用静态变量,在多线程的情况下更安全。


    

      花近10块钱买了个苹果, 吃掉, 睡觉。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值