contributed by < ignite1771
>
queue.h
queue.c
TODO: 思考如何讓上方程式碼更精簡 (注意 free
)
已在 commit 851e85f 改寫。 ignite1771
自第 10, 15 行起可改寫,降低縮排深度
已在 commit 04e0d96 改寫。 ignite1771
sp[bufsize - 1] = '\0';
來確保 sp 儲存之字串有 terminating charater發問
if (sp != NULL)
改寫成 if (!sp)
?
if (sp != NULL)
等價於 if (sp)
,而非 if (!sp)
,兩者意思相反了解,在修改程式碼讓其變簡潔時,我會更注意保持邏輯清晰 ignite1771
考慮以下程式變更:
要點:
likely
和 unlikely
巨集,程式碼會更清晰已在 commit 04e0d96 改寫。 ignite1771
這個 MIN 巨集存在缺陷:
MIN(++i, i++)
,依據上述巨集的定義方式,就要考慮到 side effect;因此,在 gcc 中,會這樣定義:
請討論背後的思考議題
Natural Sort
將 strnatcmp.h
及 strnatcmp.c
加入 lab0-c 目錄,並修改 Makefile
OBJS
merge 函式 iterative 版本 + pointer to pointer
merge_sort_list 函式則還是對照 merge 函式 recursive 版本 實作
merge
和 mergeSortList
這兩個函式,其作用為 helper (內部使用),應該在宣告加上 static
,將 visibility 設定為不公開,有利於編譯器之後施加最佳化,也可避免符號 (symbol) 的衝突;merge
程式碼尚可精簡;strcpy
函數時出現警告
The
strcpy
built-in function does not check buffer lengths and may very well overwrite memory zone contiguous to the intended destination. In fact, the whole family of functions is similarly vulnerable:strcpy
,strcat
andstrcmp
.
strncpy
函數使用情境: 當一次完成多個 functions 時
git rebase {原始要拆解的 commit 之 id}~
edit
{原始要拆解的 commit 之 id}git reset HEAD~
V
, y
, p
git co -- {filename}
git add
& git commit
git reflog
查詢 {原始要拆解的 commit 之 id}
git reset {原始要拆解的 commit 之 id}
git reset {拆完之第 1 個 commit 之 id}
git add
& git commit
git rebase --continue
參考: 你所不知道的 C 語言:動態連結器篇 Symbol Visibility 章節
加入 __attribute__((visibility("hidden")))
描述至 merge
, merge_sort_list
2 個函式中:
或是,參考 visibility 中提到
To aid you converting old code to use the new system, GCC now supports also a
#pragma GCC visibility command
:
…
#pragma GCC visibility
is stronger than-fvisibility
; it affects extern declarations as well. -fvisibility only affects definitions, so that existing code can be recompiled with minimal changes. This is more true for C than C++; C++ interfaces tend use classes, which are affected by-fvisibility
.
#pragma GCC visibility push(hidden)
與 #pragma GCC visibility pop
包含住 merge
, merge_sort_list
2 個函式中:觀察 dynamic symobol table:
merge
, merge_sort_list
函式的 Vis
(Visability) 變成 HIDDEN
Bind
依然是 GLOBAL
static
描述至 merge
, merge_sort_list
2 個函式中:觀察 dynamic symobol table:
merge
, merge_sort_list
函式的 Bind
(Visability) 變成 LOCAL
Vis
依然是 DEFAULT
發問
Bind
=LOCAL, Vis
=DEFAULTBind
=GLOBAL, Vis
=HIDDENBind
=LOCAL, Vis
=HIDDEN 嗎?"local binding is the opposite and keeps the symbol local only (static) and weak is like global, but suggests that the symbol can be overridden."
交叉對照 你所不知道的 C 語言:動態連結器篇
同時加入:
__attribute__((visibility("hidden")))
或 #pragma GCC visibility
static
描述至 merge
, merge_sort_list
2 個函式中,例如:
static
已標示該函式僅限此 compilation unit 可見