• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            關于c++對象全局對象析構的幾點記錄

            Posted on 2009-02-25 20:35 藍塵 閱讀(1574) 評論(2)  編輯 收藏 引用 所屬分類: C++
            #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() 退出之前,一個在之后...
            (的確,手工調用析構函數的情況很少出現  -_! )
                 如果我在析構里面存在 釋放內存 這一類實現, 那第二次再次釋放不是容易出問題!!!
                 以后遇到這種情況得注意檢測代碼的添加...

            Feedback

            # re: 關于c++對象全局對象析構的幾點記錄  回復  更多評論   

            2009-02-26 12:05 by 陳梓瀚(vczh)
            說不定因為cout被析構了,你的代碼就沒看到輸出了。

            # re: 關于c++對象全局對象析構的幾點記錄  回復  更多評論   

            2009-02-27 19:28 by 藍塵
            @陳梓瀚(vczh)
            std::cout 被析構, 而且是只在vc6.0下面的 global static object ? 也就是說剩下的就是編譯器的實現問題了(網上的說法好像只有std::cout在vc6有這個情況)
            等有時間時調試跟蹤下...
            多謝提醒了

            Copyright © 藍塵

            久久香蕉国产线看观看精品yw| 久久青青草原精品影院| 一级做a爰片久久毛片免费陪| 日本高清无卡码一区二区久久| 久久受www免费人成_看片中文| 欧美一区二区三区久久综| 国产精品一区二区久久| 深夜久久AAAAA级毛片免费看| 久久精品国产清自在天天线| 欧美精品一区二区精品久久 | 精品少妇人妻av无码久久| 国产99久久久久久免费看| 精品久久久久久中文字幕大豆网| 国内精品久久人妻互换| 伊人久久大香线蕉无码麻豆| avtt天堂网久久精品| 一本色道久久99一综合| 久久嫩草影院免费看夜色| 国产亚洲欧美成人久久片| 色婷婷综合久久久久中文一区二区 | 伊人久久大香线蕉亚洲| 久久久亚洲精品蜜桃臀| 亚洲成人精品久久| 粉嫩小泬无遮挡久久久久久| 久久精品久久久久观看99水蜜桃 | 人妻少妇久久中文字幕一区二区| 国产AV影片久久久久久| 久久777国产线看观看精品| 久久99精品久久久久久久久久| 欧美亚洲国产精品久久| 久久久高清免费视频| 亚洲国产精品嫩草影院久久| 久久综合五月丁香久久激情| 成人a毛片久久免费播放| 国产无套内射久久久国产| 91精品国产91久久| 亚洲一本综合久久| 久久精品成人一区二区三区| 久久人妻少妇嫩草AV蜜桃| 要久久爱在线免费观看| 久久精品国产日本波多野结衣|