关于c++对象全局对象析构的几点记录

转自网上http://www.cppblog.com/lazz/archive/2009/02/25/74904.html

#include<iostream>
using std::cout;
using std::endl;
class CDust{
public:
    CDust()
    {
        cout 
<< " CDust constructor " << endl;
    }

    
~CDust()
    {

        cout 
<< " ~CFoo destructor " << endl;
    }
};


CDust A;

int main()
{
    
return 0;
}

我想类似的代码在网上Google下应该有不少
尝试了下,在vc6.0的情况下,是没有输出 "~CFoo destructor", 但这并不代表 ~CDust() 没有执行.
在~CDust里面设置断点,会发现事实上程序运行时进入了析构函数里  // -_! 表达好牵强
本来想实在跟踪这里开始程序发生了什么调用了什么,发现功底不足,完全不明白,就先打断了
而再在' return 0 ' 语句前面加上断点,会看到这个新加的断点比析构里面的断点先到达,... 
以现在c++的造诣和vc6.0的了解情况来看,头痛了
为什么 return 0 后程序不是正常结束了才去执行 全局对象的析构?

改写下代码
#include < iostream.h >

class  CDust{
public :
    CDust()
    {
        cout 
<<   "  CDust constructor  "   <<  endl;
    }

    
~ CDust()
    {

        cout 
<<   "  ~CFoo destructor  "   <<  endl;
    }
};


CDust A;

int  main()
{

    
return   0 ;

}

这样用旧版本的头文件实现,控制台输出了意外的两个调用 --- 析构函数输出显示了..

是cout在头文件的实现方式不同?

在dev-c++里面,运行第一代代码
根据断点的设置测试,也是先执行了 main()函数里面的 return 0 才进入全局函数的析构,也能发现析构函数里面的输出被调用了,控制台有明确的显示
这样一来,又不明白了...
在vc6.0里面为什么执行了全局函数的析构却没有所谓的输出?
是因为cmd控制台在'return 0'程序权限收回  // 好像扯到系统的一些混乱的旧记忆了...

网游了一下
找到暂时比较清晰的说法是:
   In C++, the constructor of a global object is executed before the main() function(of course after the STARTUP code), while the destructor
is invoked after the main() function. So in my humble opinion, the main() function is a bridge between the constructor and the destructor.Further more, the constructor and the destructor is the actual manager of the whole program, because they can manage all the
resources of the program(for instance, the constructor allocate memory units and the destructor free them.I'am not sure of this, any comments will be appreciated in advance.).
4)In C++, is it true that the resources obtained during the constructor and the destructor (both belong to a global object)are managed by themselves and have nothing with the main() function.Therfore, I think the main() function is not the king in C++ as it is in C. 
   //感谢提出此说法的朋友
 
  _startup才是用户程序的起点和终点? 的确,调用_exit()函数再断点测试,全局对象的destructor是没有进入的机会   //长见识了

  就此先打断... 再深入今晚就这样完了.
  就此记录下,以后再接触



不小心又接触了...
懒得开新的就集中在这里吧

在main()里面手动调用全局对象的析构, 最后程序都会执行两次析构调用... 在<iostream.h>的cout这种情况下明显,在std::cout下还得靠断点设置才能体现到(vc6的情况)
     这里是一种解析
     {
      Once a destructor is invoked for an object, the object no longer exists; the behavior is undefined if the destructor is invoked for an object whose lifetime has ended. [Example: if the destructor for an automatic object is explicitly invoked, and the block is subsequently left in a manner that would ordinarily invoke implicit destruction of the object, the behavior is undefined
}
     非global static object 也会出现两次调用, 区别只在与一个在main() 退出之前,一个在之后...
(的确,手工调用析构函数的情况很少出现  -_! )
     如果我在析构里面存在 释放内存 这一类实现, 那第二次再次释放不是容易出问题!!!
     以后遇到这种情况得注意检测代码的添加...

转自网上http://hi.baidu.com/%D4%C6%C7%E5881989/blog/item/ee2ddf27217e0c1e8b82a176.html

如何修改以下代码,使其输出

begin

hello world

end

Code:
1. #include<iostream>
2. using namespace std;  
3.
4. int
main()
5. {      
6.   cout<<"hello world"<<endl;
7. }
注意不得修改main函数的实现也就是说4-7行的代码是不允许任何修改的。

上面这道题目是今天在CSDN里面的一个问答题,我做了一下,最初的想法还是声明一个全局对象,在构造函数中实现对"begin"的打印,然后调用主函数,最后利用析构函数打印"end",但是哪知道却始终不能显示出析构函数里面的内容,难道全局对象不用析构吗?肯定不可能啊?
仔细一想,cout是是iostream流的一个全局对象,也是要析构的,可能是在析构全局对象的之前就将cout析构了,所以就不能打印"end"了哦!于是将析构函数中的cout换成了标准c语言里面的printf函数进行输出,一试果然得到了正确的结果。于是马上将这个题目提交了,然而提交后所给出的解答是这样的:"
利用全局对象在main之前构造,在main退出之后析构 #include using namespace std; int main() { cout<<"hello world"<<endl;} "
这就奇怪了噻!!怎么我的VC++6.0就显示不出析构函数中的cout中的内容啊?于是马上Baidu了一下,却发现很多网友说vc6不得行,更高的版本就可以,说这是VC++6.0的一个BUG,O(∩_∩)O哈哈~,太气人了噻!!又浪费了我一个积分啊!
不过,全局对象这个概念理解更深了!

哦!对了,下面是我的实现代码:
#include<iostream>
#include<cstdio>  
using namespace std;
class A
{
public:
       A()
       {
         cout<<"begin"<<endl;
       }
       ~A()
       {
           printf("end/n");
       }
};

A a;     //声明全局对象

int main()
{
    cout<<"hello world"<<endl;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值