# 2018q3 Homework1 contributed by < `kevin85421` > ## 開發工具和規格標準 ```clike char* ptr; // data type : char* ; [64-bit] 8 bytes char str[] = "Hello World"; // data type : char [] ; 11 bytes ptr = str; // No warning ``` [Reference : 深入探討char * ,char ** ,char a[ ] ,char *a[ ]](https://blog.csdn.net/daiyutage/article/details/8604720) * 在「你所不知道的C語言」系列中,探討之版本為ISO/IEC 9899 (簡稱 “C99”) --- ### 為何要用 GDB * trace code 更加迅速 * 使用 watch 可以快速找出 segmentation fault * 節省反覆 build code 的時間 ### 為何要用 GDB (個人經驗) * 之前實習時有參與一個 commercial tool 的開發,整個 tool 規模大約為 170 萬行的 C/C++ code。在此 tool 當中 ```printf / cout``` 無法直觀運作。 * 在很多情況可能 100 萬筆測資中可能只有十筆資料有錯誤,此時運用 break point 搭配 condition 的設定可以快速進入該測資。 * 在 segmentation fault 的時候,GDB 可以透過 watch 快速定位。 * **在 commercial tool 的開發中時間分佈為「找到 bug」50%、「debug」20%、「implement」30%。因此 debugger 在大型的 project 中運作的很好。** --- ### Vim (個人經驗) * 容易擴充功能 * ```ctag``` 讓閱讀 code 更加容易 --- 指標篇 === ## C Traps and Pitfalls ```clike (*(void(*)())0)(); ``` [Reference](https://blog.csdn.net/tennysonsky/article/details/69055774) * 上式之目的: I once talked to someone who was writing a C program that was going to run stand-alone in a small microprocessor. When this machine was switched on, the hardware would call the subroutine whose address was stored in location 0. * 假設 fp 為一個 functional pointer,使用 ```(*fp)()``` 呼叫 fp 所指向的函數。 * 步驟: * **Step 1 :** ```clike (void(*)())0 ``` 將 0 做 type conversion 轉成 ```void(*)()```,指向一個回傳 void 的function 的 functional pointer。 * **Step 2 :** 因此我們以 fp 來表示 ```(void(*)())0``` (一個 functional pointer)因此可將 ```(*(void(*)())0)()``` 改寫成```(*fp)()``` 也就是呼叫 fp 所指向的函式。 ```clike typedef void (*funcptr)(); (* (funcptr) 0)(); ``` * 上式可以直觀看出和 ```(*(void(*)())0)();``` 等效。 ## How to read this prototype? [Reference]() ```clike void ( *signal(int sig, void (*handler)(int)) ) (int); ``` * signal 為一個回傳 functional pointer 的 function,而 signal 這個 function 需要兩個參數。 * ```int sig``` * ```void (*handler)(int)``` : this function pointer takes an int and returns void * signal 指向的 function 需要一個參數 int 且 return void。 # Incomplete Type [Reference](https://msdn.microsoft.com/en-us/library/200xfxh6.aspx) * 定義:宣告一個資料型態但沒有足夠資訊得知該型態之 size。 * A structure type whose members you have not yet specified. * A union type whose members you have not yet specified. * An array type whose dimension you have not yet specified. * 因此宣告 ```struct GraphicsObject;``` 後,使用 ```struct GraphicsObject x;``` 會出現 ```error : storage size of x isn't known``` 的錯誤訊息。而 ```struct GraphicsObject* x;``` 則不會報錯。 ### (練習題) 設定絕對地址為 0x67a9 的 32-bit 整數變數的值為 0xaa6,該如何寫? ```clike *(int32_t * const) (0x67a9) = 0xaa6; /* Lvalue */ ``` * 先將 ```(0x67a9)``` 轉成 pointer type,因此用 ```(int32_t * const)``` 把 (0x67a9) 轉為一個指向 32 位元 integer 的 pointer 而該 pointer 所指向的 address 不變(const),但 address 所存之資料可以改變。 # void* 之謎
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up