## 面試時間:2024/11 ### 1.Struct, Union and Enum ``` typedef unsigned char mUINT_8; typedef unsigned short mUINT_16; typedef unsigned int mUINT_32; typedef struct { mUINT_8 bA; mUINT_8 bB; mUINT_8 bC; mUINT_8 bD; } testStruct; typedef union { mUINT_32 dwAll; testStruct stPart; } testUnion; enum { LOW = 0x10, MID, HIGH }; int main () { //Test struct, union and enum testUnion stTemp; testUnion *pTemp; // *** pTemp = &stTemp; pTemp->stPart.bD = HIGH; } ``` A.請問上述testStruct, testUion和enum各需要佔用幾個Bytes? * Ans: ``` testStruct: 4, testUion: 4, enum: 4 ``` B.請寫一行程式印出stTemp的值,以及執行結果可能為何? * Ans: ``` printf("stTemp.dwAll = %x\n", stTemp.dwAll); ``` >stTemp.dwAll = 12000000 C. ``` // Test struct ary initialize mUINT_32 dwIdx=0; mUINT_16 *bPtr; testStruct structAry[]= { {1,2,3}, {4,5,6} }; bPtr = (mUINT_16 *)structAry; for(dwIdx=0; dwIdx < 8; dwIdx++) { printf("(struct.i%d) = %0x%X\n", dwIdx, *(bPtr+dwIdx)) } ``` 假設下表是structAry,表的最左側是記憶體起始位置,左側為低位,每格表示1 Byte。請利用程式的宣告及下表的列印資訊,把記憶體的值填入並將下面列印的值補齊(i0~i3)。(testStruct結構宣告在題目1) |1|2| | | | | | |0x89|0x25|0x40|0x00|0|0|0|0| |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| (structAry.i0)=0x (structAry.i1)=0x (structAry.i2)=0x (structAry.i3)=0x (structAry.i4)=0x2589 (structAry.i5)=0x40 (structAry.i6)=0x0 (structAry.i7)=0x0 * Ans: ``` (structAry.i0) = 0x0201 (structAry.i1) = 0x0003 (structAry.i2) = 0x0504 (structAry.i3) = 0x0006 (structAry.i4) = 0x2589 (structAry.i5) = 0x0040 (structAry.i6) = 0x0000 (structAry.i7) = 0x0000 ``` ### 2. Queue and Stack: A.簡述兩者的差別 * Ans: 1. Queue (佇列): * 遵循先進先出(FIFO)原則。 * 插入操作在尾端,移除操作在頭端。 2. Stack (堆疊): * 遵循後進先出(LIFO)原則。 * 插入和移除操作都發生在頂端。 B.請在下圖中簡述各個區塊利用方式(eg. Code, Global/ Local/ static variables..) |Stack| |-| | | |Heap| |BSS| |Data| |Text| * Ans: > Text 儲存程式的可執行指令(機器碼)。 > Data 儲存已初始化的全域變數和靜態變數。 > BSS 儲存未初始化的全域變數和靜態變數(記憶體初始化為 0)。 > Heap 動態記憶體分配區(例如 malloc、free)。 > Stack 儲存函數呼叫堆疊,包括區域變數、參數和返回地址。 C.簡述Static的作用 * Ans: >靜態變數(static)在編譯期間分配記憶體。 >* 作用範圍: >全域靜態變數: 作用於當前檔案,不會暴露給其他檔案。 >區域靜態變數: 作用於函數內,但其值在多次函數呼叫之間保持不變。 ### 3.Call by value 與 call by address A.請簡述兩者差別 * Ans: > 1. Call by Value(值傳遞): > 傳遞參數的副本,函數內對變數的修改不會影響到原變數。 > 2. Call by Address(地址傳遞): > 傳遞變數的地址,函數內對地址所指向變數的修改會影響到原變數。 B.請根據下列程式碼,寫出列印的結果 ``` typedef struct{ int total; int index; int nextIndex; }FormInfo; void FormUpdate(FormInfo *TempForm, int amount) { amount = amount + 100; TempForm->total = amount; } int main() { FormInfo CurForm; int base_count = 9; CurForm.total = 0; CurForm.index = 0; FormUpdate(&CurForm, base_count); printf("%d\n", CurForm.total); printf("%d\n", base_count); } ``` * Ans: ``` 109 9 ``` ### 4.請簡述何謂Finite-state machine(FSM)? * Ans: > FSM 是一種計算模型,由有限數量的狀態、轉換規則和事件組成。 > * 一個 FSM 包含: > 1. 狀態集(States):描述系統的所有可能狀態。 > 2. 轉換(Transitions):由觸發條件引起狀態改變。 > 3. 初始狀態(Initial State):開始時的狀態。 > 4. 事件(Events):驅動狀態轉換。 ### 5.使用volatile宣告的variable特性? * Ans: > 告訴編譯器變數的值可能會被異步更新(如中斷或硬體)。 > 編譯器不應對該變數進行優化,保證每次訪問都直接讀取記憶體中的最新值。 ### 6.Linked-List(程式題C/C++) A.請宣告一個struct node供linked list使用須包含以下element: 1. prev(記錄前一node) 2. nxt(記錄下一node) 3. int data 4. int num(記錄list中第幾號node,start from 0) B.將一維陣列{5,9,10,2,4,3,7}依序放入struct data中並串為list。(需有head, tail兩pointer) * Ans: ``` typedef struct node { struct node *prev; struct node *nxt; int data; int num; } Node; #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *prev; struct node *nxt; int data; int num; } Node; int main() { int arr[] = {5, 9, 10, 2, 4, 3, 7}; int size = sizeof(arr) / sizeof(arr[0]); Node *head = NULL, *tail = NULL, *temp = NULL; for (int i = 0; i < size; i++) { temp = (Node *)malloc(sizeof(Node)); temp->data = arr[i]; temp->num = i; temp->nxt = NULL; temp->prev = tail; if (tail) { tail->nxt = temp; } else { head = temp; } tail = temp; } // Print Linked List Node *current = head; while (current) { printf("Node %d: Data = %d\n", current->num, current->data); current = current->nxt; } return 0; } ```