• <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>

            從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū)

                                                                                                              peakflys原創(chuàng)作品,轉(zhuǎn)載請(qǐng)保留原作者和源鏈接
                上午一個(gè)師弟在QQ上問我一道筆試題,是他前兩天去KONAMI面試時(shí)做的,這道題大致是這樣的:
                    解釋以下語句的含義:
                     1、new A;
                     2、new A();   
             
               也許很多人包括我自己,都可以馬上給出第一種情況的答案:在堆上為A類分配內(nèi)存,然后調(diào)用A的構(gòu)造函數(shù)。這種說法被大家所熟知,因?yàn)榘ā禨TL源碼剖析》等大作在內(nèi)也都是這么寫的(但是你認(rèn)為這種說法完全正確嗎?其實(shí)不盡然,答案后面揭曉)
                第二種情況,對(duì)象構(gòu)造的時(shí)候初始化列表為空會(huì)和第一種有什么不同呢?對(duì)于這種在實(shí)際工程中很少使用的情況,我一時(shí)還真給不出確切的答案。
               網(wǎng)上搜了一下,看到CSDN里面還有專門針對(duì)這個(gè)問題的一個(gè)帖子(原帖鏈接 http://bbs.csdn.net/topics/320161716)。
               好像最終也沒有可以信服的答案,認(rèn)同度比較高的是這樣的說法:“加括號(hào)調(diào)用沒有參數(shù)的構(gòu)造函數(shù),不加括號(hào)調(diào)用默認(rèn)構(gòu)造函數(shù)或唯一的構(gòu)造函數(shù),看需求” (peakflys注:這種說法是錯(cuò)誤的,答案后面揭曉)
               既然沒有特別靠譜的答案,不如自己動(dòng)手找出答案。
               構(gòu)造以下示例:
            /**
             *\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函數(shù)的匯編代碼(編譯器: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>         //調(diào)用new
              4005d6:   48 89 45 f0             mov    %rax,-0x10(%rbp)           //rax寄存器內(nèi)容賦給指針pa(rax寄存器里是new調(diào)用產(chǎn)生的A對(duì)象堆內(nèi)存地址)
                A *paa = new A();
              4005da:   bf 04 00 00 00          mov    $0x4,%edi
              4005df:   e8 e4 fe ff ff          callq  4004c8 <_Znwm@plt>         //調(diào)用new
              4005e4:   48 89 c2                mov    %rax,%rdx                      //rax的內(nèi)容放入rdx,執(zhí)行之后,rdx里存放的即是通過new A()產(chǎn)生的內(nèi)存地址
              4005e7:   c7 02 00 00 00 00       movl   $0x0,(%rdx)                 //把rdx內(nèi)存指向的內(nèi)容賦為0值,即把A::a賦值為0
              4005ed:   48 89 45 f8             mov    %rax,-0x8(%rbp)             //rax寄存器內(nèi)容賦給指針paa(rax寄存器里是new()調(diào)用產(chǎn)生的A對(duì)象堆內(nèi)存地址)
                 return 0;
              4005f1:   b8 00 00 00 00          mov    $0x0,%eax
            }
              4005f6:   c9                      leaveq 
              4005f7:   c3                      retq
                通過上面產(chǎn)生的匯編代碼(對(duì)AT&T匯編不熟悉的可以看注釋)可以很容易看出,new A()的執(zhí)行,在調(diào)用完operator new分配內(nèi)存后,馬上對(duì)新分配內(nèi)存中的對(duì)象使用0值初始化,而new A 僅僅是調(diào)用了operator new分配內(nèi)存!
               是不是這樣就可以下結(jié)論 new A()比new A多了一步,即初始化對(duì)象的步驟呢?
               我們?cè)倏纯聪旅孢@種情況:
            /**
             *\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;
            }
               這種情況是類顯示提供含默認(rèn)值的構(gòu)造函數(shù)。
               查看匯編實(shí)現(xiàn)如下:
            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 
               上面的匯編代碼就不在添加注釋了,因?yàn)閮煞N操作產(chǎn)生的匯編代碼是一樣的,都是先調(diào)用operator new分配內(nèi)存,然后調(diào)用構(gòu)造函數(shù)。
               上面的情況在VS2010下驗(yàn)證是一樣的情況,有興趣的朋友可以自己去看,這里就不再貼出VS2010下的匯編代碼了。
               通過上面的分析,對(duì)于new A和 new A() 的區(qū)別,我們可以得出下面的結(jié)論:
                  1、類體含有顯示適合地默認(rèn)構(gòu)造函數(shù)時(shí),new A和new A()的作用一致,都是首先調(diào)用operator new分配內(nèi)存,然后調(diào)用默認(rèn)構(gòu)造函數(shù)初始化對(duì)象。
                  2、類體無顯示構(gòu)造函數(shù)時(shí),new A()首先調(diào)用operator new來為對(duì)象分配內(nèi)存,然后使用空值初始化對(duì)象成員變量,而new A僅僅是調(diào)用operator new分配內(nèi)存,對(duì)象的成員變量是無意義的隨機(jī)值!  (peakflys注:對(duì)于基本數(shù)據(jù)類型,如int等 適用此條)
               注意到,現(xiàn)在很多書籍對(duì)new操作符的說明都存在紕漏,例如《STL源碼剖析》中2.2.2節(jié)中有以下的描述:

            事實(shí)證明,new Foo的操作是否有構(gòu)造函數(shù)的調(diào)用是不確定的,具體要看Foo類體里是否有顯示構(gòu)造函數(shù)的出現(xiàn)。

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

            /*****************************************華麗分割線**************************************
            補(bǔ)充:剛才發(fā)現(xiàn),在C++Primer第四版5.11節(jié)中,已經(jīng)有了對(duì)于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()的行為是一致的。
            /***************************************再次華麗分割線************************************
            鑒于上面的結(jié)論是通過GCC和VS2010得出的,而且有朋友也提出同樣的質(zhì)疑,為了確定這種結(jié)果是否是編譯器相關(guān)的,剛才特意查看了一下C++的標(biāo)準(zhǔn)化文檔。
            摘自: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);
            所以可以確定,這種情況完全是編譯器無關(guān)的(當(dāng)然那些不完全按照標(biāo)準(zhǔn)實(shí)現(xiàn)的編譯器除外)。
            但是通過上面標(biāo)準(zhǔn)化文檔的描述,我們可以看出文中對(duì)new A在無顯示構(gòu)造函數(shù)時(shí)的總結(jié)并不是特別全面,鑒于很多公司都有這道面試題(撇去這些題目的實(shí)際考察意義不說),我們有必要再補(bǔ)充一下:   對(duì)于new A: 這樣的語句,再調(diào)用完operator new分配內(nèi)存之后,如果A類體內(nèi)含有POD類型,則POD類型的成員變量處于未定義狀態(tài),如果含有非POD類型則調(diào)用該類型的默認(rèn)構(gòu)造函數(shù)。而 new A()在這些情況下都會(huì)初始化。
               PS:估計(jì)很多公司的“正確答案“ 也不一定正確吧。

            posted on 2013-04-08 13:43 peakflys 閱讀(6828) 評(píng)論(21)  編輯 收藏 引用 所屬分類: C++

            評(píng)論

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-04-08 18:09 Richard Wei

            分析的不錯(cuò),支持下.  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-04-08 18:34 zgpxgame

            分析的不錯(cuò),但就這道題目來說,感覺這題考的意義不大,面試過程用這種細(xì)枝末節(jié)的語法題考察不出面試者的思維。  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-04-09 01:29 Wilbur

            Default initialization和value initialiaztion,http://en.cppreference.com/w/cpp/language/default_initialization
            http://en.cppreference.com/w/cpp/language/value_initialization  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-04-09 09:51 zuhd

            int *pi = new int; // pi points to an uninitialized int
            int *pi = new int(); // pi points to an int value-initialized to 0
            這樣也行?
            AT&T的匯編看的好別扭啊  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-04-09 09:54 tangfu

            http://en.cppreference.com/w/cpp/language/default_initialization語言標(biāo)準(zhǔn)提到了幾點(diǎn)

            1. when an object with dynamic storage duration is created by a new-expression without an initializer
            2. If T is a class type, the default constructor is called to provide the initial value for the new object.

            感覺你這個(gè)是屬于語言灰色地帶,不同編譯器的實(shí)現(xiàn)有關(guān)系啊。。。。  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-04-09 10:17 peakflys

            其實(shí)現(xiàn)在國內(nèi)很多公司的筆試題都很學(xué)究,真正考察在工程項(xiàng)目中使用的比較多的那些語法、算法或者一些編程技巧很少、很淺……@zgpxgame
              回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-04-09 10:22 peakflys

            恩,這個(gè)網(wǎng)站提供了很好的C++參考文檔@Wilbur
              回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-04-09 10:58 peakflys

            @tangfu
            其實(shí)這個(gè)不是編譯器相關(guān)的,標(biāo)準(zhǔn)化委員會(huì)細(xì)化規(guī)則的程度大大超過你的想象。
            摘自: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);  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-04-09 11:20 溪流

            @peakflys
            學(xué)習(xí)了
              回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū)[未登錄] 2013-04-14 09:50 peakflys

            看多了就很自然了@zuhd
              回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-04-14 09:57 天馬星空

            @peakflys
            學(xué)習(xí)了,我們公司筆試題也有這一題,剛才看了一下標(biāo)準(zhǔn)答案,確實(shí)沒有像樓主分析的這么細(xì),只要寫出初始化的區(qū)別就行了。  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū)[未登錄] 2013-05-03 11:21 noname

            你這完全是因?yàn)閐ebug才有的問題, vs2012 release證明,所謂的初始化代碼不會(huì)產(chǎn)生,其它版本應(yīng)該也是一樣的  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū)[未登錄] 2013-05-03 11:24 noname

            class A{
            int a;
            };
            #pragma optimize=2

            int _tmain(int argc, _TCHAR* argv[])
            {
            auto x = new A;
            x = new A();

            return 0;
            }




            auto x = new A;
            003A1000 push 4
            003A1002 call dword ptr ds:[3A2090h]
            x = new A();
            003A1008 push 4
            003A100A call dword ptr ds:[3A2090h]
            003A1010 add esp,8
            003A1013 test eax,eax
            003A1015 je wmain+1Dh (03A101Dh)
            003A1017 mov dword ptr [eax],0

            return 0;
            003A101D xor eax,eax
            }
            003A101F ret   回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū)[未登錄] 2013-05-10 09:25 路人甲

            2012產(chǎn)生的匯編這么奇怪?不過如果上面匯編是release版本的話,不正好和樓主的觀點(diǎn)一樣嗎? 看 003A1017,明顯的0賦值操作啊。搬起石頭砸自己的腳了,哈哈 @noname
              回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-05-10 14:30 jxhgzs

            我編程的時(shí)候從來沒有使用過 new A 這種形式,都是 new A() ;但是也經(jīng)常碰到數(shù)據(jù)取隨機(jī)數(shù)值的問題,我得到的解釋是 類成員變量不會(huì)賦初始值,如果是靜態(tài)的 基本類型 會(huì)有初始值,求解!  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-05-10 17:29 syd

            由編譯器構(gòu)造出來的構(gòu)造函數(shù)是trivial的,不會(huì)對(duì)member初始為0的  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-05-10 17:52 peakflys

            @noname
            文中的結(jié)論是基于C++98標(biāo)準(zhǔn)的編譯器驗(yàn)證結(jié)果,而VS2012是根據(jù)最新的C++11標(biāo)準(zhǔn)實(shí)現(xiàn)的,新標(biāo)準(zhǔn)對(duì)于這種情況的規(guī)定有沒有變動(dòng)尚未驗(yàn)證。
            PS:你提供的匯編代碼應(yīng)該是和文中的結(jié)論吻合的吧?  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-05-10 17:54 peakflys

            @jxhgzs
            請(qǐng)?zhí)峁┠愕木幾g器版本,因?yàn)檫@是C++98標(biāo)準(zhǔn)規(guī)定的操作,如果你的編譯器產(chǎn)生的代碼行為同文中描述有出入,只能說明你的編譯器沒按照98標(biāo)準(zhǔn)來實(shí)現(xiàn)。  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-05-10 17:58 peakflys

            @syd
            其實(shí)不然,編譯器產(chǎn)生的構(gòu)造函數(shù)很多時(shí)候是nontrivial的,
            例如:成員變量是含有構(gòu)造函數(shù)的類類型,含有virtual function等等  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2013-12-12 15:18 挑燈看劍

            表達(dá)式 POD類型T non-POD類型T
            new T 不初始化 缺省初始化
            new T() 總是缺省初始化  回復(fù)  更多評(píng)論   

            # re: 從一道面試題來闡釋一個(gè)普遍的認(rèn)知誤區(qū) 2014-04-02 14:06 allen

            贊 ! 態(tài)度很認(rèn)真 !  回復(fù)  更多評(píng)論   

            <2013年4月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            導(dǎo)航

            統(tǒng)計(jì)

            公告

            人不淡定的時(shí)候,就愛表現(xiàn)出來,敲代碼如此,偶爾的靈感亦如此……

            常用鏈接

            留言簿(4)

            隨筆分類

            隨筆檔案

            文章檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            亚洲精品国产第一综合99久久| 亚洲一级Av无码毛片久久精品| 亚洲欧美日韩精品久久| 亚洲国产精品综合久久一线| 亚洲精品99久久久久中文字幕| 亚洲国产另类久久久精品小说| 一本一本久久a久久精品综合麻豆| 久久精品国产男包| 久久久久亚洲精品日久生情| 国产成年无码久久久久毛片| 久久精品亚洲日本波多野结衣| 无码专区久久综合久中文字幕 | 久久综合久久综合久久| 国内精品久久久人妻中文字幕| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 色婷婷综合久久久久中文字幕| 99热都是精品久久久久久| 成人精品一区二区久久久| 国内精品人妻无码久久久影院导航| 一本色综合网久久| 久久久久久久久久免免费精品| 国产A三级久久精品| 久久www免费人成精品香蕉| 久久99精品久久久久婷婷| AV无码久久久久不卡蜜桃| 欧美国产精品久久高清| 久久亚洲熟女cc98cm| 久久国产精品一国产精品金尊| 久久综合视频网站| 久久99久久无码毛片一区二区| 热re99久久精品国产99热| 国产精品视频久久| 色偷偷88欧美精品久久久| 999久久久免费国产精品播放| 久久综合88熟人妻| 国内精品久久久久久久coent| 色噜噜狠狠先锋影音久久| 久久精品国产亚洲沈樵| 久久久久人妻一区二区三区| 合区精品久久久中文字幕一区| 欧洲性大片xxxxx久久久|