109 年 國立中正大學資訊工程學系碩士班乙組《計算機概論》
第 1 題12 分
Assume that the following program is executed on a little-endian machine. Write down the output of the following C program.
#include <stdio.h>
#define LOC(row, col) row*3+col
int main(void)
{
int A[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int *ptr1;
char *ptr2;
ptr1 = (int*) A;
printf("%d\n", *ptr1);
printf("%d\n", *(ptr1+LOC(1,1)));
printf("%d\n", ptr1[LOC(2,1)]);
printf("%d\n", ptr1[5]/5);
ptr2 = (char*) A;
printf("%d\n", ptr2[0]);
printf("%d\n", ptr2[1]);
return 0;
}
登入後即可作答並保存紀錄。
本題測驗 C 語言的巨集展開、二維陣列在記憶體中的列優先(row-major)配置、指標算術,以及 little-endian 架構下的位元組序。
二維陣列 int A[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; 在記憶體中依列優先順序連續存放,依序為 1, 2, 3, 4, 5, 6, 7, 8, 9。
-
ptr1 = (int*) A;
將A的起始位址轉型為int *,使ptr1指向A[0][0](即連續記憶體中的第 0 個整數)。 -
printf("%d\n", *ptr1);
*ptr1即ptr1[0],對應A[0][0],輸出1。 -
printf("%d\n", *(ptr1+LOC(1,1)));
巨集LOC(1,1)展開為1*3+1。運算式ptr1 + 1*3 + 1因乘法優先權高於加法,等同於ptr1 + 3 + 1,即ptr1 + 4。*(ptr1 + 4)對應A[1][1],輸出5。 -
printf("%d\n", ptr1[LOC(2,1)]);
巨集LOC(2,1)展開為2*3+1 = 7。ptr1[7]對應A[2][1],輸出8。
第 2 題8 分
#include <stdio.h>
void func(int **a, int **b)
{
int *temp;
temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int **x1, y1, z1, **x2, y2, z2;
z1 = 10, z2 = 20;
x1 = &y1;
y1 = &z1;
x2 = &y2;
y2 = &z2;
printf("%d %d %d %d\n", *y1, *y2, **x1, **x2);
func(x1,x2);
printf("%d %d %d %d\n", *y1, y2, **x1, **x2);
return 0;
}
Write down the output of the following C program.
登入後即可作答並保存紀錄。
核心觀念
這題考察 C 語言指標與雙重指標,以及函式透過指標修改呼叫端變數的方式。
int *p表示p是「指向int的指標」。int **q表示q是「指向int *的指標」。*q會取出q指向的指標;**q再取出該指標所指向的整數。- 函式參數若是
int **,函式內可透過*a修改呼叫端的int *變數。
解題方法
題目程式有兩處型別錯誤,因此照原文無法產生有效輸出:
- 宣告
int **x1, y1, z1, **x2, y2, z2;中,只有x1和x2是int **;y1、z1、y2、z2都是int。因此y1 = &z1不符合型別,後續*y1也無法對整數解參照。 - 即使將
y1、y2改宣告為int *,第二個printf仍以%d輸出指標y2,格式指定與引數型別不符,行為未定義。
依程式其餘內容推定,原意是將 y1、y2 宣告為 int *,並在第二個 printf 使用 *y2。以下依此修正推導。
初始狀態:
| 變數 | 指向/內容 |
|---|---|
z1 | 10 |
z2 | 20 |
y1 | 指向 z1 |
y2 | 指向 z2 |
x1 | 指向 y1 |
x2 | 指向 y2 |
第一個 printf:
*y1是z1的值,即10。*y2是z2的值,即20。**x1是x1經過y1指向z1後的值,即10。**x2是x2經過y2指向z2後的值,即20。
所以第一行輸出為:
10 20 10 20
第 3 題30 分
A memory leak is a dynamically allocated-memory object that is not deallocated properly and cannot be accessed again. A double-free error happens when a deallocated-memory object is freed again. These issues often occur in programming languages like C and C++.
(a) (21%) There are three code fragments in (1), (2), and (3). L1~L17 are line numbers. Do the code fragments (1), (2), and (3) exist memory-leak or double-free problems at run-time? If yes, please move the existed free() statement or insert a free() statement to correct the problems. The modified programs do not have any memory-leak or double-free errors.
(Answer sample: insert a statement "free (...)" immediately after L1)
Code Fragment (1):
L1 #include <stdio.h>
L2 #include <stdlib.h>
L3
L4 int main(void)
L5 {
L6 int *ptr;
L7
L8 ptr = (int*) malloc(10);
L9 *ptr = 2020;
L10 printf("%d\n", *ptr);
L11 ptr = (int*) malloc(20);
L12 ptr[0] = 10;
L13 ptr[1] = 20;
L14 printf("%d %d\n", *ptr, ptr[1]);
L15 free(ptr);
L16 return 0;
L17 }
Code Fragment (2):
L1 #include <stdio.h>
L2 #include <stdlib.h>
L3 int main(void)
L4 {
L5 int *ptr;
L6 int i;
L7
L8 for (i=0; i<100; i++) {
L9 ptr = (int*) malloc(10);
L10 *ptr = i+1;
L11 printf("%d\n", *ptr);
L12 }
L13 free(ptr);
L14
L15 return 0;
L16 }
L17
Code Fragment (3):
L1 #include <stdlib.h>
L2 int main(void)
L3 {
L4 int *ptr;
L5 int i;
L6
L7 for (i=0; i<100; i++) {
L8 ptr = (int*) malloc(10);
L9 *ptr = i+1;
L10 if (i%2 == 0) {
L11 *ptr ++;
L12 free(ptr);
L13 }
L14 }
L15 return 0;
L16 }
L17
(b) (9%) Java programming language provides a garbage collection technique to avoid memory-leak errors. What is garbage collection?
登入後即可作答並保存紀錄。
(a) 動態記憶體錯誤分析與修正
判斷原則:每一次 malloc() 配置的記憶體區塊,都必須在指標遺失前,以原本回傳的起始位址呼叫恰好一次 free() 釋放;未釋放或遺失指標會造成 memory leak,對同一區塊重複呼叫 free() 則為 double-free。
Code Fragment (1)
- 問題判斷:有 memory leak,無 double-free。
- 原因分析:
L8配置 10 bytes 記憶體並由ptr指向;L10輸出後並未釋放。L11直接將新的malloc(20)位址覆寫給ptr,導致L8配置的區塊失去唯一指標而無法釋放,造成 memory leak。L15的free(ptr);僅釋放L11配置的區塊。
- 修正方式:
在L10後(L11重新指派ptr前)插入釋放敘述:Insert a statement
free(ptr);immediately after L10.
Code Fragment (2)
- 問題判斷:有 memory leak,無 double-free。
- 原因分析:
for迴圈共執行 次,每次L9都會配置新的 10 bytes 區塊並覆寫ptr。- 迴圈中沒有
free(ptr);,導致前 次配置(i = 0至i = 98)的位址全數遺失,造成 memory leak。 - 迴圈結束後的
L13free(ptr);僅能釋放最後一次(i = 99)配置的區塊。
- 修正方式:
將L13的free(ptr);移至L11後(即每次迴圈使用完畢立即釋放;注意不可保留L13,否則最後一筆會變成 double-free):Move
free(ptr);from L13 to immediately after L11.
Code Fragment (3)
- 問題判斷:有 memory leak 與無效位址釋放(invalid free),無 double-free。
- 原因分析:
- 奇數次迴圈造成 memory leak:
free(ptr);(L12)位於if (i%2 == 0)條件式內,當i為奇數(i = 1, 3, ..., 99)時不會執行free(ptr);,共 個配置區塊未釋放而發生 memory leak。 - 偶數次迴圈因
*ptr ++;造成無效位址釋放:在 C 語言中,後置遞增++的優先順序高於間接運算子*,因此L11的*ptr ++;等價於*(ptr++);,會將指標ptr本身向後移動sizeof(int)bytes。接著L12呼叫free(ptr);時,傳入的已非malloc()回傳的原始位址,屬於未定義行為(invalid free),且原配置區塊未能正確釋放。
- 奇數次迴圈造成 memory leak:
- 修正方式:
第 4 題10 分
Please answer the following questions for the two binary trees in the following figure.
🖼️【此處有附圖,請對照原卷】
(a) For each tree in the following figure, list the leaf nodes and the non-leaf nodes.
(b) Write out the inorder, preorder, and postorder traversals for each tree in the following figure.
登入後即可作答並保存紀錄。
核心觀念
本題評量資料結構中二元樹(Binary Tree)的基本組成要素與三種深先搜尋走訪(Depth-First Traversals):
- 葉節點(Leaf Node / Terminal Node):分支度(Degree)為 的節點,亦即沒有任何子節點的節點。
- 非葉節點(Non-leaf Node / Internal Node):分支度大於 的節點,在二元樹中代表具有 個或 個子節點的節點。
- 二元樹走訪(Tree Traversals):
- 前序走訪(Preorder Traversal):順序為「根節點 左子樹 右子樹」(V-L-R)。
- 中序走訪(Inorder Traversal):順序為「左子樹 根節點 右子樹」(L-V-R)。
- 後序走訪(Postorder Traversal):順序為「左子樹 右子樹 根節點」(L-R-V)。
解題方法
由原卷附圖讀取兩棵二元樹的結構如下:
- 左圖(Tree 1):為一棵嚴格的左偏斜二元樹(Left-skewed binary tree),共包含 5 個節點( 至 ),樹根為 。 的左子節點為 、 的左子節點為 、 的左子節點為 、 的左子節點為 ;所有節點皆無右子節點。
- 右圖(Tree 2):共包含 9 個節點( 至 ),樹根為 。
- 的左子節點為 、右子節點為 。
- 的左子節點為 、右子節點為 。
- 的左子節點為 、右子節點為 。
- 的左子節點為 、右子節點為 。
- 節點 均無子節點。
(a) 判斷各樹的葉節點與非葉節點
-
左圖(Tree 1):
- 葉節點(Leaf nodes):僅節點 的分支度為 (無子節點)。
- 非葉節點(Non-leaf nodes):節點 的分支度皆為 。
-
右圖(Tree 2):
- 葉節點(Leaf nodes):節點 的分支度皆為 。
- 非葉節點(Non-leaf nodes):
- 分支度為 的節點:。
(b) 推導各樹的走訪序列
-
左圖(Tree 1)的走訪推導:
- 前序(Preorder, V-L-R):
先訪問樹根 ,遞迴走訪左子樹(依序訪問 ),均無右子樹。
- 中序(Inorder, L-V-R):
一路往左走到最底部的葉節點 先輸出,再依序返回訪問各父節點 。
- 後序(Postorder, L-R-V):
各子樹皆先走訪左子樹,右子樹為空,最後訪問根節點。由底層逐層向上訪問:。
- 前序(Preorder, V-L-R):
-
右圖(Tree 2)的走訪推導:
-
前序(Preorder, V-L-R):
- 訪問樹根 。
- 走訪左子樹(根為 ):
- 訪問 。
- 走訪 的左子樹(根為 ):訪問 訪問左子節點 訪問右子節點 。
- 走訪 的右子節點:訪問 。
- 左子樹前序序列為:。
- 走訪右子樹(根為 ):
- 訪問 訪問左子節點 訪問右子節點 。
- 右子樹前序序列為:。
- 結合得前序走訪:。
-
中序(Inorder, L-V-R):
- 走訪左子樹(根為 ):
- 的子樹中序:左子節點 根 右子節點 (即 )。
- 訪問根 。
- 訪問右子節點 。
- 左子樹中序序列為:。
- 訪問樹根 。
- 走訪右子樹(根為 ):
- 走訪左子樹(根為 ):
-
第 5 題20 分
Carefully examine the following twenty sentences and give your answer, true (T) or false (F), for each sentence. Two points are given for each correct answer, whereas three points are deducted for each incorrect answer. Therefore, please do not guess at the answer if you are unsure since the expected value is negative. Note that the deduction for incorrect responses only affects the 20 points of this question.
(a) The fields of a user-defined data type "union" in C share their memory space.
(b) The maximum number of nodes in a binary tree of depth 10 is 1024.
(c) We can invert a linked list in place (i.e., no need to use additional memory with a size proportional to the elements in the linked list).
(d) A heap (i.e., priority queue) is better than a binary search tree when we examine whether an element is stored in the data set.
(e) Dijkstra algorithm cannot deal with the graph with negative edge weights.
(f) In most cases, prime numbers are good divisors for implementing a division-based hash function.
(g) A balanced binary search tree is always the best binary search tree for storing the records even if the identifiers are with different searching probabilities.
(h) Quick sort is faster than insertion sort even if the input size is small.
(i) A stack is a first-in-first-out data structure.
(j) A user-defined "struct" in C creates a data type that can be used to group items of possibly different types into a single type.
登入後即可作答並保存紀錄。
本題為是非題(True / False),共 10 小題,每題答對得 2 分、答錯倒扣 3 分。各小題解析如下:
(a) T
在 C 語言中,union(共用體)的所有成員共享同一塊記憶體空間,該空間大小至少等於最大成員所需的大小,同一時間僅能有效儲存其中一個成員的值。
(b) F
無論二元樹的深度從 或從 開始計算,深度為 的二元樹最多可容納的節點數分別為 (樹高 、共 層時為 )或 (共 層時為 )。最大節點數必為奇數,不可能是偶數 ( 僅是第 層單層的最大節點數)。
(c) T
反轉鏈結串列可透過迭代法,僅使用三個指標變數(如 prev、curr、next)逐一修改節點的指標方向來完成,額外記憶體空間複雜度為 ,不需要與節點數量成正比的額外空間(註:若改用遞迴法則需 函式呼叫堆疊空間,非 in-place)。
(d) F
堆積(Heap / 優先佇列)僅能保證在 時間內取得極值(最大或最小值),若要搜尋任意元素是否存在於資料集中,必須線性掃描,時間複雜度為 ;相對地,二元搜尋樹(BST)的平均搜尋時間複雜度為 (平衡 BST 最壞亦為 ),因此檢查元素是否存在時 BST 優於 Heap。
(e) T
Dijkstra 演算法採用貪婪策略,假設所有邊的權重皆為非負數,才能保證每次從尚未確定的頂點中選出距離起點最近者時,該距離已是最短路徑長度。若圖中存在負權重邊,該假設不成立,可能導致錯誤結果(此時應改用 Bellman-Ford 演算法)。
(f) T
在除法雜湊函數 中,若 與鍵值分布存在共同因數(例如 為 的冪次或合數),容易使鍵值集中於特定雜湊值而增加碰撞。選取質數作為除數 可減少鍵值規律帶來的碰撞,使雜湊結果分布較為均勻。
(g) F
平衡二元搜尋樹(如 AVL 樹、紅黑樹)能保證最壞情況搜尋時間為 ,但這是針對所有鍵值搜尋機率相近的情況。
第 6 題20 分
Consider the following graph. Please write down the sequences of edges selected by the following two algorithms, respectively.
🖼️【此處有附圖,請對照原卷】
(a) (10%) Kruskal's algorithm.
(b) (10%) Prim's algorithm (start from vertex a).
登入後即可作答並保存紀錄。
核心觀念
最小生成樹是在連通且無向的加權圖中,選出連接所有頂點、沒有環且總權重最小的邊集合。Kruskal 演算法依邊權重由小到大考慮,只有在加入邊不會形成環時才選取;Prim 演算法則從指定頂點開始,每次選擇一條連接「已納入頂點」與「未納入頂點」且權重最小的邊。
解題方法
原圖有頂點 ,邊及權重為:、、、、、、、、、、、、、。下方分別依權重排序執行 Kruskal,以及從 開始逐步擴張執行 Prim。
(a) Kruskal 演算法
依權重由小到大考慮邊;權重相同時,依序處理不影響本題的最小生成樹結果。
- 選 。
- 選 。
- 選 ,連接兩個不同的連通分量。
- 選 。
- 選 ,連接目前不同的連通分量。
- 權重為 的 會形成環,略過;選 。
- 權重為 的 會形成環,略過;選 。
- 選 ,將 連到其他頂點。
原圖有 個頂點,因此最小生成樹需有 條邊;此時已選滿 8 條。
(b) Prim 演算法,從 開始
每一步只在連接已納入頂點與未納入頂點的邊中,選權重最小者: