• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              62 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            上午一個師弟在QQ上問我一道筆試題,是他前兩天去KONAMI面試時做的,這道題大致是這樣的:
                    解釋以下語句的含義:
                     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 A和new A()的作用一致,都是首先調用operator new分配內存,然后調用默認構造函數初始化對象。
                  2、類體無顯示構造函數時,new A()首先調用operator new來為對象分配內存,然后使用空值初始化對象成員變量,而new A僅僅是調用operator new分配內存,對象的成員變量是無意義的隨機值!  (peakflys注:對于基本數據類型,如int等 適用此條)
               注意到,現在很多書籍對new操作符的說明都存在紕漏,例如《STL源碼剖析》中2.2.2節中有以下的描述:

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

                                                                                                            by peakflys 13:40:00 Monday, April 08, 2013

            /*****************************************華麗分割線**************************************
            補充:剛才發現,在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()的行為是一致的。
            /***************************************再次華麗分割線************************************
            鑒于上面的結論是通過GCC和VS2010得出的,而且有朋友也提出同樣的質疑,為了確定這種結果是否是編譯器相關的,剛才特意查看了一下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()在這些情況下都會初始化。
            posted on 2013-04-14 00:51 沛沛 閱讀(371) 評論(0)  編輯 收藏 引用 所屬分類: C++
            欧美久久天天综合香蕉伊| 久久精品无码一区二区三区| 一本一道久久a久久精品综合| 久久九色综合九色99伊人| 久久只有这里有精品4| 亚洲精品国产字幕久久不卡| 久久Av无码精品人妻系列| 久久99精品久久久久久噜噜| 久久这里只精品99re66| 国产精品99久久精品| 午夜精品久久久久| 青青国产成人久久91网| 少妇人妻综合久久中文字幕| 亚洲国产精品久久久久网站 | 国产成人精品久久一区二区三区av | 亚洲国产精品久久| 久久天天躁狠狠躁夜夜2020一 | 亚洲愉拍99热成人精品热久久| 99999久久久久久亚洲| 一本综合久久国产二区| 嫩草影院久久99| 精品国产乱码久久久久久1区2区| 狠狠人妻久久久久久综合蜜桃| 久久精品中文字幕无码绿巨人| 中文字幕无码久久精品青草| 青青青国产成人久久111网站| 丁香色欲久久久久久综合网| 亚洲另类欧美综合久久图片区| 国产99久久久久久免费看| 久久久久久狠狠丁香| 99久久人妻无码精品系列蜜桃| 久久久国产精华液| 亚洲午夜久久久| 伊人久久国产免费观看视频| 亚洲日本va午夜中文字幕久久| 中文字幕成人精品久久不卡| 91精品国产高清久久久久久io| 成人久久综合网| 国产精品久久久久天天影视| 亚洲国产精品人久久| 一本色道久久88加勒比—综合|