• <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://www.shnenglu.com/peakflys/archive/2013/04/08/199208.html

             

            posted on 2013-04-18 11:03 王海光 閱讀(467) 評論(0)  編輯 收藏 引用 所屬分類: 其他
            久久人人爽人人人人片av| 久久青青草原精品影院| 国产亚洲色婷婷久久99精品91| 69SEX久久精品国产麻豆| 99久久免费国产精品| 无夜精品久久久久久| 久久久久国产精品人妻| 久久国产精品无码HDAV | 精品久久久久成人码免费动漫| 久久久久亚洲AV成人网人人网站| 久久久无码精品亚洲日韩按摩| 久久青青草原综合伊人| 久久久久久精品成人免费图片| 国产亚洲婷婷香蕉久久精品| 久久婷婷是五月综合色狠狠| 久久99国产精品久久| 久久精品人人做人人爽电影 | 久久久亚洲欧洲日产国码二区| 99久久国产综合精品成人影院| 老男人久久青草av高清| 国产亚洲成人久久| 久久99国产乱子伦精品免费| 性欧美大战久久久久久久| 情人伊人久久综合亚洲| 久久er99热精品一区二区| 久久久久亚洲AV片无码下载蜜桃| 久久99中文字幕久久| 热re99久久精品国99热| 亚洲日本久久久午夜精品| 99久久99久久精品国产片| 麻豆精品久久久一区二区| 久久久国产打桩机| 久久久久国产精品人妻| 欧美久久一区二区三区| 国产精品午夜久久| 久久香蕉国产线看观看乱码| 国产精品久久久久影视不卡| 蜜臀av性久久久久蜜臀aⅴ | 日本福利片国产午夜久久| 国产精品一区二区久久| 精品久久久久久|