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

            Xiao.Zhu C++

            Xiao.Zhu C++

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              29 隨筆 :: 14 文章 :: 17 評(píng)論 :: 0 Trackbacks
            ---Sailor_forever分析整理,sailing_9806@163.com
            1、預(yù)處理器(Preprocessor)... 1
            2、如何定義宏... 2
            3、預(yù)處理器標(biāo)識(shí)#error的目的是什么?... 4
            4、死循環(huán)(Infinite loops)... 4
            5、數(shù)據(jù)聲明(Data declarations)... 5
            6、關(guān)鍵字static的作用是什么?... 6
            7、關(guān)鍵字const有什么含意?... 7
            8、Volatile的使用... 9
            9、位操作(Bit manipulation)... 12
            10、訪問固定的內(nèi)存位置(Accessing fixed memory locations)... 13
            11、中斷(Interrupts)... 13
            12、符號(hào)擴(kuò)展的代碼例子(Code examples)... 15
            13、處理器字長導(dǎo)致的數(shù)據(jù)擴(kuò)展問題... 16
            14、動(dòng)態(tài)內(nèi)存分配(Dynamic memory allocation)... 16
            15、用Typedef構(gòu)造復(fù)合類型... 17
            16、晦澀的語法及代碼風(fēng)格... 18

            C語言測(cè)試是招聘嵌入式系統(tǒng)程序員過程中必須而且有效的方法。這些年,我既參加也組織了許多這種測(cè)試,在這過程中我意識(shí)到這些測(cè)試能為面試者和被面試者提供許多有用信息,此外,撇開面試的壓力不談,這種測(cè)試也是相當(dāng)有趣的。

            從 被面試者的角度來講,你能了解許多關(guān)于出題者或監(jiān)考者的情況。這個(gè)測(cè)試只是出題者為顯示其對(duì)ANSI標(biāo)準(zhǔn)細(xì)節(jié)的知識(shí)而不是技術(shù)技巧而設(shè)計(jì)嗎?這是個(gè)愚蠢的 問題嗎?如要你答出某個(gè)字符的ASCII值。這些問題著重考察你的系統(tǒng)調(diào)用和內(nèi)存分配策略方面的能力嗎?這標(biāo)志著出題者也許花時(shí)間在微機(jī)上而不是在嵌入式 系統(tǒng)上。如果上述任何問題的答案是"是"的話,那么我知道我得認(rèn)真考慮我是否應(yīng)該去做這份工作。

            從面試者的角度來講,一個(gè)測(cè)試也許能 從多方面揭示應(yīng)試者的素質(zhì):最基本的,你能了解應(yīng)試者C語言的水平。不管怎么樣,看一下這人如何回答他不會(huì)的問題也是滿有趣。應(yīng)試者是以好的直覺做出明智 的選擇,還是只是瞎蒙呢?當(dāng)應(yīng)試者在某個(gè)問題上卡住時(shí)是找借口呢,還是表現(xiàn)出對(duì)問題的真正的好奇心,把這看成學(xué)習(xí)的機(jī)會(huì)呢?我發(fā)現(xiàn)這些信息與他們的測(cè)試成 績一樣有用。

            有了這些想法,我決定出一些真正針對(duì)嵌入式系統(tǒng)的考題,希望這些令人頭痛的考題能給正在找工作的人一點(diǎn)幫助。這些問題都是我這些年實(shí)際碰到的。其中有些題很難,但它們應(yīng)該都能給你一點(diǎn)啟迪。

            這個(gè)測(cè)試適于不同水平的應(yīng)試者,大多數(shù)初級(jí)水平的應(yīng)試者的成績會(huì)很差,經(jīng)驗(yàn)豐富的程序員應(yīng)該有很好的成績。為了讓你能自己決定某些問題的偏好,每個(gè)問題沒有分配分?jǐn)?shù),如果選擇這些考題為你所用,請(qǐng)自行按你的意思分配分?jǐn)?shù)。



            1、預(yù)處理器(Preprocessor)
            用預(yù)處理指令#define 聲明一個(gè)常數(shù),用以表明1年中有多少秒(忽略閏年問題)

            #define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL(大小寫都行,常量后面可以加此標(biāo)志,宏的命名風(fēng)格要大寫,多個(gè)之間用下劃線)

            我在這想看到幾件事情:

            1) #define 語法的基本知識(shí)(例如:不能以分號(hào)結(jié)束,括號(hào)的使用(表達(dá)式、參數(shù)等要括起來),等等)

            2)懂得預(yù)處理器將為你計(jì)算常數(shù)表達(dá)式的值(難道不是替換么,先算再替?會(huì)將常數(shù)合并),因此,直接寫出你是如何計(jì)算一年中有多少秒而不是計(jì)算出實(shí)際的值,是更清晰而沒有代價(jià)的。

            3) 意識(shí)到這個(gè)表達(dá)式將使一個(gè)16位機(jī)的整型數(shù)溢出-因此要用到長整型符號(hào)L,告訴編譯器這個(gè)常數(shù)是的長整型數(shù)。

            4) 如果你在你的表達(dá)式中用到UL(表示無符號(hào)長整型),那么你有了一個(gè)好的起點(diǎn)。記住,第一印象很重要。

            2、如何定義宏
            寫一個(gè)"標(biāo)準(zhǔn)"宏MIN ,這個(gè)宏輸入兩個(gè)參數(shù)并返回較小的一個(gè)。

            考點(diǎn):(表達(dá)式、參數(shù)等要括起來)

            #define MIN(A,B) ((A) <= (B) ? (A) : (B))

            這個(gè)測(cè)試是為下面的目的而設(shè)的:

            1) 標(biāo)識(shí)#define在宏中應(yīng)用的基本知識(shí)。這是很重要的。因?yàn)樵谇度?inline)操作符變?yōu)闃?biāo)準(zhǔn)C的一部分之前,宏是方便產(chǎn)生嵌入代碼的唯一方法,對(duì) 于嵌入式系統(tǒng)來說,為了能達(dá)到要求的性能(當(dāng)然主要是實(shí)時(shí)性哦,犧牲代碼空間換取時(shí)間效率),嵌入代碼經(jīng)常是必須的方法。

            2)三重條件操作符的知識(shí)。這個(gè)操作符存在C語言中的原因是它使得編譯器能產(chǎn)生比if-then-else(存在條件轉(zhuǎn)移會(huì)中斷指令流水線)更優(yōu)化的代碼,了解這個(gè)用法是很重要的。

            3) 懂得在宏中小心地把參數(shù)用括號(hào)括起來

            4) 我也用這個(gè)問題開始討論宏的副作用,例如:當(dāng)你寫下面的代碼時(shí)會(huì)發(fā)生什么事?

            least = MIN(*p++, b);



            此處考點(diǎn):inline函數(shù)和宏的區(qū)別

            宏 只是將參數(shù)完全替換,即MIN(*p++, b)進(jìn)行宏展開后為((*p++) <= (b) ? (*p++) : (b)),如果(*p++) <= (b)成立,則表達(dá)式的值為(*p++),但由于在(*p++)<= (b)判斷過程中改變了p的值,使得此時(shí)的? (*p++)非(*p++)<= (b)中的值了,違背了?號(hào)表達(dá)式的原意。

            但是內(nèi)聯(lián)inline函數(shù)將進(jìn)行參數(shù)檢查,求出參數(shù)的值后再將此值帶入函數(shù)中,因此((A) <= (B) ? (A) : (B))中的A是一致的。



            第一部分:宏
            為什么要使用宏呢?
            因 為函數(shù)的調(diào)用必須要將程序執(zhí)行的順序轉(zhuǎn)移到函數(shù)所存放在內(nèi)存中的某個(gè)地址,將函數(shù)的程序內(nèi)容執(zhí)行完后,再返回到轉(zhuǎn)去執(zhí)行該函數(shù)前的地方。這種轉(zhuǎn)移操作要求 在轉(zhuǎn)去執(zhí)行前要保存現(xiàn)場(chǎng)并記憶執(zhí)行的地址,轉(zhuǎn)回后要恢復(fù)現(xiàn)場(chǎng),并按原來保存地址繼續(xù)執(zhí)行。因此,函數(shù)調(diào)用要有一定的時(shí)間和空間方面的開銷,于是將影響其效 率。
            而宏只是在預(yù)處理的地方把代碼展開,不需要額外的空間和時(shí)間方面的開銷,所以調(diào)用一個(gè)宏比調(diào)用一個(gè)函數(shù)更有效率。
            但是宏也有很多的不盡人意的地方。
            1、宏不能訪問對(duì)象的私有成員。
            2、宏的定義很容易產(chǎn)生二意性。

            3、宏定義的常量在代碼區(qū),很多調(diào)試器不能夠?qū)ζ湔{(diào)試
            我們舉個(gè)例子:
            #define square(x) (x*x)

            避免這些錯(cuò)誤的方法,一是給宏的參數(shù)都加上括號(hào)。
            #define square(x) ((x)*(x))

            第二部分:內(nèi)聯(lián)函數(shù)
            從上面的闡述,可以看到宏有一些難以避免的問題,怎么解決呢?
            內(nèi)聯(lián)函數(shù)是代碼被插入到調(diào)用者代碼處的函數(shù)。如同 #define 宏,內(nèi)聯(lián)函數(shù)通過避免被調(diào)用的開銷來提高執(zhí)行效率,尤其是它能夠通過調(diào)用(“過程化集成”)被編譯器優(yōu)化。


            內(nèi) 聯(lián)函數(shù)和宏很類似,而本質(zhì)區(qū)別在于,宏是由預(yù)處理器對(duì)宏進(jìn)行替代,而內(nèi)聯(lián)函數(shù)是通過編譯器控制來實(shí)現(xiàn)的。而且內(nèi)聯(lián)函數(shù)是真正的函數(shù),只是在需要用到的時(shí) 候,內(nèi)聯(lián)函數(shù)像宏一樣的展開,所以取消了函數(shù)的參數(shù)壓棧,減少了調(diào)用的開銷。你可以象調(diào)用函數(shù)一樣來調(diào)用內(nèi)聯(lián)函數(shù),而不必?fù)?dān)心會(huì)產(chǎn)生于處理宏的一些問題。
            聲明內(nèi)聯(lián)函數(shù)看上去和普通函數(shù)非常相似:
            void f(int i, char c);
            當(dāng)你定義一個(gè)內(nèi)聯(lián)函數(shù)時(shí),在函數(shù)定義前加上 inline 關(guān)鍵字,并且將定義放入頭文件:
            inline void f(int i, char c)
            {
            // ...
            }
            內(nèi)聯(lián)函數(shù)必須是和函數(shù)體的定義申明在一起,才有效。
            像這樣的申明inline function(int i)是沒有效果的,編譯器只是把函數(shù)作為普通的函數(shù)申明,我們必須定義函數(shù)體。
            inline int function(int i) {return i*i;}
            這樣我們才算定義了一個(gè)內(nèi)聯(lián)函數(shù)。我們可以把它作為一般的函數(shù)一樣調(diào)用。但是執(zhí)行速度確比一般函數(shù)的執(zhí)行速度要快。


            當(dāng)然,內(nèi)聯(lián)函數(shù)也有一定的局限性。就是函數(shù)中的執(zhí)行代碼不能太多了,如果,內(nèi)聯(lián)函數(shù)的函數(shù)體過大,一般的編譯器會(huì)放棄內(nèi)聯(lián)方式,而采用普通的方式調(diào)用函數(shù)。這樣,內(nèi)聯(lián)函數(shù)就和普通函數(shù)執(zhí)行效率一樣了。
            有上面的兩者的特性,我們可以用內(nèi)聯(lián)函數(shù)完全取代預(yù)處理宏。



            3、預(yù)處理器標(biāo)識(shí)#error的目的是什么?
            如果你不知道答案,請(qǐng)看參考文獻(xiàn)1。這問題對(duì)區(qū)分一個(gè)正常的伙計(jì)和一個(gè)書呆子是很有用的。只有書呆子才會(huì)讀C語言課本的附錄去找出象這種問題的答案。當(dāng)然如果你不是在找一個(gè)書呆子,那么應(yīng)試者最好希望自己不要知道答案。



            4、死循環(huán)(Infinite loops)
            嵌入式系統(tǒng)中經(jīng)常要用到無限循環(huán),你怎么樣用C編寫死循環(huán)呢? 這個(gè)問題用幾個(gè)解決方案。

            我首選的方案是:

            while(1)

            {



            }

            一些程序員更喜歡如下方案:

            for(;;) (此處的判斷效率要低的多,在匯編代碼中看看???)

            {



            }

            這 個(gè)實(shí)現(xiàn)方式讓我為難,因?yàn)檫@個(gè)語法沒有確切表達(dá)到底怎么回事。如果一個(gè)應(yīng)試者給出這個(gè)作為方案,我將用這個(gè)作為一個(gè)機(jī)會(huì)去探究他們這樣做的基本原理。如果 他們的基本答案是:"我被教著這樣做,但從沒有想到過為什么。"這會(huì)給我留下一個(gè)壞印象。 (很多時(shí)候面試官關(guān)注你思考問題的方式,是否留意某些東西善于思考,可能并沒有對(duì)錯(cuò),只是偏好而已,比如memset和memcopy以及strcpy都 能拷貝字符串,到底有什么區(qū)別呢?看你是否善于比較是否關(guān)注細(xì)節(jié))



            第三個(gè)方案是用 goto (goto語句在C中是應(yīng)該盡量避免的,只在處理錯(cuò)誤代碼時(shí)用)

            Loop:

            ...

            goto Loop;

            應(yīng)試者如給出上面的方案,這說明或者他是一個(gè)匯編語言程序員(這也許是好事)或者他是一個(gè)想進(jìn)入新領(lǐng)域的BASIC/FORTRAN程序員。



            5、數(shù)據(jù)聲明(Data declarations)
            用變量a給出下面的定義

            a) 一個(gè)整型數(shù)(An integer)

            b)一個(gè)指向整型數(shù)的指針( A pointer to an integer)

            c)一個(gè)指向指針的的指針,它指向的指針是指向一個(gè)整型數(shù)( A pointer to a pointer to an integers)

            d)一個(gè)有10個(gè)整型數(shù)的數(shù)組( An array of 10 integers)

            e) 一個(gè)有10個(gè)指針的數(shù)組,該指針是指向一個(gè)整型數(shù)的。(An array of 10 pointers to integers)

            f) 一個(gè)指向有10個(gè)整型數(shù)數(shù)組的指針( A pointer to an array of 10 integers)

            g) 一個(gè)指向函數(shù)的指針,該函數(shù)有一個(gè)整型參數(shù)并返回一個(gè)整型數(shù)(A pointer to a function that takes an integer as an argument and returns an integer)

            h) 一個(gè)有10個(gè)指針的數(shù)組,該指針指向一個(gè)函數(shù),該函數(shù)有一個(gè)整型參數(shù)并返回一個(gè)整型數(shù)( An array of ten pointers to functions that take an integer argument and return an integer )

            答案是:

            a) int a; // An integer

            b) int *a; // A pointer to an integer

            c) int **a; // A pointer to a pointer to an integer

            d) int a[10]; // An array of 10 integers

            e) int *a[10]; // An array of 10 pointers to integers

            f) int (*a)[10]; // A pointer to an array of 10 integers

            g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer // 不是(int x),不需要具體的參數(shù)

            h) int (*a[10])(int)(可以從e、g類比得到); // An array of 10 pointers to functions that take an integer argument and return an integer

            人 們經(jīng)常聲稱這里有幾個(gè)問題是那種要翻一下書才能回答的問題,我同意這種說法。當(dāng)我寫這篇文章時(shí),為了確定語法的正確性,我的確查了一下書。但是當(dāng)我被面試 的時(shí)候,我期望被問到這個(gè)問題(或者相近的問題)。因?yàn)樵诒幻嬖嚨倪@段時(shí)間里,我確定我知道這個(gè)問題的答案。應(yīng)試者如果不知道所有的答案(或至少大部分答 案),那么也就沒有為這次面試做準(zhǔn)備,如果該面試者沒有為這次面試做準(zhǔn)備,那么他又能為什么做準(zhǔn)備呢?



            6、關(guān)鍵字static的作用是什么?
            這個(gè)簡單的問題很少有人能回答完全。在C語言中,關(guān)鍵字static有三個(gè)明顯的作用:

            1)在函數(shù)體內(nèi),一個(gè)被聲明為靜態(tài)的變量在這一函數(shù)被調(diào)用過程中維持其值不變(該變量存放在靜態(tài)變量區(qū))。

            2) 在模塊內(nèi)(但在函數(shù)體外),一個(gè)被聲明為靜態(tài)的變量可以被模塊內(nèi)所用函數(shù)訪問,但不能被模塊外其它函數(shù)訪問。它是一個(gè)本地的全局變量。

            3) 在模塊內(nèi),一個(gè)被聲明為靜態(tài)的函數(shù)只可被這一模塊內(nèi)的其它函數(shù)調(diào)用。那就是,這個(gè)函數(shù)被限制在聲明它的模塊的本地范圍內(nèi)使用。

            大多數(shù)應(yīng)試者能正確回答第一部分,一部分能正確回答第二部分,但是很少的人能懂得第三部分。這是一個(gè)應(yīng)試者的嚴(yán)重的缺點(diǎn),因?yàn)樗@然不懂得本地化數(shù)據(jù)和代碼范圍的好處和重要性。



            考點(diǎn):在嵌入式系統(tǒng)中,要時(shí)刻懂得移植的重要性,程序可能是很多程序員共同協(xié)作同時(shí)完成,在定義變量及函數(shù)的過程,可能會(huì)重名,這給系統(tǒng)的集成帶來麻煩,因此保證不沖突的辦法是顯示的表示此變量或者函數(shù)是本地的,static即可。

            在Linux的模塊編程中,這一條很明顯,所有的函數(shù)和全局變量都要用static關(guān)鍵字聲明,將其作用域限制在本模塊內(nèi)部,與其他模塊共享的函數(shù)或者變量要EXPORT到內(nèi)核中。



            static關(guān)鍵字至少有下列n個(gè)作用:
            (1)設(shè)置變量的存儲(chǔ)域,函數(shù)體內(nèi)static變量的作用范圍為該函數(shù)體,不同于auto變量,該變量的內(nèi)存只被分配一次,因此其值在下次調(diào)用時(shí)仍維持上次的值;

            (2)限制變量的作用域,在模塊內(nèi)的static全局變量可以被模塊內(nèi)所用函數(shù)訪問,但不能被模塊外其它函數(shù)訪問;

            (3)限制函數(shù)的作用域,在模塊內(nèi)的static函數(shù)只可被這一模塊內(nèi)的其它函數(shù)調(diào)用,這個(gè)函數(shù)的使用范圍被限制在聲明它的模塊內(nèi);
            (4)在類中的static成員變量意味著它為該類的所有實(shí)例所共享,也就是說當(dāng)某個(gè)類的實(shí)例修改了該靜態(tài)成員變量,其修改值為該類的其它所有實(shí)例所見;
            (5)在類中的static成員函數(shù)屬于整個(gè)類所擁有,這個(gè)函數(shù)不接收this指針,因而只能訪問類的static成員變量。



            7、關(guān)鍵字const有什么含意?
            我 只要一聽到被面試者說:"const意味著常數(shù)"(不是常數(shù),可以是變量,只是你不能修改它),我就知道我正在和一個(gè)業(yè)余者打交道。去年Dan Saks已經(jīng)在他的文章里完全概括了const的所有用法,因此ESP(譯者:Embedded Systems Programming)的每一位讀者應(yīng)該非常熟悉const能做什么和不能做什么.如果你從沒有讀到那篇文章,只要能說出const意味著"只讀"就可 以了。盡管這個(gè)答案不是完全的答案,但我接受它作為一個(gè)正確的答案。(如果你想知道更詳細(xì)的答案,仔細(xì)讀一下Saks的文章吧。)

            如果應(yīng)試者能正確回答這個(gè)問題,我將問他一個(gè)附加的問題:下面的聲明都是什么意思?

            Const只是一個(gè)修飾符,不管怎么樣a仍然是一個(gè)int型的變量

            const int a;

            int const a;

            const int *a;

            int * const a;

            int const * a const;

            本質(zhì):const在誰后面誰就不可修改,const在最前面則將其后移一位即可,二者等效



            前兩個(gè)的作用是一樣,a是一個(gè)常整型數(shù)。第三個(gè)意味著a是一個(gè)指向常整型數(shù)的指針(也就是,指向的整型數(shù)是不可修改的,但指針可以,此最常見于函數(shù)的參數(shù),當(dāng)你只引用傳進(jìn)來指針?biāo)赶虻闹禃r(shí)應(yīng)該加上const修飾符,程序中修改編譯就不通過,可以減少程序的bug)。



            第四個(gè)意思a是一個(gè)指向整型數(shù)的常指針(也就是說,指針指向的整型數(shù)是可以修改的,但指針是不可修改的)。最后一個(gè)意味著a是一個(gè)指向常整型數(shù)的常指針(也就是說,指針指向的整型數(shù)是不可修改的,同時(shí)指針也是不可修改的)。



            如果應(yīng)試者能正確回答這些問題,那么他就給我留下了一個(gè)好印象。順帶提一句,也許你可能會(huì)問,即使不用關(guān)鍵字 ,也還是能很容易寫出功能正確的程序,那么我為什么還要如此看重關(guān)鍵字const呢?我也如下的幾下理由:

            1) 關(guān)鍵字const的作用是為給讀你代碼的人傳達(dá)非常有用的信息,實(shí)際上,聲明一個(gè)參數(shù)為常量是為了告訴了用戶這個(gè)參數(shù)的應(yīng)用目的。如果你曾花很多時(shí)間清理 其它人留下的垃圾,你就會(huì)很快學(xué)會(huì)感謝這點(diǎn)多余的信息。(當(dāng)然,懂得用const的程序員很少會(huì)留下的垃圾讓別人來清理的。)

            2) 通過給優(yōu)化器一些附加的信息,使用關(guān)鍵字const也許能產(chǎn)生更緊湊的代碼。

            3) 合理地使用關(guān)鍵字const可以使編譯器很自然地保護(hù)那些不希望被改變的參數(shù),防止其被無意的代碼修改。簡而言之,這樣可以減少bug的出現(xiàn)。



            const關(guān)鍵字至少有下列n個(gè)作用:

            (1)欲阻止一個(gè)變量被改變,可以使用const關(guān)鍵字。在定義該const變量時(shí),通常需要對(duì)它進(jìn)行初始化,因?yàn)橐院缶蜎]有機(jī)會(huì)再去改變它了;
            (2)對(duì)指針來說,可以指定指針本身為const,也可以指定指針?biāo)傅臄?shù)據(jù)為const,或二者同時(shí)指定為const;
            (3)在一個(gè)函數(shù)聲明中,const可以修飾形參,表明它是一個(gè)輸入?yún)?shù),在函數(shù)內(nèi)部不能改變其值;
            (4)對(duì)于類的成員函數(shù),若指定其為const類型,則表明其是一個(gè)常函數(shù),不能修改類的成員變量;
            (5)對(duì)于類的成員函數(shù),有時(shí)候必須指定其返回值為const類型,以使得其返回值不為“左值”。例如:
            const classA operator*(const classA& a1,const classA& a2);
              operator*的返回結(jié)果必須是一個(gè)const對(duì)象。如果不是,這樣的變態(tài)代碼也不會(huì)編譯出錯(cuò):
            classA a, b, c;
            (a * b) = c; // 對(duì)a*b的結(jié)果賦值
              操作(a * b) = c顯然不符合編程者的初衷,也沒有任何意義。



            8、Volatile的使用
            關(guān)鍵字volatile有什么含意?并給出三個(gè)不同的例子。

            一 個(gè)定義為volatile的變量是說這變量可能會(huì)被意想不到地改變,這樣,編譯器就不會(huì)去假設(shè)這個(gè)變量的值了。精確地說就是,優(yōu)化器在用到這個(gè)變量時(shí)必須 每次都小心地重新讀取這個(gè)變量的值,而不是使用保存在寄存器里的備份(由于訪問寄存器的速度要快過RAM,所以編譯器一般都會(huì)作減少存取外部RAM的優(yōu) 化)。下面是volatile變量的幾個(gè)例子:

            1) 并行設(shè)備的硬件寄存器(如:狀態(tài)寄存器,通常在頭文件中將硬件寄存器地址define為某個(gè)意義明確的表達(dá)式)

            2) 一個(gè)中斷服務(wù)子程序中會(huì)訪問到的非自動(dòng)變量(Non-automatic variables,即static變量) ;在中斷服務(wù)程序中修改的供其他程序檢測(cè)用的變量需要加volatile聲明;否則編譯器可能對(duì)變量更新一次后每次都使用緩存值不再立即更新;

            3) 多線程應(yīng)用中被幾個(gè)任務(wù)共享的變量(可能被多個(gè)線程隨時(shí)修改)



            回 答不出這個(gè)問題的人是不會(huì)被雇傭的。我認(rèn)為這是區(qū)分C程序員和嵌入式系統(tǒng)程序員的最基本的問題。搞嵌入式的家伙們經(jīng)常同硬件、中斷、RTOS等等打交道, 所有這些都要求用到volatile變量。不懂得volatile的內(nèi)容將會(huì)帶來災(zāi)難。假設(shè)被面試者正確地回答了這是問題(嗯,懷疑是否會(huì)是這樣),我將 稍微深究一下,看一下這家伙是不是直正懂得volatile完全的重要性。

            1)一個(gè)參數(shù)既可以是const還可以是volatile嗎?解釋為什么。

            2); 一個(gè)指針可以是volatile 嗎?解釋為什么。

            3); 下面的函數(shù)有什么錯(cuò)誤:

            int square(volatile int *ptr)

            {

            return *ptr * *ptr;

            }

            下面是答案:

            1)是的。一個(gè)例子是只讀的狀態(tài)寄存器。它是volatile因?yàn)樗赡鼙灰庀氩坏降馗淖儭K莄onst因?yàn)槌绦虿粦?yīng)該試圖去修改它。

            2); 是的。盡管這并不很常見。一個(gè)例子是當(dāng)一個(gè)中斷服務(wù)子程序修改一個(gè)指向一個(gè)buffer的指針時(shí)。

            3) 這段代碼有點(diǎn)變態(tài)。這段代碼的目的是用來返回指針*ptr指向值的平方,但是,由于*ptr指向一個(gè)volatile型參數(shù),編譯器將產(chǎn)生類似下面的代碼:

            int square(volatile int *ptr)

            {

            int a,b;

            a = *ptr;

            b = *ptr;

            return a * b;

            }

            由于*ptr的值可能被意想不到地該變,因此a和b可能是不同的。結(jié)果,這段代碼可能返不是你所期望的平方值!正確的代碼如下:

            long square(volatile int *ptr)

            {

            int a;

            a = *ptr;

            return a * a;

            }



            關(guān)于volatile關(guān)鍵字在中斷函數(shù)中的影響實(shí)例



            串口發(fā)送數(shù)據(jù),中斷中對(duì)其檢測(cè),當(dāng)中斷產(chǎn)生后,置接收標(biāo)志,主循環(huán)中檢測(cè)此主標(biāo)志,未用valotile修飾時(shí),編譯結(jié)果如下:



            [0xe59f41bc] ldr r4,0x30203378 ; = #0x302096f0

            0x302031b8 [0xe5d40000] ldrb r0,[r4,#0]

            while(!uart1_rxFlag); //uart1_rxFlag為全局變量,在串口接收中斷中置1

            0x302031bc [0xe3500000] cmp r0,#0

            0x302031c0 [0x0afffffd] beq 0x302031bc; (Can_Int_Test + 0x17c)

            即 編譯器對(duì)其進(jìn)行了優(yōu)化,讀取一次uart1_rxFlag的值之后,將其存放在寄存器r0中,比較后,條件不滿足,繼續(xù)等待,但未重新取存儲(chǔ)器中 uart1_rxFlag的值,此時(shí)即使中斷服務(wù)函數(shù)中修改了uart1_rxFlag的值,比較處仍然不能發(fā)現(xiàn),就出現(xiàn)了無論如何程序就停在此處的問 題。



            // 加了volatile關(guān)鍵字后,編譯的結(jié)果

            302031b4 ldr r4,0x30203378 ; = #0x302096f0

            while(uart1_rxFlag == 0);

            302031b8 [0xe5d40000] ldrb r0,[r4,#0]

            302031bc [0xe3500000] cmp r0,#0

            302031c0 [0x0afffffc] beq 0x302031b8 ; (Can_Int_Test + 0x288)

            添加了關(guān)鍵字后,比較不等,跳轉(zhuǎn)到重新取存儲(chǔ)器中的uart1_rxFlag,因此任何時(shí)候uart1_rxFlag的值都是最新的。

            一定程度的優(yōu)化,去掉了讀取uart1_rxFlag地址的語句。



            定 義一個(gè)易失性變量,編譯器有一種技術(shù)叫數(shù)據(jù)流分析,分析程序中的變量在哪里被賦值、在哪里使用、在哪里失效,分析結(jié)果可以用于常量合并,常量傳播等優(yōu)化。 當(dāng)編譯器檢查到代碼沒有修改字段的值,就有可能在你訪問字段時(shí)提供上次訪問的緩存值,這能夠提高程序的效率,但有時(shí)這些優(yōu)化會(huì)帶來問題,不是我們程序所需 要的,特點(diǎn)是對(duì)硬件寄存器操作的程序,這時(shí)可以用volatile關(guān)鍵字禁止做這些優(yōu)化。



            多任務(wù)環(huán)境下各任務(wù)間共享的標(biāo)志應(yīng)該加voatile關(guān)鍵字:在多線程訪問某字段時(shí),代碼希望這些訪問能夠操作(讀取)到字段的最新值,同時(shí)寫到變量的操作能立即更新;對(duì)字段加上volatile關(guān)鍵字,那么對(duì)該字段的任何請(qǐng)求(讀/寫)都會(huì)立刻得到執(zhí)行。



            9、位操作(Bit manipulation)
            嵌入式系統(tǒng)總是要用戶對(duì)變量或寄存器進(jìn)行位操作。給定一個(gè)整型變量a,寫兩段代碼,第一個(gè)設(shè)置a的bit 3,第二個(gè)清除a 的bit 3。在以上兩個(gè)操作中,要保持其它位不變。 對(duì)這個(gè)問題有三種基本的反應(yīng)

            1)不知道如何下手。該被面者從沒做過任何嵌入式系統(tǒng)的工作。

            2) 用bit fields。Bit fields是被扔到C語言死角的東西,它保證你的代碼在不同編譯器之間是不可移植的,同時(shí)也保證了的你的代碼是不可重用的。

            3) 用 #defines 和 bit masks 操作。這是一個(gè)有極高可移植性的方法,是應(yīng)該被用到的方法。最佳的解決方案如下:

            #define BIT3 (0x1 << 3) (采用宏將數(shù)字定義為有意義的BIT3,明確,不易出錯(cuò),改起來方便)

            static int a;

            void set_bit3(void)

            {

            a |= BIT3;

            }

            void clear_bit3(void)

            {

            a &= ~BIT3;

            }



            一 些人喜歡為設(shè)置和清除值而定義一個(gè)掩碼(待操作位全1,其余位全0的數(shù),對(duì)于某個(gè)意義靠多位同時(shí)表示的最好帶上掩碼,隔離其他位的影響)同時(shí)定義一些說明 常數(shù),這也是可以接受的。我希望看到幾個(gè)要點(diǎn):說明常數(shù)、|=和&=~操作,先取反再&是對(duì)某位清0的最好操作。



            考點(diǎn):

            在嵌入式系統(tǒng)中,時(shí)刻要關(guān)注移植性,具體的程序中不要出現(xiàn)具體的數(shù)字,這些數(shù)字都應(yīng)該define成某個(gè)有意義的符號(hào),可讀性可移植性都很強(qiáng),比如

            #define BIT(x) (0x1 << (x))

            X作為參數(shù)可以很方便的對(duì)任意位進(jìn)行操作,意義明確,更改替換方便



            10、訪問固定的內(nèi)存位置(Accessing fixed memory locations)
            嵌入式系統(tǒng)經(jīng)常具有要求程序員去訪問某特定的內(nèi)存位置的特點(diǎn)。

            在 某工程中,要求設(shè)置一絕對(duì)地址為0x67a9的整型變量的值為0xaa66。編譯器是一個(gè)純粹的ANSI編譯器。寫代碼去完成這一任務(wù)。這一問題測(cè)試你是 否知道為了訪問一絕對(duì)地址把一個(gè)整型數(shù)強(qiáng)制轉(zhuǎn)換(typecast)為一指針是合法的。這一問題的實(shí)現(xiàn)方式隨著個(gè)人風(fēng)格不同而不同。典型的類似代碼如下:

            int *ptr;

            ptr = (int *)0x67a9;

            *ptr = 0xaa55;

            A more obscure approach is: ( 一個(gè)較晦澀的方法是):

            *(int * const)(0x67a9) = 0xaa55;

            即使你的品味更接近第二種方案,但我建議你在面試時(shí)使用第一種方案。



            在嵌入式系統(tǒng)中,對(duì)于大量此類型數(shù)據(jù)如硬件寄存器應(yīng)該采用如下方式

            typedef volatile unsigned int HARD_REG;

            #define REG_NAME (*(HARD_REG *)ADDR)

            即將ADDR強(qiáng)制轉(zhuǎn)換為一個(gè)指向HARD_REG類型數(shù)據(jù)的指針,*HARD_REG為volatile的無符號(hào)整型數(shù)



            11、中斷(Interrupts)
            中 斷是嵌入式系統(tǒng)中重要的組成部分,這導(dǎo)致了很多編譯開發(fā)商提供一種擴(kuò)展-讓標(biāo)準(zhǔn)C支持中斷。其代表事實(shí)是,產(chǎn)生了一個(gè)新的關(guān)鍵字 __interrupt(51即如此)。下面的代碼就使用了__interrupt關(guān)鍵字去定義了一個(gè)中斷服務(wù)子程序(ISR),請(qǐng)?jiān)u論一下這段代碼的。

            __interrupt double compute_area (double radius)

            {

            double area = PI * radius * radius;

            printf("\nArea = %f", area);

            return area;

            }

            這個(gè)函數(shù)有太多的錯(cuò)誤了,以至讓人不知從何說起了(前提是非操作系統(tǒng)下的中斷服務(wù)函數(shù)):

            1)ISR 不能返回一個(gè)值(都應(yīng)該為void類型)。如果你不懂這個(gè),那么你不會(huì)被雇用的。

            2)ISR 不能傳遞參數(shù)。如果你沒有看到這一點(diǎn),你被雇用的機(jī)會(huì)等同第一項(xiàng)。

            3)在許多的處理器/編譯器中,浮點(diǎn)一般都是不可重入的。有些處理器/編譯器需要讓額外的寄存器入棧,有些處理器/編譯器就是不允許在ISR中做浮點(diǎn)運(yùn)算。此外,ISR應(yīng)該是短而有效率的,在ISR中做浮點(diǎn)運(yùn)算是不明智的。

            ///////////////////////////////

            另外中斷服務(wù)程序是運(yùn)行在內(nèi)核態(tài)的(linux),內(nèi)核通常是不支持浮點(diǎn)運(yùn)算的。

            http://access911.net/n/doc1.asp?mode=a&aid=4750647

            內(nèi)核中的printk和標(biāo)準(zhǔn)庫的printf不一樣,前者因?yàn)橛蓛?nèi)核直接實(shí)現(xiàn),不能支持浮點(diǎn)。

            在<linux內(nèi)核設(shè)計(jì)與實(shí)現(xiàn)>的第一章中內(nèi)核開發(fā)的特點(diǎn)一小節(jié)里就有比較了內(nèi)核開發(fā)與應(yīng)用開發(fā)的差異。其中一點(diǎn)就是內(nèi)核編程時(shí)浮點(diǎn)數(shù)的問題,書中有一句話是:內(nèi)核編程時(shí)浮點(diǎn)數(shù)很難使用

            因?yàn)闆]有浮點(diǎn)單元,內(nèi)核要支持浮點(diǎn)必須把內(nèi)核以soft-float 方式重新編譯,其連接所有的庫也都要用soft-float 方式編譯.

            否則另外一種方式使用整數(shù)定義浮點(diǎn)類型加浮點(diǎn)預(yù)算庫完成你的工作,



            http://topic.csdn.net/u/20070417/16/a4b56569-228c-4b70-b5ab-30ee61c99a3d.html

            如果你的內(nèi)核里編譯進(jìn)了浮點(diǎn)支持,那么是可以的。要不內(nèi)核或是模塊不能用float或是double內(nèi)型的變量或函數(shù)



            在配置內(nèi)核的時(shí)候把浮點(diǎn)模擬器選上,應(yīng)該是可以支持的,但是速度非常慢。
            我曾經(jīng)遇到過,硬件明明支持浮點(diǎn)運(yùn)算的FPU,但是編譯內(nèi)核的時(shí)候選上了浮點(diǎn)模擬器,結(jié)果所有的應(yīng)用程序的浮點(diǎn)運(yùn)算速度都非常慢。所以我懷疑要支持浮點(diǎn)只要編譯內(nèi)核的時(shí)候選上,對(duì)于應(yīng)用程序不需要怎么關(guān)心。

            ///////////////////////////////



            4) 與第三點(diǎn)一脈相承,printf()經(jīng)常有重入和性能上的問題。如果你丟掉了第三和第四點(diǎn),我不會(huì)太為難你的。不用說,如果你能得到后兩點(diǎn),那么你的被雇用前景越來越光明了。



            12、符號(hào)擴(kuò)展的代碼例子(Code examples)
            下面的代碼輸出是什么,為什么?

            void foo(void)

            {

            unsigned int a = 6;

            int b = -20;

            (a+b > 6) ? puts("> 6") : puts("<= 6");

            }

            Vc6.0測(cè)試情況

            void main(void)

            {

            unsigned int a = 6;

            int b = -20;

            printf("unsigned int a + int b = %x\n", (a + b));



            }

            /*unsigned int a + int b = fffffff2*/

            這 個(gè)問題測(cè)試你是否懂得C語言中的整數(shù)自動(dòng)轉(zhuǎn)換原則,我發(fā)現(xiàn)有些開發(fā)者懂得極少這些東西。不管如何,這無符號(hào)整型問題的答案是輸出是 ">6"。原因是當(dāng)表達(dá)式中存在有符號(hào)類型和無符號(hào)類型時(shí)所有的操作數(shù)都自動(dòng)轉(zhuǎn)換為無符號(hào)類型。因此-20變成了一個(gè)非常大的正整數(shù),所以該表達(dá)式 計(jì)算出的結(jié)果大于6。這一點(diǎn)對(duì)于頻繁用到無符號(hào)數(shù)據(jù)類型的嵌入式系統(tǒng)(硬件寄存器的值全部是無符號(hào)的)來說是豐常重要的。如果你答錯(cuò)了這個(gè)問題,你也就到 了得不到這份工作的邊緣。



            13、處理器字長導(dǎo)致的數(shù)據(jù)擴(kuò)展問題
            評(píng)價(jià)下面的代碼片斷:

            unsigned int zero = 0;

            unsigned int compzero = 0xFFFF;

            /*1's complement of zero */ 0的補(bǔ)碼為全1的數(shù)

            對(duì)于一個(gè)int型不是16位的處理器為說,上面的代碼是不正確的。應(yīng)編寫如下:

            unsigned int compzero = ~0;

            這一問題真正能揭露出應(yīng)試者是否懂得處理器字長的重要性(嵌入式平臺(tái)可能是8、16、32的,移植的角度來說寫出固定的0xFFFF是不對(duì)的)。在我的經(jīng)驗(yàn)里,好的嵌入式程序員非常準(zhǔn)確地明白硬件的細(xì)節(jié)和它的局限,然而PC機(jī)程序往往把硬件作為一個(gè)無法避免的煩惱。



            到 了這個(gè)階段,應(yīng)試者或者完全垂頭喪氣了或者信心滿滿志在必得。如果顯然應(yīng)試者不是很好,那么這個(gè)測(cè)試就在這里結(jié)束了。但如果顯然應(yīng)試者做得不錯(cuò),那么我就 扔出下面的追加問題,這些問題是比較難的,我想僅僅非常優(yōu)秀的應(yīng)試者能做得不錯(cuò)。提出這些問題,我希望更多看到應(yīng)試者應(yīng)付問題的方法(很重要哦,面試者關(guān) 注的是你思考問題解決問題的過程,當(dāng)你不知道答案時(shí)千萬千萬不要猜一個(gè)答案給他,因?yàn)楝F(xiàn)在不是選擇題,面試官要的是過程,你只需要將你考慮問題的過程說明 白就OK了),而不是答案。不管如何,你就當(dāng)是這個(gè)娛樂吧...



            14、動(dòng)態(tài)內(nèi)存分配(Dynamic memory allocation)
            盡 管不像非嵌入式計(jì)算機(jī)那么常見,嵌入式系統(tǒng)還是有從堆(heap)中動(dòng)態(tài)分配內(nèi)存的過程的。那么嵌入式系統(tǒng)中,動(dòng)態(tài)分配內(nèi)存可能發(fā)生的問題是什么?這里, 我期望應(yīng)試者能提到內(nèi)存碎片,碎片收集的問題,變量的持行時(shí)間等等。這個(gè)主題已經(jīng)在ESP雜志中被廣泛地討論過了(主要是 P.J. Plauger, 他的解釋遠(yuǎn)遠(yuǎn)超過我這里能提到的任何解釋),所有回過頭看一下這些雜志吧!讓應(yīng)試者進(jìn)入一種虛假的安全感覺后,我拿出這么一個(gè)小節(jié)目:下面的代碼片段的輸 出是什么,為什么?

            char *ptr;

            if ((ptr = (char *)malloc(0)) == NULL)

            puts("Got a null pointer");

            else

            puts("Got a valid pointer");

            這 是一個(gè)有趣的問題。最近在我的一個(gè)同事不經(jīng)意把0值傳給了函數(shù)malloc,得到了一個(gè)合法的指針之后,我才想到這個(gè)問題。這就是上面的代碼,該代碼的輸 出是"Got a valid pointer"。我用這個(gè)來開始討論這樣的一問題,看看被面試者是否想到庫例程這樣做是正確(因?yàn)槿绻暾?qǐng)失敗,則程序處理認(rèn)為內(nèi)存不足了,一般會(huì)終止 程序,是很嚴(yán)重的問題?)。得到正確的答案固然重要,但解決問題的方法和你做決定的基本原理更重要些。

            返回一個(gè)控指針還是指向 0 字節(jié)的指針甚至指向一個(gè)可以操作的指針?

            (取決于系統(tǒng)平臺(tái)的實(shí)現(xiàn),C99及其他標(biāo)準(zhǔn)規(guī)定可以不同的)

            malloc(0) in glibc returns a valid pointer to something(!?!?) while in uClibc calling malloc(0) returns a NULL. The behavior of malloc(0) is listed as implementation-defined by SuSv3, so both libraries are equally correct. This difference also applies to realloc(NULL, 0). I personally feel glibc's behavior is not particularly safe. To enable glibc behavior, one has to explicitly enable the MALLOC_GLIBC_COMPAT option.



            15、用Typedef構(gòu)造復(fù)合類型
            在C語言中頻繁用以聲明一個(gè)已經(jīng)存在的數(shù)據(jù)類型的同義字。也可以用預(yù)處理器做類似的事。例如,思考一下下面的例子:

            #define dPS struct s *

            typedef struct s * tPS;

            以上兩種情況的意圖都是要定義dPS 和 tPS 作為一個(gè)指向結(jié)構(gòu)s指針。哪種方法更好呢?(如果有的話)為什么?

            這是一個(gè)非常微妙的問題,任何人答對(duì)這個(gè)問題(正當(dāng)?shù)脑蚺叮皇遣拢绻銢]有原因,說不會(huì)比猜一個(gè)答案要好的多,記住啊,說話是要講根據(jù)的)是應(yīng)當(dāng)被恭喜的。答案是:typedef更好。思考下面的例子:

            dPS p1,p2;

            tPS p3,p4;

            第一個(gè)擴(kuò)展為

            struct s * p1, p2;

            上面的代碼定義p1為一個(gè)指向結(jié)構(gòu)的指,p2為一個(gè)實(shí)際的結(jié)構(gòu),這也許不是你想要的。第二個(gè)例子正確地定義了p3 和p4 兩個(gè)指針。



            16、晦澀的語法及代碼風(fēng)格
            C語言同意一些令人震驚的結(jié)構(gòu),下面的結(jié)構(gòu)是合法的嗎,如果是它做些什么?

            int a = 5, b = 7, c;

            c = a+++b;

            這個(gè)問題將做為這個(gè)測(cè)驗(yàn)的一個(gè)愉快的結(jié)尾。不管你相不相信,上面的例子是完全合乎語法的。問題是編譯器如何處理它?水平不高的編譯作者實(shí)際上會(huì)爭(zhēng)論這個(gè)問題,編譯器應(yīng)盡可能多的從左至右將若干個(gè)字符組成一個(gè)運(yùn)算符。因此,上面的代碼被處理成:c = a++ + b;

            逗號(hào)表達(dá)式依次對(duì)每個(gè)表達(dá)式計(jì)算,最后的結(jié)果為最后一個(gè)表達(dá)式的值

            因此, 這段代碼執(zhí)行后a = 6, b = 7, c = 12。

            如果你知道答案,或猜出正確答案,做得好。如果你不知道答案,我也不把這個(gè)當(dāng)作問題。我發(fā)現(xiàn)這個(gè)問題的最大好處是這是一個(gè)關(guān)于代碼編寫風(fēng)格(要明確的加上括號(hào),避免歧義或者編譯器不同帶來的差異),代碼的可讀性,代碼的可修改性的好的話題。



            注:引出代碼風(fēng)格的問題正是作者問此問題的目的,這告訴我們要揣摩面試管每個(gè)問題背后隱藏的考查點(diǎn),能夠趁機(jī)發(fā)揮下就大功告成了!



            好了,伙計(jì)們,你現(xiàn)在已經(jīng)做完所有的測(cè)試了。這就是我出的C語言測(cè)試題,我懷著愉快的心情寫完它,希望你以同樣的心情讀完它。如果是認(rèn)為這是一個(gè)好的測(cè)試,那么盡量都用到你的找工作的過程中去吧。天知道也許過個(gè)一兩年,我就不做現(xiàn)在的工作,也需要找一個(gè)。



            以下為上述16個(gè)問題的英文表述,熟悉下相關(guān)的專業(yè)詞匯對(duì)于英文面試的簡單表述很重要。

            http://www.yuanma.org/data/2007/0509/article_2585.htm

            An obligatory and significant part of the recruitment process for embedded systems programmers seems to be the "C test." Over the years, I have had to both take and prepare such tests and, in doing so, have realized that these tests can be informative for both the interviewer and interviewee. Furthermore, when given outside the pressure of an interview situation, these tests can also be quite entertaining.

            From the interviewee's perspective, you can learn a lot about the person who has written or administered the test. Is the test designed to show off the writer's knowledge of the minutiae of the ANSI standard rather than to test practical know-how? Does it test ludicrous knowledge, such as the ASCII values of certain characters? Are the questions heavily slanted towards your knowledge of system calls and memory allocation strategies, indicating that the writer may spend his time programming computers instead of embedded systems? If any of these are true, then I know I would seriously doubt whether I want the job in question.

            From the interviewer's perspective, a test can reveal several things about the candidate. Primarily, you can determine the level of the candidate's knowledge of C. However, it's also interesting to see how the person responds to questions to which they don't know the answers. Do they make intelligent choices backed up with good intuition, or do they just guess? Are they defensive when they are stumped, or do they exhibit a real curiosity about the problem and see it as an opportunity to learn something? I find this information as useful as their raw performance on the test.

            With these ideas in mind, I have attempted to construct a test that is heavily slanted towards the requirements of embedded systems. This is a lousy test to give to someone seeking a job writing compilers! The questions are almost all drawn from situations I have encountered over the years. Some of them are tough; however, they should all be informative.

            This test may be given to a wide range of candidates. Most entry-level applicants will do poorly on this test, while seasoned veterans should do very well. Points are not assigned to each question, as this tends to arbitrarily weight certain questions. However, if you choose to adapt this test for your own uses, feel free to assign scores.



            Preprocessor
            1. Using the #define statement, how would you declare a manifest constant that returns the number of seconds in a year? Disregard leap years in your answer.

            #define SECONDS_PER_YEAR

            (60 * 60 * 24 * 365)UL



            I'm looking for several things here:

            Basic knowledge of the #define syntax (for example, no semi-colon at the end, the need to parenthesize, and so on)

            An understanding that the pre-processor will evaluate constant expressions for you. Thus, it is clearer, and penalty-free, to spell out how you are calculating the number of seconds in a year, rather than actually doing the calculation yourself

            A realization that the expression will overflow an integer argument on a 16-bit machine-hence the need for the L, telling the compiler to treat the variable as a Long

            As a bonus, if you modified the expression with a UL (indicating unsigned long), then you are off to a great start. And remember, first impressions count!



            2. Write the "standard" MIN macro-that is, a macro that takes two arguments and returns the smaller of the two arguments.

            #define MIN(A,B)

            ((A)

            <

            = (B) ? (A) : (B))



            The purpose of this question is to test the following:

            Basic knowledge of the #define directive as used in macros. This is important because until the inline operator becomes part of standard C, macros are the only portable way of generating inline code. Inline code is often necessary in embedded systems in order to achieve the required performance level

            Knowledge of the ternary conditional operator. This operator exists in C because it allows the compiler to produce more optimal code than an if-then-else sequence. Given that performance is normally an issue in embedded systems, knowledge and use of this construct is important

            Understanding of the need to very carefully parenthesize arguments to macros

            I also use this question to start a discussion on the side effects of macros, for example, what happens when you write code such as:

            least = MIN(*p++, b);



            3. What is the purpose of the preprocessor directive #error?

            Either you know the answer to this, or you don't. If you don't, see Reference 1. This question is useful for differentiating between normal folks and the nerds. Only the nerds actually read the appendices of C textbooks to find out about such things. Of course, if you aren't looking for a nerd, the candidate better hope she doesn't know the answer.



            Infinite loops
            4. Infinite loops often arise in embedded systems. How does you code an infinite loop in C?

            There are several solutions to this question. My preferred solution is:

            while(1)

            {

            ?

            }



            Many programmers seem to prefer:

            for(;;)

            {

            ?

            }



            This construct puzzles me because the syntax doesn't exactly spell out what's going on. Thus, if a candidate gives this as a solution, I'll use it as an opportunity to explore their rationale for doing so. If their answer is basically, "I was taught to do it this way and I haven't thought about it since," it tells me something (bad) about them.

            A third solution is to use a goto :



            Loop:

            ...

            goto Loop;







            Candidates who propose this are either assembly language programmers (which is probably good), or else they are closet BASIC/FORTRAN programmers looking to get into a new field.



            Data declarations
            5. Using the variable a, give definitions for the following:
            a) An integer
            b) A pointer to an integer
            c) A pointer to a pointer to an integer
            d) An array of 10 integers
            e) An array of 10 pointers to integers
            f) A pointer to an array of 10 integers
            g) A pointer to a function that takes an integer as an argument and returns an integer
            h) An array of ten pointers to functions that take an integer argument and return an integer

            The answers are:
            a) int a; // An integer
            b) int *a; // A pointer to an integer
            c) int **a; // A pointer to a pointer to an integer
            d) int a[10]; // An array of 10 integers
            e) int *a[10]; // An array of 10 pointers to integers
            f) int (*a)[10]; // A pointer to an array of 10 integers
            g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer
            h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer

            People often claim that a couple of these are the sorts of thing that one looks up in textbooks-and I agree. While writing this article, I consulted textbooks to ensure the syntax was correct. However, I expect to be asked this question (or something close to it) when I'm being interviewed. Consequently, I make sure I know the answers, at least for the few hours of the interview. Candidates who don't know all the answers (or at least most of them) are simply unprepared for the interview. If they can't be prepared for the interview, what will they be prepared for?



            Static
            6. What are the uses of the keyword static?

            This simple question is rarely answered completely. Static has three distinct uses in C:

            A variable declared static within the body of a function maintains its value between function invocations

            A variable declared static within a module, (but outside the body of a function) is accessible by all functions within that module. It is not accessible by functions within any other module. That is, it is a localized global

            Functions declared static within a module may only be called by other functions within that module. That is, the scope of the function is localized to the module within which it is declared

            Most candidates get the first part correct. A reasonable number get the second part correct, while a pitiful number understand the third answer. This is a serious weakness in a candidate, since he obviously doesn't understand the importance and benefits of localizing the scope of both data and code.

            Const


            7. What does the keyword const mean?

            As soon as the interviewee says "const means constant," I know I'm dealing with an amateur. Dan Saks has exhaustively covered const in the last year, such that every reader of ESP should be extremely familiar with what const can and cannot do for you. If you haven't been reading that column, suffice it to say that const means "read-only." Although this answer doesn't really do the subject justice, I'd accept it as a correct answer. (If you want the detailed answer, read Saks' columns-carefully!)

            If the candidate gets the answer correct, I'll ask him these supplemental questions:

            What do the following declarations mean?



            const int a;

            int const a;

            const int *a;

            int * const a;

            int const * a const;



            The first two mean the same thing, namely a is a const (read-only) integer. The third means a is a pointer to a const integer (that is, the integer isn't modifiable, but the pointer is). The fourth declares a to be a const pointer to an integer (that is, the integer pointed to by a is modifiable, but the pointer is not). The final declaration declares a to be a const pointer to a const integer (that is, neither the integer pointed to by a, nor the pointer itself may be modified). If the candidate correctly answers these questions, I'll be impressed. Incidentally, you might wonder why I put so much emphasis on const, since it is easy to write a correctly functioning program without ever using it. I have several reasons:

            The use of const conveys some very useful information to someone reading your code. In effect, declaring a parameter const tells the user about its intended usage. If you spend a lot of time cleaning up the mess left by other people, you'll quickly learn to appreciate this extra piece of information. (Of course, programmers who use const , rarely leave a mess for others to clean up.)

            const has the potential for generating tighter code by giving the optimizer some additional information

            Code that uses const liberally is inherently protected by the compiler against inadvertent coding constructs that result in parameters being changed that should not be. In short, they tend to have fewer bugs



            Volatile
            8. What does the keyword volatile mean? Give three different examples of its use.

            A volatile variable is one that can change unexpectedly. Consequently, the compiler can make no assumptions about the value of the variable. In particular, the optimizer must be careful to reload the variable every time it is used instead of holding a copy in a register. Examples of volatile variables are:

            Hardware registers in peripherals (for example, status registers)

            Non-automatic variables referenced within an interrupt service routine

            Variables shared by multiple tasks in a multi-threaded application

            Candidates who don't know the answer to this question aren't hired. I consider this the most fundamental question that distinguishes between a C programmer and an embedded systems programmer. Embedded folks deal with hardware, interrupts, RTOSes, and the like. All of these require volatile variables. Failure to understand the concept of volatile will lead to disaster.

            On the (dubious) assumption that the interviewee gets this question correct, I like to probe a little deeper to see if they really understand the full significance of volatile . In particular, I'll ask them the following additional questions:

            Can a parameter be both const and volatile ? Explain.

            Can a pointer be volatile ? Explain.

            What's wrong with the following function?:



            int square(volatile int *ptr)

            {

            return *ptr * *ptr;

            }



            The answers are as follows:

            Yes. An example is a read-only status register. It is volatile because it can change unexpectedly. It is const because the program should not attempt to modify it

            Yes, although this is not very common. An example is when an interrupt service routine modifies a pointer to a buffer

            This one is wicked. The intent of the code is to return the square of the value pointed to by *ptr . However, since *ptr points to a volatile parameter, the compiler will generate code that looks something like this:



            int square(volatile int *ptr)

            {

            int a,b;

            a = *ptr;

            b = *ptr;

            return a * b;

            }



            Because it's possible for the value of *ptr to change unexpectedly, it is possible for a and b to be different. Consequently, this code could return a number that is not a square! The correct way to code this is:



            long square(volatile int *ptr)

            {

            int a;

            a = *ptr;

            return a * a;

            }



            Bit manipulation
            9. Embedded systems always require the user to manipulate bits in registers or variables. Given an integer variable a, write two code fragments. The first should set bit 3 of a. The second should clear bit 3 of a. In both cases, the remaining bits should be unmodified.

            These are the three basic responses to this question:

            No idea. The interviewee cannot have done any embedded systems work

            Use bit fields. Bit fields are right up there with trigraphs as the most brain-dead portion of C. Bit fields are inherently non-portable across compilers, and as such guarantee that your code is not reusable. I recently had the misfortune to look at a driver written by Infineon for one of their more complex communications chips. It used bit fields and was completely useless because my compiler implemented the bit fields the other way around. The moral: never let a non-embedded person anywhere near a real piece of hardware!

            Use #defines and bit masks. This is a highly portable method and is the one that should be used. My optimal solution to this problem would be:



            #define BIT3 (0x1 << 3)

            static int a;

            void set_bit3(void) {

            a |= BIT3;

            }

            void clear_bit3(void) {

            a &= ~BIT3;

            }



            Some people prefer to define a mask together with manifest constants for the set and clear values. This is also acceptable. The element that I'm looking for is the use of manifest constants, together with the |= and &= ~ constructs



            Accessing fixed memory locations
            10. Embedded systems are often characterized by requiring the programmer to access a specific memory location. On a certain project it is required to set an integer variable at the absolute address 0x67a9 to the value 0xaa55. The compiler is a pure ANSI compiler. Write code to accomplish this task.

            This problem tests whether you know that it is legal to typecast an integer to a pointer in order to access an absolute location. The exact syntax varies depending upon one's style. However, I would typically be looking for something like this:



            int *ptr;

            ptr = (int *)0x67a9;

            *ptr = 0xaa55;



            A more obscure approach is:



            *(int * const)(0x67a9) = 0xaa55;



            Even if your taste runs more to the second solution, I suggest the first solution when you are in an interview situation.



            Interrupts
            11. Interrupts are an important part of embedded systems. Consequently, many compiler vendors offer an extension to standard C to support interrupts. Typically, this new keyword is __interrupt. The following code uses __interrupt to define an interrupt service routine (ISR). Comment on the code.



            __interrupt double compute_area

            (double

            radius)

            {

            double area = PI * radius *

            radius;

            printf("\nArea = %f", area);

            return area;

            }



            This function has so much wrong with it, it's hard to know where to start:

            ISRs cannot return a value. If you don't understand this, you aren't hired

            ISRs cannot be passed parameters. See the first item for your employment prospects if you missed this

            On many processors/compilers, floating-point operations are not necessarily re-entrant. In some cases one needs to stack additional registers. In other cases, one simply cannot do floating point in an ISR. Furthermore, given that a general rule of thumb is that ISRs should be short and sweet, one wonders about the wisdom of doing floating-point math here

            In a vein similar to the third point, printf() often has problems with reentrancy and performance. If you missed points three and four, I wouldn't be too hard on you. Needless to say, if you got these last two points, your employment prospects are looking better and better



            Code examples
            12. What does the following code output and why?

            void foo(void)

            {

            unsigned int a = 6;

            int b = -20;

            (a+b > 6) ? puts("> 6") :

            puts("

            <

            = 6");

            }



            This question tests whether you understand the integer promotion rules in C-an area that I find is very poorly understood by many developers. Anyway, the answer is that this outputs "> 6." The reason for this is that expressions involving signed and unsigned types have all operands promoted to unsigned types. Thus ?20 becomes a very large positive integer and the expression evaluates to greater than 6. This is a very important point in embedded systems where unsigned data types should be used frequently (see Reference 2). If you get this one wrong, you are perilously close to not getting the job.



            13. Comment on the following code fragment.

            unsigned int zero = 0;

            unsigned int compzero = 0xFFFF;

            /*1's complement of zero */



            On machines where an int is not 16 bits, this will be incorrect. It should be coded:

            unsigned int compzero = ~0;



            This question really gets to whether the candidate understands the importance of word length on a computer. In my experience, good embedded programmers are critically aware of the underlying hardware and its limitations, whereas computer programmers tend to dismiss the hardware as a necessary annoyance.

            By this stage, candidates are either completely demoralized-or they're on a roll and having a good time. If it's obvious that the candidate isn't very good, then the test is terminated at this point. However, if the candidate is doing well, then I throw in these supplemental questions. These questions are hard, and I expect that only the very best candidates will do well on them. In posing these questions, I'm looking more at the way the candidate tackles the problems, rather than the answers. Anyway, have fun...



            Dynamic memory allocation
            14. Although not as common as in non-embedded computers, embedded systems do still dynamically allocate memory from the heap. What are the problems with dynamic memory allocation in embedded systems?

            Here, I expect the user to mention memory fragmentation, problems with garbage collection, variable execution time, and so on. This topic has been covered extensively in ESP , mainly by P.J. Plauger. His explanations are far more insightful than anything I could offer here, so go and read those back issues! Having lulled the candidate into a sense of false security, I then offer up this tidbit:

            What does the following code fragment output and why?



            char *ptr;

            if ((ptr = (char *)malloc(0)) ==

            NULL)

            else

            puts("Got a null pointer");

            puts("Got a valid pointer");



            This is a fun question. I stumbled across this only recently when a colleague of mine inadvertently passed a value of 0 to malloc and got back a valid pointer! That is, the above code will output "Got a valid pointer." I use this to start a discussion on whether the interviewee thinks this is the correct thing for the library routine to do. Getting the right answer here is not nearly as important as the way you approach the problem and the rationale for your decision.



            Typedef
            15. Typedef is frequently used in C to declare synonyms for pre-existing data types. It is also possible to use the preprocessor to do something similar. For instance, consider the following code fragment:



            #define dPS struct s *

            typedef struct s * tPS;



            The intent in both cases is to define dPS and tPS to be pointers to structure s. Which method, if any, is preferred and why?

            This is a very subtle question, and anyone who gets it right (for the right reason) is to be congratulated or condemned ("get a life" springs to mind). The answer is the typedef is preferred. Consider the declarations:

            dPS p1,p2;

            tPS p3,p4;



            The first expands to:

            struct s * p1, p2;



            which defines p1 to be a pointer to the structure and p2 to be an actual structure, which is probably not what you wanted. The second example correctly defines p3 and p4 to be pointers.



            Obscure syntax
            16. C allows some appalling constructs. Is this construct legal, and if so what does this code do?



            int a = 5, b = 7, c;

            c = a+++b;



            This question is intended to be a lighthearted end to the quiz, as, believe it or not, this is perfectly legal syntax. The question is how does the compiler treat it? Those poor compiler writers actually debated this issue, and came up with the "maximum munch" rule, which stipulates that the compiler should bite off as big (and legal) a chunk as it can. Hence, this code is treated as:

            c = a++ + b;

            Thus, after this code is executed, a = 6, b = 7, and c = 12.

            If you knew the answer, or guessed correctly, well done. If you didn't know the answer then I wouldn't consider this to be a problem. I find the greatest benefit of this question is that it is good for stimulating questions on coding styles, the value of code reviews, and the benefits of using lint.

            Well folks, there you have it. That was my version of the C test. I hope you had as much fun taking it as I had writing it. If you think the test is a good test, then by all means use it in your recruitment. Who knows, I may get lucky in a year or two and end up being on the receiving end of my own work.

            Nigel Jones is a consultant living in Maryland. When not underwater, he can be found slaving away on a diverse range of embedded projects. He enjoys hearing from readers and can be reached at NAJones@compuserve.com .

            References

            Jones, Nigel, "In Praise of the #error directive," Embedded Systems Programming, September 1999, p. 114.

            Jones, Nigel, " Efficient C Code for Eight-bit MCUs ," Embedded Systems Programming, November 1998, p. 66.

             原文地址 http://blog.csdn.net/sailor_8318/archive/2008/03/25/2215041.aspx
            posted on 2008-11-28 08:41 Xiao.Zhu 閱讀(428) 評(píng)論(0)  編輯 收藏 引用

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


            久久精品一区二区| 久久夜色精品国产www| 热久久最新网站获取| 久久青青色综合| 久久精品无码专区免费东京热| 女人香蕉久久**毛片精品| 韩国三级中文字幕hd久久精品| 亚洲国产精品无码久久青草| 久久久久亚洲AV无码专区体验| 一本大道久久a久久精品综合| 久久夜色精品国产噜噜亚洲a| 色偷偷久久一区二区三区| 久久国产香蕉视频| 久久精品国产亚洲AV无码娇色| 久久亚洲国产成人精品无码区| 丁香色欲久久久久久综合网| 久久成人国产精品一区二区| 波多野结衣AV无码久久一区| 久久国产影院| 国产成人香蕉久久久久| 久久婷婷成人综合色综合| 欧美精品丝袜久久久中文字幕 | 亚洲午夜久久久久妓女影院| 91精品国产91久久久久久青草 | 久久涩综合| 99久久精品免费看国产免费| 久久精品99久久香蕉国产色戒| 日韩久久久久中文字幕人妻| 国产精品午夜久久| 久久精品国产亚洲综合色| 欧美黑人又粗又大久久久| 亚洲精品乱码久久久久久| 人妻无码精品久久亚瑟影视| 久久九九久精品国产| 99久久综合国产精品二区| 久久精品国产69国产精品亚洲| 国产产无码乱码精品久久鸭| 久久久久无码精品国产| 精品久久久久久亚洲精品| 久久无码人妻一区二区三区午夜| 区久久AAA片69亚洲|