# 2024q1 Homework1 (lab0) contributed by < `Henryshsieh` > :::danger 避免非必要的項目縮排 (即 `* ` 和 `- `),以清晰、明確,且流暢的漢語書寫。 ::: ## 指定佇列的操作 ### `q_new` - 使用 list.h 中提供的巨集和函式 - `LIST_HEAD(tmp); return &tmp;` 是錯誤:returns address of local variable。注意 [scope](https://stackoverflow.com/a/12380788) - `q_insert_head()` 與 `q_insert_tail()` - 記得為字串動態配置記憶體空間 - 若記憶體配置失敗而沒有釋放,會因為 memory leak 無法通過靜態檢查。但我還沒想通為何這沒有在所有動態記憶體配置的場合發生 ```c bool q_insert_head(struct list_head *head, char *s) { element_t *node = malloc(sizeof(element_t)); if (node == NULL) return false; // 不需要 free(node)? int arrlen = strlen(s) + 1; node->value = malloc(sizeof(char) * arrlen); if (node->value == NULL) { free(node->value); free(node); // 必須釋放 return false; } strncpy(node->value, s, arrlen); list_add(&node->list, head); return true; } ``` - [你所不知道的 C 語言: linked list 和非連續記憶體](https://hackmd.io/@sysprog/c-linked-list#Linux-%E6%A0%B8%E5%BF%83%E9%A2%A8%E6%A0%BC-Linked-List-%E6%87%89%E7%94%A8%E6%A1%88%E4%BE%8B), [Linux 核心原始程式碼巨集: container_of](https://hackmd.io/@sysprog/linux-macro-containerof) - `element_t *node = list_entry(ptr_to_list_head, element_t, list);` - [你所不知道的C語言:指標篇#Address and indirection operators](https://hackmd.io/@sysprog/c-pointer#Address-and-indirection-operators) - [作業系統術語及概念](https://hackmd.io/@sysprog/linux-concepts), [不僅是個執行單元的 Process](https://hackmd.io/@sysprog/linux-process#Linux-%E8%A8%AD%E8%A8%88%E7%9A%84-trade-off-%E5%92%8C-evolution): clone, [UNIX 作業系統 fork/exec 系統呼叫的前世今生](https://hackmd.io/@sysprog/unix-fork-exec#Linux-%E5%B0%8D%E6%96%BC-fork-%E5%AF%A6%E4%BD%9C%E7%9A%84%E6%89%8B%E6%B3%95)
×
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