关键词搜索

源码搜索 ×
×

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

发布2014-04-24浏览16211次

详情内容

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

        

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

 

  1. #include <iostream>
  2. using namespace std;
  3. int fun()
  4. {
  5. int a = 0;
  6. a++;
  7. return a;
  8. }
  9. int main()
  10. {
  11. cout << fun() << endl;
  12. cout << fun() << endl;
  13. return 0;
  14. }

      我相信, 只要你能找到我的博客,会读到该博文,那么你肯定知道上述的结果是

 

1

1

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

 

  1. #include <iostream>
  2. using namespace std;
  3. int fun()
  4. {
  5. static int a = 0;
  6. a++;
  7. return a;
  8. }
  9. int main()
  10. {
  11. cout << fun() << endl;
  12. cout << fun() << endl;
  13. return 0;
  14. }

     只要你不是特别菜, 那么, 你肯定知道上述的结果是

 

1

2

 

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

 

  1. #include <iostream>
  2. using namespace std;
  3. int *fun(int x)
  4. {
  5. int a = x;
  6. return &a;
  7. }
  8. int main()
  9. {
  10. int *p = fun(10);
  11. int *q = fun(20);
  12. cout << p << endl;
  13. cout << q << endl;
  14. cout << *p << endl;
  15. cout << *q << endl;
  16. return 0;
  17. }

    结果是(我在VC++6.0中运行):

 

0013FF1C
0013FF1C
4200781
4200781

 

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

 

       继续看程序:

 

  1. #include <iostream>
  2. using namespace std;
  3. int *fun(int x)
  4. {
  5. static int a = x;
  6. return &a;
  7. }
  8. int main()
  9. {
  10. int *p = fun(10);
  11. int *q = fun(20);
  12. cout << p << endl;
  13. cout << q << endl;
  14. cout << *p << endl;
  15. cout << *q << endl;
  16. return 0;
  17. }

     结果为:

 

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

 

    继续上菜:

 

  1. #include <iostream>
  2. using namespace std;
  3. int *fun(int x)
  4. {
  5. static int a = 0;
  6. a = x;
  7. return &a;
  8. }
  9. int main()
  10. {
  11. int *p = fun(10);
  12. int *q = fun(20);
  13. cout << p << endl;
  14. cout << q << endl;
  15. cout << *p << endl;
  16. cout << *q << endl;
  17. return 0;
  18. }

     结果为:

 

0047BDEC
0047BDEC
20
20

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

 

     再次上菜, 爱吃不吃:

 

  1. #include <iostream>
  2. using namespace std;
  3. int *fun(int x)
  4. {
  5. static int a = 0;
  6. a = x;
  7. return &a;
  8. }
  9. int main()
  10. {
  11. int *p = fun(10);
  12. cout << p << endl;
  13. cout << *p << endl;
  14. int *q = fun(20);
  15. cout << q << endl;
  16. cout << *q << endl;
  17. return 0;
  18. }

 

 

 

0047BDEC
10
0047BDEC
20

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

 

 

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

 

  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4. int main()
  5. {
  6. time_t tNow =time(NULL);
  7. time_t tEnd = tNow + 3600;
  8. struct tm* ptm = localtime(&tNow);
  9. struct tm* ptmEnd = localtime(&tEnd);
  10. char szTmp[100] = {0};
  11. strftime(szTmp, 100, "%H:%M:%S",ptm);
  12. char szEnd[100] = {0};
  13. strftime(szEnd, 100, "%H:%M:%S",ptmEnd);
  14. printf("%s\n", szTmp);
  15. printf("%s\n", szEnd);
  16. return 0;
  17. }

     结果:

 

00:39:24
00:39:24

    哈哈, 一样的。

 

     再上佳肴:

 

  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4. int main()
  5. {
  6. time_t tNow =time(NULL);
  7. time_t tEnd = tNow + 3600;
  8. struct tm* ptm = localtime(&tNow);
  9. char szTmp[100] = {0};
  10. strftime(szTmp, 100, "%H:%M:%S",ptm);
  11. struct tm* ptmEnd = localtime(&tEnd);
  12. char szEnd[100] = {0};
  13. strftime(szEnd, 100, "%H:%M:%S",ptmEnd);
  14. printf("%s\n", szTmp);
  15. printf("%s\n", szEnd);
  16. return 0;
  17. }

 

     结果为:

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块钱买了个苹果, 吃掉, 睡觉。

 

 


 

 

 


 

相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载