之前, 我们讲过不可重入函数, 现在, 我们继续深挖一下。 我说过,大道至简, 能把复杂的问题简化, 那是一种可贵的能力。
我们先看一个超级简单的可重入函数, 上菜:
- #include <iostream>
 - using namespace std;
 -  
 - int fun()
 - {
 - 	int a = 0;
 - 	a++;
 -  
 - 	return a;
 - }
 -  
 - int main()
 - {
 - 	cout << fun() << endl;
 - 	cout << fun() << endl;
 -  
 - 	return 0;
 - }
 
 
我相信, 只要你能找到我的博客,会读到该博文,那么你肯定知道上述的结果是
1
1
上面的fun函数就是可重入函数, 下面我们看看不可重入的fun函数;
- #include <iostream>
 - using namespace std;
 -  
 - int fun()
 - {
 - 	static int a = 0;
 - 	a++;
 -  
 - 	return a;
 - }
 -  
 - int main()
 - {
 - 	cout << fun() << endl;
 - 	cout << fun() << endl;
 -  
 - 	return 0;
 - }
 
 
只要你不是特别菜, 那么, 你肯定知道上述的结果是
1
2
好, 继续上菜,如果你水平还可以, 你肯定不会知道下面程序的结果:
- #include <iostream>
 - using namespace std;
 -  
 - int *fun(int x)
 - {
 - 	int a = x;
 - 	return &a;
 - }
 -  
 - int main()
 - {
 - 	int *p = fun(10);
 - 	int *q = fun(20);
 - 	cout << p << endl;
 - 	cout << q << endl;
 - 	cout << *p << endl;
 - 	cout << *q << endl;
 -  
 - 	return 0;
 - }
 
 
结果是(我在VC++6.0中运行):
0013FF1C
 0013FF1C
 4200781
 4200781
为什么是这么多呢? 因为返回栈指针后, 栈指针指向的值被淹没了。
继续看程序:
- #include <iostream>
 - using namespace std;
 -  
 - int *fun(int x)
 - {
 - 	static int a = x;
 - 	return &a;
 - }
 -  
 - int main()
 - {
 - 	int *p = fun(10);
 - 	int *q = fun(20);
 - 	cout << p << endl;
 - 	cout << q << endl;
 - 	cout << *p << endl;
 - 	cout << *q << endl;
 -  
 - 	return 0;
 - }
 
 
结果为:
0047BDEC
 0047BDEC
 10
 10
     为什么呢? 因为static int形式的a只会被初始化一次。
继续上菜:
- #include <iostream>
 - using namespace std;
 -  
 - int *fun(int x)
 - {
 - 	static int a = 0;
 - 	a = x;
 - 	return &a;
 - }
 -  
 - int main()
 - {
 - 	int *p = fun(10);
 - 	int *q = fun(20);
 - 	cout << p << endl;
 - 	cout << q << endl;
 - 	cout << *p << endl;
 - 	cout << *q << endl;
 -  
 - 	return 0;
 - }
 
 
结果为:
0047BDEC
 0047BDEC
 20
 20
为什么都为20呢? 因为第一次的值被替换了。
再次上菜, 爱吃不吃:
- #include <iostream>
 - using namespace std;
 -  
 - int *fun(int x)
 - {
 - 	static int a = 0;
 - 	a = x;
 - 	return &a;
 - }
 -  
 - int main()
 - {
 - 	int *p = fun(10);
 - 	cout << p << endl;
 - 	cout << *p << endl;
 - 	
 - 	int *q = fun(20);
 - 	cout << q << endl;	
 - 	cout << *q << endl;
 -  
 - 	return 0;
 - }
 
 
0047BDEC
 10
 0047BDEC
 20
这才对呀。 可见, 不可重入函数, 十恶不赦。
下面,我们来看看不可重入函数localtime:
- #include <iostream>
 - #include <ctime>
 - using namespace std;
 -  
 - int main()
 - {
 -     time_t tNow =time(NULL);
 -     time_t tEnd = tNow + 3600;
 -  
 -     struct tm* ptm = localtime(&tNow);
 - 	struct tm* ptmEnd = localtime(&tEnd);
 -  
 -     char szTmp[100] = {0};
 -     strftime(szTmp, 100, "%H:%M:%S",ptm);
 -  
 -     char szEnd[100] = {0};
 -     strftime(szEnd, 100, "%H:%M:%S",ptmEnd);
 -     
 -     printf("%s\n", szTmp);
 -     printf("%s\n", szEnd);
 -     
 -     return 0;
 - }
 
 
结果:
00:39:24
 00:39:24
哈哈, 一样的。
再上佳肴:
- #include <iostream>
 - #include <ctime>
 - using namespace std;
 -  
 - int main()
 - {
 -     time_t tNow =time(NULL);
 -     time_t tEnd = tNow + 3600;
 -  
 -     struct tm* ptm = localtime(&tNow);
 -     char szTmp[100] = {0};
 -     strftime(szTmp, 100, "%H:%M:%S",ptm);
 -  
 - 	struct tm* ptmEnd = localtime(&tEnd);
 -     char szEnd[100] = {0};
 -     strftime(szEnd, 100, "%H:%M:%S",ptmEnd);
 -     
 -     printf("%s\n", szTmp);
 -     printf("%s\n", szEnd);
 -     
 -     return 0;
 - }
 
 
结果为:
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块钱买了个苹果, 吃掉, 睡觉。
  
  

                

















