# Study - 你所不知道的C語言 ###### tags: `C語言` ### 1. [指標篇](https://hackmd.io/@sysprog/c-pointer) - &a => address of a - *ptr => dereference ptr等於value of ptr - [ ] array[ ] => it's an array subscripting - In C11 chap 6.5.2.1, E1[E2] is identical to (*((E1)+(E2))). - sizeof()是一個operator - Incomplete type - Ex. char x[]; - 沒辦法建立instance(object);但可以建立a pointer to incomplete type - [ ] void 算是incomplete type嗎? - Ex. void *ptr => ptr is a pointer to a void ```! Types are partitioned into object types (types that describe objects) and function types (types that describe functions). At various points within a translation unit an object type may be incomplete (lacking sufficient information to determine the size of objects of that type) or complete (having sufficient information) ``` - [ ] Lvalue (Locator value)? ``` C *(int32_t * const) (0x67a9) = 0xaa6; /* Lvalue */ ``` - function call傳入的參數是個複本,input argument只存在function內 - Ex. void func(int *p) { p = &B; } v.s. void func(int **p) { *p = &B; } - 所以若會對input argument做assign動作,就會需要 pointer to pointer - Linked List Remove head - 若不用使用pointer to pointer,解法: - Input p_head; Return Updated p_head - 若使用pointer to pointer,解法 - Input pp_head; Return don't care - 可以達到ADT(Abstract Data Type),不用aware - [ ] 是否有***這種用法呢? - [ ] Forward declaration? - Array v.s. Pointer - char x[10] => 不能變更為 pointer 的形式,因為pointer是描述一個start address,但沒有指定size。 - [ ] 為什麼GDB可以修改記憶體? ``` C= (gdb) p (&b[0])->v = {1, 2, 3} $12 = {1, 2, 3} (gdb) p b[0] $13 = { v = {1, 2, 3}, length = 0 } ``` - [x] LLP64 data model - ==L==ong==L==ong - ==P==ointer - [ ] 為什麼不用帶入argc就可以執行? - ![](https://i.imgur.com/hWkd6n6.png) - [ ] C 語言規格中定義 string literals 會被分配於 “static storage” 當中 (C99 [6.4.5]),並說明如果程式嘗試修改 string literals 的內容,將會造成未定義行為 - 以 gcc 的 ELF target 來說,將 string literals 分配在 read-only data section 中 (當然包含 \0 結尾) - [ ] branch predictor? ### 2. [linked list 和非連續記憶體](https://hackmd.io/@sysprog/c-linked-list) - linked list適用在分散式系統(資料龐大, RTOS排成器...) - 改linked list的p_head -> 這是個常數時間 - [ ] Locality of reference (cache 用語)? - [ ] Merge Sort - [ ] [cache line](https://hackmd.io/@sysprog/HkW3Dr1Rb?type=view) 介紹 ### 3. [函式呼叫](https://hackmd.io/@sysprog/c-function) - 在 C 語言中,“function” 其實是特化的形式,並非數學意義上的函數,而隱含一個狀態到另一個狀態的關聯,因此,我們將一般的 C function 翻譯為「函式」,以區別數學意義上的函數 (如 abs, cos, exp) - ABI(application binary interface) - [ ] Calling convention - [ ] Forward declaration - forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition. - C preprocessor 以獨立程式的形式存在 > As the C preprocessor can be invoked separately from the compiler with which it is supplied, it can be used separately, on different languages. - [const pointer](https://stackoverflow.com/questions/8808167/c-const-correctness-and-pointer-arguments) --- ### Reference link: - [C99 standard](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) - [C11 standard](https://www.open-std.org/jtc1/sc22/WG14/www/docs/n1570.pdf) - [GNU debugger GDB](http://sourceware.org/gdb/current/onlinedocs/gdb/) ---