顯示具有 c 標籤的文章。 顯示所有文章
顯示具有 c 標籤的文章。 顯示所有文章

【C】call by address


基本上interger的交換都是以swap當例子。參考 http://xpode.com/ShowArticle.aspx?Articleid=72
不過今天碰到一個詭異的現象。先研究一下再來寫報告。

【Update】
經過最不花腦筋的二分之一分割法(就是把code分成1/2,再由有問題的部分分成1/2,慢慢濃縮問題點)之後,確定是一個function出問題,這function會切回real-mode去抓edid的資料,可能是由保護模式切到真實模式stack沒處理好,目前先直接用全域變數來接回傳的資料。

【C】Coding Convention(tbd)


Linux kernel coding style(http://lxr.linux.no/linux/Documentation/CodingStyle)也算是coding convention的聖經吧。老實說我覺得規定coding style是對團體的考量多於個人的考量。按照這份文件,就我知道的寫一下。

Chapter 1: Indentation

  • tab/indent要八個,這也是programmer的麻吉vi預設的設定。
  • 一行有80個欄(columns),方便打印與觀看。

Chapter 2: Breaking long lines and strings

  • function的寫法,開始的大括號在function宣告下面。
  • if/witch/do-while的寫法,開始的大括號在function宣告後面。
  • 判斷是如果只有一個conditional statement可以不用加括號(不過這點本人不太建議)
  • if-else 就算單一個conditional statement也要加括號。

3.1: Spaces

  • 在下面的keywords後空一格
    if, switch, case, for, do, while but not with sizeof, typeof, alignof, or __attribute__. E.g.,
  • 運算元前後個各空一格。

Chapter 4: Naming

  • local的話,善用tmp這類的變數,不要把暫存的資料變數名稱取名的落落長,越短越好。
  • global的話,就要特別注意,如果取作foo就是自討苦吃。
  • 在這邊的function名稱不流行大大小小(大寫混合小寫的)命名宣告,以小寫配合底線去宣告,例如count_active_users()。

Chapter 5: Typedefs

  • 如果是struct宣告的話,使用
    struct virtual_container *a;
    不要使用
    vps_t a;
  • 後面哩哩叩叩講了一堆,有空再看。

Chapter 6: Functions

Chapter 7: Centralized exiting of functions

Chapter 8: Commenting

  • 這邊講解註解要寫些甚麼。
  • 這邊用的是 C89 "/* ... */" 這種格式。
  • 不要用C99-style "// ..." 的格式。

Chapter 9: You've made a mess of it

Chapter 10: Kconfig configuration files

Chapter 11: Data structures

Chapter 12: Macros, Enums and RTL

Chapter 13: Printing kernel messages

  • 確保不含糊的輸出你的debug訊息。
  • "dont" 應該改成 "do not” 或 "don’t”。
  • 不要只單純輸出數字,像“(%d)”。
  • 研究一下linux/device.h善用下面現成的debug宣告。
  • dev_err(), dev_warn(), dev_info()
  • pr_debug() and pr_info().
  • 用Kconfig options/-DDEBUG來掌控你的debug marco。
  • 研究一下VERBOSE_DEBUG/add dev_vdbg(),這是和Kconfig有關的。

Chapter 14: Allocating memory

  • 善用kernel提供的memory alloc。
  • kmalloc(), kzalloc(), kcalloc(), and vmalloc()
  • (tbd)

Chapter 15: The inline disease

  • 講了一些在那些狀況下不要用inline。可以好好看一下本文。
  • kmalloc()提供一個不錯的範例。
  • 其實個人覺得inline是由compiler決定要編成macro或function,這和compiler的版本比較有關係(看compiler寫得好不好)。

Chapter 16: Function return values and names

  • 分成 static和 publc function來說,public的要沿用下面規則
  • 如果是命令的話(ex. add_work),返回0代表成功;而-EBUSY代表失敗。
  • 如果是述語(predicate)的話(ex.pci_dev_present()),1代表成功;0的話是失敗。
  • point的話以NULL或ERR_PTR代表成功或失敗。

Chapter 17: Don't re-invent the kernel macros

  • 像以下這些現成的Macros就別再自行宣告了。
  • ARRAY_SIZE
  • FIELD_SIZEOF
  • max/min

Chapter 18: Editor modelines and other cruft

  • vim/emacs會把編輯器的配置利用註解方式(/* */)嵌入在source code中,記得把它拿掉。以免造成別人的不便。

Appendix I: References

  • 最後再丟幾本聖經給你。
  • The C Programming Language, Second Edition
  • The Practice of Programming
  • GNU manuals
  • WG14
  • Kernel CodingStyle

astyle http://astyle.sourceforge.net/

【C】更改const變數內容


【宣告】
extern char const timer; 


【更改】

void * ptr; 
ptr = (void *) &timer; 
*((tU1 *) ptr) = 0;

【C】Interview questions


先把會問到的題目複習一次,答案先記下來。有時候在面試官之前寫程式,和在電腦面前coding的感覺不同,很容易因為心情緊張而under performance。先就自己知道先列一下,以後再補充。

【函數類】

叫你去Implement一些function。看你的基本觀念正不正確。

  1. atoi
  2. strcmp/strcpy
  3. exchange x/y
  4. 計算32bit裡面1的數量

【宣告/定義】

  1. volatile
  2. static宣告,分別在變數與函數中的意義
  3. const宣告

【觀念】

  1. Context switch

【Point/Reference】

  1. 問一些 * & 的觀念。

【Network】

  1. OSI七層
  2. ntos

【實作類】

socket觀念實作,雖然不太可能考這個,不過還是要準備一下。

  1. 寫一個web server
  2. 寫一個 ftp server

【C】Data size


每種機器上面的data type size不一定一樣,與其聽別人說,不如自己驗證。
#include <stdio.h>
#define BYTES 8
int main(void)
{
        printf("Get Data Type size...\n");
        printf("int    %d\n", sizeof(int) * BYTES);
        printf("float  %d\n", sizeof(float) * BYTES);
        printf("double %d\n", sizeof(double) * BYTES);
        printf("char   %d\n", sizeof(char) * BYTES);
        printf("unsigned long %d\n", sizeof(unsigned long) * BYTES);
#if 0
        printf("  %d\n", sizeof() * BYTES);
#endif
        printf("short %d\n", sizeof(short) * BYTES);
        printf("long  %d\n", sizeof(long) * BYTES);
        return 0;
}

【C】function point


有的code會把function point放在 struct裡面。

【宣告方式】
參考 usbtouch_device_info,其中的 read_date 和 init 就是使用 function point。

【更多資料】

【C】Debug 暫存器


通常都會把暫存器的讀寫配合 volatile 作成一個巨集,
如果要把暫存器的位址與內容印出來的話,可以把呼叫它的function
弄成一樣的參數,雖然這樣子比較醜,不過用起來比較實用。

【C】Trace INIT_WORK


在 Trace 延遲函數INIT_WORK,令人頭疼到不行的就是跟蹤困難。
不過我發現可以用這個參數來作跟蹤。
__builtin_return_address

另外也可使用Backtrace
  • http://blog.richliu.com/2007/04/18/467/

【參考】

【C】Array/Queue/Stack


Array
Queue
Stack

【FONT】造字與顯示中文


FreeType2
TrueType
Gtk+
QT
Misc

【C】用C寫OO


C++雖然有OO的觀念程式容易擴充但有時build出來的bin也蠻大的,有時就須使用C來搞定使用OO觀念的東西。

【參考】

Some code borrowed from…


如果我們有跟別人借code的話,記的加 
Some code borrowed from …

【參考】Some code borrowed from the Linux EHCI driver http://lxr.linux.no/linux+v2.6.31/drivers/usb/host/xhci-hcd.c

【C】利用sizeof取得陣列數量


static tU1 list[][8] = { "how", "are", "you" };
#define NUM_OF_LIST sizeof(list) / sizeof(list[0])

sprintf用法


sprintf(file_path + file_path_len, "%s", file_name);
 

Ed32. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com