• <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/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析

            本文的寫作目的并不在于提供C/C++程序員求職面試指導(dǎo),而旨在從技術(shù)上分析面試題的內(nèi)涵。文中的大多數(shù)面試題來自各大論壇,部分試題解答也參考了網(wǎng)友的意見。

            許多面試題看似簡單,卻需要深厚的基本功才能給出完美的解答。企業(yè)要求面試者寫一個最簡單的strcpy函數(shù)都可看出面試者在技術(shù)上究竟達到了怎樣的程 度,我們能真正寫好一個strcpy函數(shù)嗎?我們都覺得自己能,可是我們寫出的strcpy很可能只能拿到10分中的2分。讀者可從本文看到strcpy 函數(shù)從2分到10分解答的例子,看看自己屬于什么樣的層次。此外,還有一些面試題考查面試者敏捷的思維能力。

              分析這些面試題,本身包含很強的趣味性;而作為一名研發(fā)人員,通過對這些面試題的深入剖析則可進一步增強自身的內(nèi)功。

              2.找錯題

              試題1:

            void test1()
            {
             char string[10];
             char* str1 = "0123456789";
             strcpy( string, str1 );
            }
              試題2:

            void test2()
            {
             char string[10], str1[10];
             int i;
             for(i=0; i<10; i++)
             {
              str1 = 'a';
             }
             strcpy( string, str1 );
            }
              試題3:

            void test3(char* str1)
            {
             char string[10];
             if( strlen( str1 ) <= 10 )
             {
              strcpy( string, str1 );
             }
            }
              解答:

              試題1字符串str1需要11個字節(jié)才能存放下(包括末尾的’\0’),而string只有10個字節(jié)的空間,strcpy會導(dǎo)致數(shù)組越界;

            對試題2,如果面試者指出字符數(shù)組str1不能在數(shù)組內(nèi)結(jié)束可以給3分;如果面試者指出strcpy(string, str1)調(diào)用使得從str1內(nèi)存起復(fù)制到string內(nèi)存起所復(fù)制的字節(jié)數(shù)具有不確定性可以給7分,在此基礎(chǔ)上指出庫函數(shù)strcpy工作方式的給10 分;

              對試題3,if(strlen(str1) <= 10)應(yīng)改為if(strlen(str1) < 10),因為strlen的結(jié)果未統(tǒng)計’\0’所占用的1個字節(jié)。

              剖析:

              考查對基本功的掌握:

              (1)字符串以’\0’結(jié)尾;

              (2)對數(shù)組越界把握的敏感度;

              (3)庫函數(shù)strcpy的工作方式,如果編寫一個標(biāo)準(zhǔn)strcpy函數(shù)的總分值為10,下面給出幾個不同得分的答案:

              2分

            void strcpy( char *strDest, char *strSrc )
            {
              while( (*strDest++ = * strSrc++) != ‘\0’ );
            }
              4分

            void strcpy( char *strDest, const char *strSrc )
            //將源字符串加const,表明其為輸入?yún)?shù),加2分
            {
              while( (*strDest++ = * strSrc++) != ‘\0’ );
            }
              7分

            void strcpy(char *strDest, const char *strSrc)
            {
             //對源地址和目的地址加非0斷言,加3分
             assert( (strDest != NULL) && (strSrc != NULL) );
             while( (*strDest++ = * strSrc++) != ‘\0’ );
            }
              10分

            //為了實現(xiàn)鏈?zhǔn)讲僮鳎瑢⒛康牡刂贩祷兀?分!

            char * strcpy( char *strDest, const char *strSrc )
            {
             assert( (strDest != NULL) && (strSrc != NULL) );
             char *address = strDest;
             while( (*strDest++ = * strSrc++) != ‘\0’ );
              return address;
            }
              從2分到10分的幾個答案我們可以清楚的看到,小小的strcpy竟然暗藏著這么多玄機,真不是蓋的!需要多么扎實的基本功才能寫一個完美的strcpy啊!

              (4)對strlen的掌握,它沒有包括字符串末尾的'\0'。

              讀者看了不同分值的strcpy版本,應(yīng)該也可以寫出一個10分的strlen函數(shù)了,完美的版本為: int strlen( const char *str ) //輸入?yún)?shù)const

            {
             assert( strt != NULL ); //斷言字符串地址非0
             int len;
             while( (*str++) != '\0' )
             {
              len++;
             }
             return len;
            }
              試題4:

            void GetMemory( char *p )
            {
             p = (char *) malloc( 100 );
            }

            void Test( void )
            {
             char *str = NULL;
             GetMemory( str );
             strcpy( str, "hello world" );
             printf( str );
            }
              試題5:

            char *GetMemory( void )
            {
             char p[] = "hello world";
             return p;
            }

            void Test( void )
            {
             char *str = NULL;
             str = GetMemory();
             printf( str );
            }
              試題6:

            void GetMemory( char **p, int num )
            {
             *p = (char *) malloc( num );
            }

            void Test( void )
            {
             char *str = NULL;
             GetMemory( &str, 100 );
             strcpy( str, "hello" );
             printf( str );
            }
              試題7:

            void Test( void )
            {
             char *str = (char *) malloc( 100 );
             strcpy( str, "hello" );
             free( str );
             ... //省略的其它語句
            }
              解答:

              試題4傳入中GetMemory( char *p )函數(shù)的形參為字符串指針,在函數(shù)內(nèi)部修改形參并不能真正的改變傳入形參的值,執(zhí)行完

            char *str = NULL;
            GetMemory( str );
              后的str仍然為NULL;

              試題5中

            char p[] = "hello world";
            return p;
              的p[]數(shù)組為函數(shù)內(nèi)的局部自動變量,在函數(shù)返回后,內(nèi)存已經(jīng)被釋放。這是許多程序員常犯的錯誤,其根源在于不理解變量的生存期。

              試題6的GetMemory避免了試題4的問題,傳入GetMemory的參數(shù)為字符串指針的指針,但是在GetMemory中執(zhí)行申請內(nèi)存及賦值語句
            tiffany bracelets

            *p = (char *) malloc( num );
              后未判斷內(nèi)存是否申請成功,應(yīng)加上:

            if ( *p == NULL )
            {
             ...//進行申請內(nèi)存失敗處理
            }
              試題7存在與試題6同樣的問題,在執(zhí)行

            char *str = (char *) malloc(100);
              后未進行內(nèi)存是否申請成功的判斷;另外,在free(str)后未置str為空,導(dǎo)致可能變成一個“野”指針,應(yīng)加上:

            str = NULL;
              試題6的Test函數(shù)中也未對malloc的內(nèi)存進行釋放。

              剖析:

              試題4~7考查面試者對內(nèi)存操作的理解程度,基本功扎實的面試者一般都能正確的回答其中50~60的錯誤。但是要完全解答正確,卻也絕非易事。

              對內(nèi)存操作的考查主要集中在:

              (1)指針的理解;

              (2)變量的生存期及作用范圍;

              (3)良好的動態(tài)內(nèi)存申請和釋放習(xí)慣。

              再看看下面的一段程序有什么錯誤:

            swap( int* p1,int* p2 )
            {
             int *p;
             *p = *p1;
             *p1 = *p2;
             *p2 = *p;
            }
              在swap函數(shù)中,p是一個“野”指針,有可能指向系統(tǒng)區(qū),導(dǎo)致程序運行的崩潰。在VC++中DEBUG運行時提示錯誤“Access Violation”。該程序應(yīng)該改為:

            swap( int* p1,int* p2 )
            {
             int p;
             p = *p1;
             *p1 = *p2;
             *p2 = p;
            }

            posted on 2008-03-14 10:03 lovetiffany 閱讀(4847) 評論(16)  編輯 收藏 引用

            評論

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2008-03-14 11:07 winsty

            這是純c啊..
            沒有c++..  回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2008-03-14 11:28 夢在天涯

            很不錯!大家謝謝你!
              回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2008-03-14 11:37 raof01

            打著C++旗號的C  回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2008-03-14 11:40 夢在天涯

            仔細(xì)看了一邊,真好!轉(zhuǎn)了
              回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2008-03-14 12:00 魔域私服

            http://www.zhaomysf.com.cn  回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2008-03-14 23:36 酷勤網(wǎng)

            很好,這種分析性的試題很喜歡@!  回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2008-03-16 21:10 Kven

            其實我讀了半年C罷了!
            現(xiàn)在我是修讀著C++了,上頭的我還不是很明。
            其實,我還不太懂它們的分別。可是我有想到到一個問題。
            就是當(dāng)void時,C要寫return 0,而C++不用。
            會不會C++太自動化了。會是一件好事嗎?
            而且一個軟件不是要講求移植性嗎?
            C不是有比C++更好的移植性嗎?
            公司要求這樣的程式編碼,我覺得是應(yīng)該的。  回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2008-03-20 10:23

            經(jīng)典!

            以前被面試的公司蒙過,現(xiàn)在終于大徹大悟了!  回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2008-08-25 23:07 gerald

            好像都可以在高質(zhì)量C編程這本書里找到  回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2008-11-17 16:07 zdwsj

            不錯啊.有些不會的看過有收獲.  回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2009-12-28 18:43 Bg20Amy

            Do you exactly know that you could work at the <a href="http://www.topdissertations.com">dissertation writing</a> or custom writing services, because some people like to write the smart release reffering to this topic or <a href="http://www.topdissertations.com">thesis writing</a> thus, they buy a essay online.   回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2010-01-18 22:22 KIMBERLEYun32

            It's not so easy to buy a good custom essays, essentially if you are occupied. I give advice you to notice <a href=" http://www.qualityessay.com">buy custom essay papers</a> and to be void from disbelief that your work will be done by paper writing service  回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2010-02-27 16:40 BucknerAdrian

            Would you like to get <a href="http://www.prime-resume.com">resume services</a>, that suit the field of research you expect?. You can trust our resume writers, as you count on yourself. Thanks because it is the useful information  回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2010-03-31 17:58 submit article

            We added the superior data relating with this post taking the help of the article submission service and any submit article service.   回復(fù)  更多評論   

            # Article submission service 2010-07-16 20:51 ..

            I wish I had got your ability to write,this informative and interesting blog is so very useful and knowledgeable.Thanks
            <a href="http://www.article-marketer.biz/article-submission-service.html">Article submission service</a>  回復(fù)  更多評論   

            # re: C/C++ 程序設(shè)計員應(yīng)聘常見面試試題深入剖析 2010-07-23 22:18 Article submission service

            Make the most of your hard work. Just look ut for article writing and submission services that are easily available online. <a href="http://www.article-marketer.biz/article-submission-service.html">Article submission service</a>

              回復(fù)  更多評論   


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2008年1月>
            303112345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿(4)

            隨筆檔案

            網(wǎng)站收藏

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲中文字幕无码久久精品1| 综合久久国产九一剧情麻豆| 精品久久久久久99人妻| 久久久久99精品成人片直播| 日本WV一本一道久久香蕉| 久久久久亚洲AV无码专区网站| 97精品伊人久久大香线蕉app| 久久亚洲视频| 国产激情久久久久影院老熟女免费| 国产精品无码久久久久久| 中文字幕无码免费久久| 亚洲第一极品精品无码久久| 伊人久久精品无码二区麻豆| 国产精品99久久久精品无码| 中文精品久久久久人妻不卡| 亚洲第一极品精品无码久久| 久久精品国产亚洲av高清漫画| …久久精品99久久香蕉国产| 国内精品久久久久影院优| 人妻精品久久无码区| 99久久精品国产免看国产一区| 久久精品国产99国产精偷| 久久精品毛片免费观看| 久久精品国产亚洲av影院| 久久精品国产99国产精偷| 国产福利电影一区二区三区久久久久成人精品综合| 欧美久久精品一级c片片| 国产亚洲精久久久久久无码AV| 一本一道久久a久久精品综合 | 波多野结衣久久一区二区| 狠狠色丁香婷婷久久综合五月| 久久99国产综合精品免费| 97r久久精品国产99国产精| 久久狠狠一本精品综合网| 久久久亚洲欧洲日产国码是AV| 久久99国产精品99久久| 久久久久九国产精品| 久久精品无码专区免费青青| 久久久久国产一区二区| 国产一区二区三区久久| 亚洲午夜久久久久久久久久 |