• <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>
            面對現實,超越自己
            逆水行舟,不進則退
            posts - 269,comments - 32,trackbacks - 0

               解釋以下語句的含義:
                     1
            new A;
                     2
            、new A();    
               也許很多人包括我自己,都可以馬上給出第一種情況的答案:在堆上為A類分配內存,然后調用A的構造函數。這種說法被大家所熟知,因為包括《STL源碼剖析》等大作在內也都是這么寫的(但是你認為這種說法完全正確嗎?
            其實不盡然,答案后面揭曉)
                第二種情況,對象構造的時候初始化列表為空會和第一種有什么不同呢?對于這種在實際工程中很少使用的情況,我一時還真給不出確切的答案。

               網上搜了一下,看到CSDN里面還有專門針對這個問題的一個帖子(原帖鏈接 http://bbs.csdn.net/topics/320161716)。

               好像最終也沒有可以信服的答案,認同度比較高的是這樣的說法:
            加括號調用沒有參數的構造函數,不加括號調用默認構造函數或唯一的構造函數,看需求peakflys注:這種說法是錯誤的,答案后面揭曉)
               既然沒有特別靠譜的答案,不如自己動手找出答案。

               構造以下示例:

            /**
             *\brief example1 difference between new and new()
             *\author peakflys
             *\data 12:10:24 Monday, April 08, 2013
             
            */

            class A
            {
            public:
                int a;
            };

            int main()
            {
                A *pa = new A;
                A *paa = new A();
                return 0;
            }

            查看main函數的匯編代碼(編譯器:gcc (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4) )

            int main()
            {
              4005c4:   55                      push   %rbp
              4005c5:   48 89 e5                mov    %rsp,%rbp
              4005c8:   48 83 ec 10             sub    $0x10,%rsp
                A *pa = new A;
              4005cc:   bf 04 00 00 00          mov    $0x4,%edi
              4005d1:   e8 f2 fe ff ff          callq  4004c8 <_Znwm@plt>         //調用new
              4005d6:   48 89 45 f0             mov    %rax,-0x10(%rbp)           //rax寄存器內容賦給指針pa(rax寄存器里是new調用產生的A對象堆內存地址)
                A *paa = new A();
              4005da:   bf 04 00 00 00          mov    $0x4,%edi
              4005df:   e8 e4 fe ff ff          callq  4004c8 <_Znwm@plt>         //調用new
              4005e4:   48 89 c2                mov    %rax,%rdx                      //rax的內容放入rdx,執行之后,rdx里存放的即是通過new A()產生的內存地址
              4005e7:   c7 02 00 00 00 00       movl   $0x0,(%rdx)                 //把rdx內存指向的內容賦為0值,即把A::a賦值為0
              4005ed:   48 89 45 f8             mov    %rax,-0x8(%rbp)             //rax寄存器內容賦給指針paa(rax寄存器里是new()調用產生的A對象堆內存地址)
                 return 0;
              4005f1:   b8 00 00 00 00          mov    $0x0,%eax
            }
              4005f6:   c9                      leaveq 
              4005f7:   c3                      retq
                通過上面產生的匯編代碼(AT&T匯編不熟悉的可以看注釋)可以很容易看出,new A()的執行,在調用完operator new分配內存后,馬上對新分配內存中的對象使用0值初始化,而new A 僅僅是調用了operator new分配內存!
               是不是這樣就可以下結論 new A()new A多了一步,即初始化對象的步驟呢?

               我們再看看下面這種情況:

            /**
             *\brief example2 difference between new and new()
             *\author peakflys
             *\data 12:23:20 Monday, April 08, 2013
             
            */

            class A
            {
            public:
                A(){a = 10;}
                int a;
            };

            int main()
            {
                A *pa = new A;
                A *paa = new A();
                return 0;
            }

               這種情況是類顯示提供含默認值的構造函數。
               查看匯編實現如下:

            int main()
            {
              4005c4:   55                      push   %rbp
              4005c5:   48 89 e5                mov    %rsp,%rbp
              4005c8:   53                      push   %rbx
              4005c9:   48 83 ec 18             sub    $0x18,%rsp
                A *pa = new A;
              4005cd:   bf 04 00 00 00          mov    $0x4,%edi
              4005d2:   e8 f1 fe ff ff          callq  4004c8 <_Znwm@plt>
              4005d7:   48 89 c3                mov    %rax,%rbx
              4005da:   48 89 d8                mov    %rbx,%rax
              4005dd:   48 89 c7                mov    %rax,%rdi
              4005e0:   e8 2d 00 00 00          callq  400612 <_ZN1AC1Ev>
              4005e5:   48 89 5d e0             mov    %rbx,-0x20(%rbp)
                A *paa = new A();
              4005e9:   bf 04 00 00 00          mov    $0x4,%edi
              4005ee:   e8 d5 fe ff ff          callq  4004c8 <_Znwm@plt>
              4005f3:   48 89 c3                mov    %rax,%rbx
              4005f6:   48 89 d8                mov    %rbx,%rax
              4005f9:   48 89 c7                mov    %rax,%rdi
              4005fc:   e8 11 00 00 00          callq  400612 <_ZN1AC1Ev>
              400601:   48 89 5d e8             mov    %rbx,-0x18(%rbp)
                return 0;
              400605:   b8 00 00 00 00          mov    $0x0,%eax
            }
              40060a:   48 83 c4 18             add    $0x18,%rsp
              40060e:   5b                      pop    %rbx
              40060f:   c9                      leaveq 
              400610:   c3                      retq 

               上面的匯編代碼就不在添加注釋了,因為兩種操作產生的匯編代碼是一樣的,都是先調用operator new分配內存,然后調用構造函數。
               上面的情況在VS2010下驗證是一樣的情況,有興趣的朋友可以自己去看,這里就不再貼出VS2010下的匯編代碼了。

               通過上面的分析,對于
            new A new A() 的區別,我們可以得出下面的結論:
                  
            1、類體含有顯示適合地默認構造函數時,new Anew A()的作用一致,都是首先調用operator new分配內存,然后調用默認構造函數初始化對象。
                 2、類體無顯示構造函數時,new A()首先調用operator new來為對象分配內存,然后使用空值初始化對象成員變量,而new A僅僅是調用operator new分配內存,對象的成員變量是無意義的隨機值!  peakflys注:對于基本數據類型,如int 適用此條)
               注意到,現在很多書籍對new操作符的說明都存在紕漏,例如《STL源碼剖析》中2.2.2節中有以下的描述:


            事實證明,new Foo的操作是否有構造函數的調用是不確定的,具體要看Foo類體里是否有顯示構造函數的出現。

            /*****************************************華麗分割線**************************************
            補充:剛才發現,在C++Primer第四版5.11節中,已經有了對于new A()的說明:

               We indicate that we want to value-initialize the newly allocated object by following the type nameby a pair of empty parentheses. The empty parentheses signal that we want initialization but arenot supplying a specific initial value. In the case of class types (such as string) that define their own constructors, requesting value-initialization is of no consequence: The object is initialized by running the default constructor whether we leave it apparently uninitialized orask for value-initialization. In the case of built-in types or types that do not define any constructors, the difference is significant

                 int *pi = new int;         // pi points to an uninitialized int 

                 int *pi = new int();       // pi points to an int value-initialized to 0 

            In the first case, the int is uninitialized; in the second case, the int is initialized to zero.
               這里給出的解釋和上面自己分析的new A()的行為是一致的。

            /***************************************再次華麗分割線************************************
            鑒于上面的結論是通過GCCVS2010得出的,而且有朋友也提出同樣的質疑,為了確定這種結果是否是編譯器相關的,剛才特意查看了一下C++的標準化文檔。
            摘自:ISO/IEC 14882:2003(E) 5.3.4 - 15
            — If the new-initializer is omitted:
                  — If T is a (possibly cv-qualified) non-POD class type (or array thereof), the object is default-initialized(8.5). If T is a const-qualified type, the underlying class type shall have a user-declared default constructor.
                  — Otherwise, the object created has indeterminate value. If T is a const-qualified type, or a (possibly cv-qualified) POD class type (or array thereof) containing (directly or indirectly) a member of const-qualified type, the program is ill-formed;
            — If the new-initializer is of the form (), the item is value-initialized (8.5);

            所以可以確定,這種情況完全是編譯器無關的(當然那些不完全按照標準實現的編譯器除外)。
            但是通過上面標準化文檔的描述,我們可以看出文中對new A在無顯示構造函數時的總結并不是特別準確,鑒于很多公司都有這道面試題(撇去這些題目的實際考察意義不說),我們有必要再補充一下: 對于new A: 這樣的語句,再調用完operator new分配內存之后,如果A類體內含有POD類型,則POD類型的成員變量處于未定義狀態,如果含有非POD類型則調用該類型的默認構造函數。而 new A()在這些情況下都會初始化。
               PS:估計很多公司的正確答案“ 也不一定正確吧。

             

            本文轉自:http://m.shnenglu.com/peakflys/archive/2013/04/08/199208.html

             

            posted on 2013-04-18 11:03 王海光 閱讀(467) 評論(0)  編輯 收藏 引用 所屬分類: 其他
            国产精品免费久久久久电影网| 久久国产精品免费一区| 天天躁日日躁狠狠久久| 久久香蕉国产线看观看乱码| 99久久国产综合精品网成人影院| 久久这里只精品99re66| 亚洲精品高清国产一线久久| 久久这里只精品国产99热| 久久综合久久伊人| 国产成人综合久久综合| 国产精品久久久久久五月尺| a高清免费毛片久久| 欧美成人免费观看久久| 国产精品久久久久乳精品爆| 久久久久亚洲AV片无码下载蜜桃| 丁香五月综合久久激情| 国内精品久久久久影院优| 免费精品久久天干天干| 99久久精品无码一区二区毛片| 久久久久久午夜成人影院| 欧美日韩中文字幕久久久不卡 | 久久久噜噜噜久久| 狠狠久久亚洲欧美专区| 久久久久AV综合网成人| 无码人妻久久久一区二区三区| 国产A三级久久精品| 亚洲&#228;v永久无码精品天堂久久 | 中文字幕无码av激情不卡久久| 国产AV影片久久久久久| 久久青青草原国产精品免费| 国产69精品久久久久777| 999久久久免费精品国产| 久久久国产乱子伦精品作者| 青草国产精品久久久久久| 亚洲AV无码久久精品成人| 亚洲精品美女久久777777| 久久人与动人物a级毛片| 亚洲精品无码久久久久久| 久久精品人人做人人爽97| 久久国产热精品波多野结衣AV| 久久精品国产亚洲av高清漫画|