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

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

            從 被面試者的角度來講,你能了解許多關于出題者或監考者的情況。這個測試只是出題者為顯示其對ANSI標準細節的知識而不是技術技巧而設計嗎?這是個愚蠢的 問題嗎?如要你答出某個字符的ASCII值。這些問題著重考察你的系統調用和內存分配策略方面的能力嗎?這標志著出題者也許花時間在微機上而不是在嵌入式 系統上。如果上述任何問題的答案是"是"的話,那么我知道我得認真考慮我是否應該去做這份工作。

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

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

            這個測試適于不同水平的應試者,大多數初級水平的應試者的成績會很差,經驗豐富的程序員應該有很好的成績。為了讓你能自己決定某些問題的偏好,每個問題沒有分配分數,如果選擇這些考題為你所用,請自行按你的意思分配分數。



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

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

            我在這想看到幾件事情:

            1) #define 語法的基本知識(例如:不能以分號結束,括號的使用(表達式、參數等要括起來),等等)

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

            3) 意識到這個表達式將使一個16位機的整型數溢出-因此要用到長整型符號L,告訴編譯器這個常數是的長整型數。

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

            2、如何定義宏
            寫一個"標準"宏MIN ,這個宏輸入兩個參數并返回較小的一個。

            考點:(表達式、參數等要括起來)

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

            這個測試是為下面的目的而設的:

            1) 標識#define在宏中應用的基本知識。這是很重要的。因為在嵌入(inline)操作符變為標準C的一部分之前,宏是方便產生嵌入代碼的唯一方法,對 于嵌入式系統來說,為了能達到要求的性能(當然主要是實時性哦,犧牲代碼空間換取時間效率),嵌入代碼經常是必須的方法。

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

            3) 懂得在宏中小心地把參數用括號括起來

            4) 我也用這個問題開始討論宏的副作用,例如:當你寫下面的代碼時會發生什么事?

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



            此處考點:inline函數和宏的區別

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

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



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

            3、宏定義的常量在代碼區,很多調試器不能夠對其調試
            我們舉個例子:
            #define square(x) (x*x)

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

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


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


            當然,內聯函數也有一定的局限性。就是函數中的執行代碼不能太多了,如果,內聯函數的函數體過大,一般的編譯器會放棄內聯方式,而采用普通的方式調用函數。這樣,內聯函數就和普通函數執行效率一樣了。
            有上面的兩者的特性,我們可以用內聯函數完全取代預處理宏。



            3、預處理器標識#error的目的是什么?
            如果你不知道答案,請看參考文獻1。這問題對區分一個正常的伙計和一個書呆子是很有用的。只有書呆子才會讀C語言課本的附錄去找出象這種問題的答案。當然如果你不是在找一個書呆子,那么應試者最好希望自己不要知道答案。



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

            我首選的方案是:

            while(1)

            {



            }

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

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

            {



            }

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



            第三個方案是用 goto (goto語句在C中是應該盡量避免的,只在處理錯誤代碼時用)

            Loop:

            ...

            goto Loop;

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



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

            a) 一個整型數(An integer)

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

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

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

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

            f) 一個指向有10個整型數數組的指針( 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) 一個有10個指針的數組,該指針指向一個函數,該函數有一個整型參數并返回一個整型數( 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),不需要具體的參數

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

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



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

            1)在函數體內,一個被聲明為靜態的變量在這一函數被調用過程中維持其值不變(該變量存放在靜態變量區)。

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

            3) 在模塊內,一個被聲明為靜態的函數只可被這一模塊內的其它函數調用。那就是,這個函數被限制在聲明它的模塊的本地范圍內使用。

            大多數應試者能正確回答第一部分,一部分能正確回答第二部分,但是很少的人能懂得第三部分。這是一個應試者的嚴重的缺點,因為他顯然不懂得本地化數據和代碼范圍的好處和重要性。



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

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



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

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

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



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

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

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

            const int a;

            int const a;

            const int *a;

            int * const a;

            int const * a const;

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



            前兩個的作用是一樣,a是一個常整型數。第三個意味著a是一個指向常整型數的指針(也就是,指向的整型數是不可修改的,但指針可以,此最常見于函數的參數,當你只引用傳進來指針所指向的值時應該加上const修飾符,程序中修改編譯就不通過,可以減少程序的bug)。



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



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

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

            2) 通過給優化器一些附加的信息,使用關鍵字const也許能產生更緊湊的代碼。

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



            const關鍵字至少有下列n個作用:

            (1)欲阻止一個變量被改變,可以使用const關鍵字。在定義該const變量時,通常需要對它進行初始化,因為以后就沒有機會再去改變它了;
            (2)對指針來說,可以指定指針本身為const,也可以指定指針所指的數據為const,或二者同時指定為const;
            (3)在一個函數聲明中,const可以修飾形參,表明它是一個輸入參數,在函數內部不能改變其值;
            (4)對于類的成員函數,若指定其為const類型,則表明其是一個常函數,不能修改類的成員變量;
            (5)對于類的成員函數,有時候必須指定其返回值為const類型,以使得其返回值不為“左值”。例如:
            const classA operator*(const classA& a1,const classA& a2);
              operator*的返回結果必須是一個const對象。如果不是,這樣的變態代碼也不會編譯出錯:
            classA a, b, c;
            (a * b) = c; // 對a*b的結果賦值
              操作(a * b) = c顯然不符合編程者的初衷,也沒有任何意義。



            8、Volatile的使用
            關鍵字volatile有什么含意?并給出三個不同的例子。

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

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

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

            3) 多線程應用中被幾個任務共享的變量(可能被多個線程隨時修改)



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

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

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

            3); 下面的函數有什么錯誤:

            int square(volatile int *ptr)

            {

            return *ptr * *ptr;

            }

            下面是答案:

            1)是的。一個例子是只讀的狀態寄存器。它是volatile因為它可能被意想不到地改變。它是const因為程序不應該試圖去修改它。

            2); 是的。盡管這并不很常見。一個例子是當一個中斷服務子程序修改一個指向一個buffer的指針時。

            3) 這段代碼有點變態。這段代碼的目的是用來返回指針*ptr指向值的平方,但是,由于*ptr指向一個volatile型參數,編譯器將產生類似下面的代碼:

            int square(volatile int *ptr)

            {

            int a,b;

            a = *ptr;

            b = *ptr;

            return a * b;

            }

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

            long square(volatile int *ptr)

            {

            int a;

            a = *ptr;

            return a * a;

            }



            關于volatile關鍵字在中斷函數中的影響實例



            串口發送數據,中斷中對其檢測,當中斷產生后,置接收標志,主循環中檢測此主標志,未用valotile修飾時,編譯結果如下:



            [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)

            即 編譯器對其進行了優化,讀取一次uart1_rxFlag的值之后,將其存放在寄存器r0中,比較后,條件不滿足,繼續等待,但未重新取存儲器中 uart1_rxFlag的值,此時即使中斷服務函數中修改了uart1_rxFlag的值,比較處仍然不能發現,就出現了無論如何程序就停在此處的問 題。



            // 加了volatile關鍵字后,編譯的結果

            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)

            添加了關鍵字后,比較不等,跳轉到重新取存儲器中的uart1_rxFlag,因此任何時候uart1_rxFlag的值都是最新的。

            一定程度的優化,去掉了讀取uart1_rxFlag地址的語句。



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



            多任務環境下各任務間共享的標志應該加voatile關鍵字:在多線程訪問某字段時,代碼希望這些訪問能夠操作(讀取)到字段的最新值,同時寫到變量的操作能立即更新;對字段加上volatile關鍵字,那么對該字段的任何請求(讀/寫)都會立刻得到執行。



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

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

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

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

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

            static int a;

            void set_bit3(void)

            {

            a |= BIT3;

            }

            void clear_bit3(void)

            {

            a &= ~BIT3;

            }



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



            考點:

            在嵌入式系統中,時刻要關注移植性,具體的程序中不要出現具體的數字,這些數字都應該define成某個有意義的符號,可讀性可移植性都很強,比如

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

            X作為參數可以很方便的對任意位進行操作,意義明確,更改替換方便



            10、訪問固定的內存位置(Accessing fixed memory locations)
            嵌入式系統經常具有要求程序員去訪問某特定的內存位置的特點。

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

            int *ptr;

            ptr = (int *)0x67a9;

            *ptr = 0xaa55;

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

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

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



            在嵌入式系統中,對于大量此類型數據如硬件寄存器應該采用如下方式

            typedef volatile unsigned int HARD_REG;

            #define REG_NAME (*(HARD_REG *)ADDR)

            即將ADDR強制轉換為一個指向HARD_REG類型數據的指針,*HARD_REG為volatile的無符號整型數



            11、中斷(Interrupts)
            中 斷是嵌入式系統中重要的組成部分,這導致了很多編譯開發商提供一種擴展-讓標準C支持中斷。其代表事實是,產生了一個新的關鍵字 __interrupt(51即如此)。下面的代碼就使用了__interrupt關鍵字去定義了一個中斷服務子程序(ISR),請評論一下這段代碼的。

            __interrupt double compute_area (double radius)

            {

            double area = PI * radius * radius;

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

            return area;

            }

            這個函數有太多的錯誤了,以至讓人不知從何說起了(前提是非操作系統下的中斷服務函數):

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

            2)ISR 不能傳遞參數。如果你沒有看到這一點,你被雇用的機會等同第一項。

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

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

            另外中斷服務程序是運行在內核態的(linux),內核通常是不支持浮點運算的。

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

            內核中的printk和標準庫的printf不一樣,前者因為由內核直接實現,不能支持浮點。

            在<linux內核設計與實現>的第一章中內核開發的特點一小節里就有比較了內核開發與應用開發的差異。其中一點就是內核編程時浮點數的問題,書中有一句話是:內核編程時浮點數很難使用

            因為沒有浮點單元,內核要支持浮點必須把內核以soft-float 方式重新編譯,其連接所有的庫也都要用soft-float 方式編譯.

            否則另外一種方式使用整數定義浮點類型加浮點預算庫完成你的工作,



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

            如果你的內核里編譯進了浮點支持,那么是可以的。要不內核或是模塊不能用float或是double內型的變量或函數



            在配置內核的時候把浮點模擬器選上,應該是可以支持的,但是速度非常慢。
            我曾經遇到過,硬件明明支持浮點運算的FPU,但是編譯內核的時候選上了浮點模擬器,結果所有的應用程序的浮點運算速度都非常慢。所以我懷疑要支持浮點只要編譯內核的時候選上,對于應用程序不需要怎么關心。

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



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



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

            void foo(void)

            {

            unsigned int a = 6;

            int b = -20;

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

            }

            Vc6.0測試情況

            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*/

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



            13、處理器字長導致的數據擴展問題
            評價下面的代碼片斷:

            unsigned int zero = 0;

            unsigned int compzero = 0xFFFF;

            /*1's complement of zero */ 0的補碼為全1的數

            對于一個int型不是16位的處理器為說,上面的代碼是不正確的。應編寫如下:

            unsigned int compzero = ~0;

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



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



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

            char *ptr;

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

            puts("Got a null pointer");

            else

            puts("Got a valid pointer");

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

            返回一個控指針還是指向 0 字節的指針甚至指向一個可以操作的指針?

            (取決于系統平臺的實現,C99及其他標準規定可以不同的)

            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構造復合類型
            在C語言中頻繁用以聲明一個已經存在的數據類型的同義字。也可以用預處理器做類似的事。例如,思考一下下面的例子:

            #define dPS struct s *

            typedef struct s * tPS;

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

            這是一個非常微妙的問題,任何人答對這個問題(正當的原因哦,而不是猜,如果你沒有原因,說不會比猜一個答案要好的多,記住啊,說話是要講根據的)是應當被恭喜的。答案是:typedef更好。思考下面的例子:

            dPS p1,p2;

            tPS p3,p4;

            第一個擴展為

            struct s * p1, p2;

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



            16、晦澀的語法及代碼風格
            C語言同意一些令人震驚的結構,下面的結構是合法的嗎,如果是它做些什么?

            int a = 5, b = 7, c;

            c = a+++b;

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

            逗號表達式依次對每個表達式計算,最后的結果為最后一個表達式的值

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

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



            注:引出代碼風格的問題正是作者問此問題的目的,這告訴我們要揣摩面試管每個問題背后隱藏的考查點,能夠趁機發揮下就大功告成了!



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



            以下為上述16個問題的英文表述,熟悉下相關的專業詞匯對于英文面試的簡單表述很重要。

            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 閱讀(424) 評論(0)  編輯 收藏 引用
            99精品久久精品一区二区| 欧美午夜A∨大片久久| 久久久噜噜噜久久熟女AA片 | 久久免费精品视频| 久久久久噜噜噜亚洲熟女综合| 久久精品国产99久久香蕉| 亚洲欧美国产精品专区久久| 色综合久久无码中文字幕| Xx性欧美肥妇精品久久久久久| 九九精品久久久久久噜噜| 国产精品视频久久久| 久久精品免费一区二区| 国内精品久久久久久不卡影院| 欧美熟妇另类久久久久久不卡| 久久国产精品二国产精品| 人妻无码αv中文字幕久久| 亚洲欧美一级久久精品| 国产精品99久久久久久www| 国内精品久久久久影院一蜜桃| 亚洲国产精品嫩草影院久久| 久久九九亚洲精品| 996久久国产精品线观看| 久久精品成人欧美大片| 久久久黄色大片| 午夜精品久久久久久久无码| 国产精品丝袜久久久久久不卡| 999久久久免费精品国产| 亚洲国产精品18久久久久久| 久久综合久久美利坚合众国| 一级女性全黄久久生活片免费 | 色噜噜狠狠先锋影音久久| 久久久久久午夜成人影院| 日韩人妻无码精品久久久不卡| 久久久久久久久久久| 久久人人爽人人爽人人片AV麻烦| 午夜精品久久久内射近拍高清 | 精品无码久久久久久久久久 | 久久综合鬼色88久久精品综合自在自线噜噜| 国产精品无码久久综合网| 国产精品美女久久久久av爽| Xx性欧美肥妇精品久久久久久|