Chapter 5 pointers and arrays Speaker: Lung-Sheng Chien.

Slides:



Advertisements
Similar presentations
Chapter Programming in C
Advertisements

指標 Pointers.
Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
6-1 指標簡介 6-2 指標與陣列 6-3 動態配置記憶體 6-4 本章綜合練習
布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
Introduction to Java Programming Lecture 13 Classes I OO Programming.
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
: A-Sequence 星級 : ★★☆☆☆ 題組: Online-judge.uva.es PROBLEM SET Volume CIX 題號: Problem D : A-Sequence 解題者:薛祖淵 解題日期: 2006 年 2 月 21 日 題意:一開始先輸入一個.
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章.
T-SQL 運算子介紹 11/29. 運算子的總類 指定運算子 算術運算子 比較運算子 邏輯運算子 位元運算子 字串連結運算子 單一運算元運算子.
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
1 第一章 Word 的基本觀念 內容概要: Word 的特色 啟動與離開 Word 的方法 滑鼠游標與外型的介紹 基本操作 Word 視窗法則 使用 Word 遭遇問題時, 應如何利用軟體特 性而獲得輔助解說.
1 Web of Science 利用指引 單元二 瀏覽與處理查詢結果. 2 瀏覽檢索結果 查出的結果,預設以時間排列, 使用者可改變結果的排列方式: 還可以依被引用次數、相關度、 第一作者、刊名、出版年等排序 回到前先查的結果畫面 點選想看資料的完整書目 本館訂購範圍的期刊 全文,便可直接連結.
1 Chapter 11 Verilog 硬體描述語言 Verilog 硬體描述語言的基本架構 Verilog 模組描述的基本格式 如何開啟進入 Verilog 硬體描述語言編輯器 Verilog 的描述格式 Verilog 的資料型態 Verilog 的事件基礎時間控制 Verilog 的輸入輸出埠描述.
: OPENING DOORS ? 題組: Problem Set Archive with Online Judge 題號: 10606: OPENING DOORS 解題者:侯沛彣 解題日期: 2006 年 6 月 11 日 題意: - 某間學校有 N 個學生,每個學生都有自己的衣物櫃.
第一章 變數、常數及資料型態. 變數 C 程式語言的變數名稱 第一個字必須是英文字母或底線 (_) 之後可以是數字, 英文字母或底線 (_) 不可以是保留字 例: Num (Ο) _score (Ο) C&C (X) 8num (X)
MATLAB 程式設計 第 11 章 多維陣列 多維陣列的定義 在 MATLAB 的資料型態中,向量可視為 一維陣列,矩陣可視二維陣列,對於維 度 (Dimensions) 超過 1 的陣列則均可視 為「多維陣列」 (Multidimesional Arrays , 簡稱 N-D Arrays)
第三章 變數與繫結 陳維魁 博士 儒林圖書公司. 2 大綱  變數的定義  變數元件  儲存區配置問題  參考的透明性  完全計算  捷徑計算  繫結 (binding)  繫結時間  精選習題.
程式註解說明. 2 程式註解格式 塊狀註解 對檔案、 class 、 method 、資料結構、一段程式 …. 等程式區塊 做說明。 第一行的開頭必需為 “/*” 且沒有其他文字,最後一行的開頭 必需以 “*/” 做為結束,在中間每一行的開頭都必需是一個 “*” 。 單行註解 佔據一整行的說明。 以.
Chapter 07 低階程式語言.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
第 7 章 程序與函數 7-1 模組化程式設計 7-1 模組化程式設計 7-2 VB.NET 的程序與函數 7-2 VB.NET 的程序與函數 7-3 程序與函數的變數範圍 7-3 程序與函數的變數範圍 7-4 VB.NET 常用的內建函數 7-4 VB.NET 常用的內建函數 7-5 遞迴函數 7-5.
Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.
第 1 章 PC 的基本構造. 本章提要 PC 系統簡介 80x86 系列 CPU 及其暫存器群 記憶體: Memory 80x86 的分節式記憶體管理 80x86 的 I/O 結構 學習組合語言的基本工具.
Introduction to pointers in C/C++. Pointers 特殊變數 存放變數在記憶體中的位址 MinGW C++ 中佔用 4 bytes 間接定址取執法 位址 指標變數 變數內容 變數.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
第九章 結構. 結構的宣告 結構 它是由許多不同 ( 或相同 ) 資料型態的變數所組成的 集合,通常利用結構標籤稱呼此集合 struct student { char *name; int score; }; struct 為保留字,表示結構的宣告開始 結構項目需定義於大括號「 { } 」內 結尾需加上分號.
計算機概論 - 排序 1 排序 (Sorting) 李明山 編撰 ※手動換頁.
最新計算機概論 第 5 章 系統程式. 5-1 系統程式的類型 作業系統 (OS) : 介於電腦硬體與 應用軟體之間的 程式,除了提供 執行應用軟體的 環境,還負責分 配系統資源。
7 陣列與搜尋 7.1 陣列 一般資料變數 宣告一維陣列 起始一維陣列 7-4
Hung-Hsiang WuWindows Processing Design1 Chapter 3 基本觀念 變數宣告與型態 特殊運算子符號 字串與數值的轉換 類別與物件的觀念 建立新的專案 WinMain 程式進入點 Include Header File.
資料庫程式設計與系統管理 SQL Server 2005 Express 第六章 進階資料庫設計.
第十章 再論結構. 指標與結構的關係 /* File name: ex10-1a.c */ #include int main() { struct student { char *name; int score; }; struct student st= {"Brian", 97}; struct.
: Multisets and Sequences ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11023: Multisets and Sequences 解題者:葉貫中 解題日期: 2007 年 4 月 24 日 題意:在這個題目中,我們要定義.
程式設計 Visual Basic 6.0 Visual Basic 6.0 Visual Basic 6.0 程式設計 Visual Basic 6.0 Visual Basic 6.0 Visual Basic 6.0許翠婷
資料結構實習-一 參數傳遞.
第六章 陣列.
第二章第二章 Statistica 的工具欄. 本章目標:工具欄之使用 介紹 STATISTICA 是一個非常好用的統計軟體,只 要你對微軟的 Office 套裝軟體有較熟悉的瞭解, 那對 STATISTICA 的工具欄就非常容易掌握。 STATISTICA 的文件組織有四種類型,即 Workbook.
企二 A 管理程式設計 Visual Basic 6.0 Visual Basic 6.0 Visual Basic 6.0 企二 A 管理程式設計 Visual Basic 6.0 Visual Basic 6.0 Visual Basic 6.0許翠婷
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
4 堆疊與佇列 4.1 前言 四種基本的資料結構 (可儲存資料的容器) 陣列 (Array)、串列(List): 最基本
A[0] a[1] pa ???? *pa ppa *ppa Address:4 byte Double:8 byte.
函式 Function Part.2 東海大學物理系‧資訊教育 施奇廷. 遞迴( Recursion ) 函式可以「呼叫自己」,這種動作稱為 「遞迴」 此程式的執行結果相當於陷入無窮迴圈, 無法停止(只能按 Ctrl-C ) 這給我們一個暗示:函式的遞迴呼叫可以 達到部分迴圈的效果.
資料結構實習-二.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式.
Visual Basic 簡介 Visual Basic 基礎語法 東海大學物理系‧施奇廷 《程式設計》
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
第 17 章 樣板 17-1 樣板的基礎 17-1 樣板的基礎 17-2 樣板函數 17-2 樣板函數 17-3 樣板類別 17-3 樣板類別 17-4 非型態參數的樣板類別 17-4 非型態參數的樣板類別 17-5 樣板類別的繼承 17-5 樣板類別的繼承 17-6 特化樣板與靜態成員 17-6 特化樣板與靜態成員.
資料型態名稱用途儲存空間儲存值範圍 short 短整數儲存較小的 整數 2 bytes-32,768~32,767 Int 整數儲存整數 16 位元 : 2 bytes 32 位元 : 4 bytes 16 位元 : 32,768~32, 位元 : -2,147,483,648 ~ 2,147,483,647.
Chapter 10 m-way 搜尋樹與B-Tree
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
2005/7 Linear system-1 The Linear Equation System and Eliminations.
電腦的基本單位 類比訊號 (analog signal) 指的是連續的訊號 數位訊號 (digital signal) 指的是以預先定義的符號表示不連續 的訊號 one bit 8 bits=one byte 電腦裡的所有資料,包括文 字、數據、影像、音訊、視 訊,都是用二進位來表示的。
Miscellanea Switch Bitwise operations ?,. break; continue; (example) (example) do { a = getc(stdin); if (a=='q') break; else if (a=='c') continue; n++;
Visual C++重點複習.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
:Problem E.Stone Game ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10165: Problem E.Stone Game 解題者:李濟宇 解題日期: 2006 年 3 月 26 日 題意: Jack 與 Jim.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
第 1 章 PC 的基本構造. 本章提要 PC 系統簡介 80x86 系列 CPU 及其暫存器群 記憶體: Memory 80x86 的分節式記憶體管理 80x86 的 I/O 結構 學習組合語言的基本工具.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
chap7 Chapter 7 Simple Data Types chap7 2 Objectives No programming language can predefine all the data types that a programmer may.
Chapter 3 Visual Basic.Net Visual Basic, like most programming languages, uses variables for storing values. Variables have a name (the word you use to.
Data Structures (1st Exam). 1.[5] Suppose there are only two constructors for a class, one that passes in a single integer parameter called amount, for.
1 7.7Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers –Increment/decrement pointer ( ++ or -- ) –Add an integer.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Chapter 7 - Pointers Outline 7.1 Introduction
Presentation transcript:

Chapter 5 pointers and arrays Speaker: Lung-Sheng Chien

OutLine Memory address and pointer Pointer and array Call-by-value Pointer array: pointers to pointers Function pointer Application of pointer

Physical memory Basic unit of memory is byte (8 bits) Each memory unit (byte) needs an unique address to identify it. For 32-bit system, we can address memory from 0 (0x ) to (0xFFFFFFFF), total memory is up to 4GB For 64-bit system, we can address memory from 0 (0x ) to (0xFFFFFFFF- FFFFFFFF), total memory is ∞ unit: kB = 1000 bytes, 1MB = 1000 kB, 1GB = 1000 MB

char 0x x x x x x x x x x A 0x B 0x D 0x E 0x F 0x x x x x A 0x B 0x C Int (4Byte) float 0xFFFF-004C 0xFFFF-004D 0xFFFF-004E 0xFFFF-004F 0xFFFF-FFFF 1-dimensional memory block in 32-bit system 16 進位 : A(10), B(11), C(12), D(13), E(14), F(15) 0xFFFF-0050

宣告 ip 是個整數指標 2. & is called reference operator, 提取 x 的 address 位 址給指標 ip 3. * is called dereference operator, 將指標 ip 所存的 值當作記憶體位址, 則 *ip 即 是變數 x A pointer is a variable that contains the address of a variable What is pointer Question: Since pointer is also a variable, then address = ?, size = ?

result in windows Size of pointer type Question: all four pointer types have size 4 bytes, why? A pointer is a variable that contains the address of a variable 32-bit machine uses 32 bit to address a variable

result in windows 1 0x0012FF7C 2 0x0012FF78 ? ? ? ? ? ? ? ? ? ? ? 0x0012FF74 0x0012FF70 0x0012FF6C 0x0012FF68 0x0012FF64 0x0012FF60 0x0012FF58 0x0012FF54 0x0012FF50 0x0012FF5C 0x0012FF4C 16 進位 : A(10), B(11), C(12), D(13), E(14), F(15) Address of a variable x y z[9] z[8] z[7] z[6] z[5] z[4] z[3] z[2] z[1] z[0] ip addresscontentvariable

watch window address of x value of x address of pointer ip value of ip (address of some varable) : 0xcccccccc means invalid Use debugger to show change of memory [1] 按 F10

value of ip = address of x Use debugger to show change of memory [2] x ip 指標 ip 指向 x 按 F10

Use debugger to show change of memory [3] 因為 *ip 即為 x, 所以 y = *ip 和 y = x 等價, 即 y 被設為 1 按 F10

Use debugger to show change of memory [4] 因為 *ip 即為 x, 所以 *ip = 0 和 x = 0 等價, 即 x 被設為 0 按 F10

Use debugger to show change of memory [5] ip = &z[0] 提取 z[0] 的位址並存入指標 ip 內 z[0] ip 指標 ip 指向 z[0]

OutLine Memory address and pointer Pointer and array Call-by-value Pointer array: pointers to pointers Function pointer Application of pointer

z[0]z[1]z[2]z[3]z[4]z[5]z[6]z[7]z[8]z[9] z : z[0]z[1]z[2]z[3]z[4]z[5]z[6]z[7]z[8]z[9] z : ip ip+1 ip+2 ip=&z[0] z[0]z[1]z[2]z[3]z[4]z[5]z[6]z[7]z[8]z[9] z : ip ip=&z[9] ip-1 ip-2 Pointer and array [1]

Pointer and array [2] ip := z is equivalent to ip := &z[0] since array name z is synonym( 同義字 ) for first element of z, z[0]. In other words, z = &z[0] z[i] = *(z+i), this is default substitution in C-language. In other words, &z[i] = z+i ip+i points to i-th object beyond ip If ip:=z, then ip plays the same role as z, say z[i] = *(ip+i) = *(z+i) = ip[i], or &z[i] = ip+I = z+I = &ip[i] ip++ is equivalent to ip = ip+1, (move pointer ip to next object). However z is name of array, it always points to first element z[0], so z++ is illegal. You may say pointer is a movable array name. ip+1 is an integer, but *(ip+1) is a reference to some memory location, l-value

Why not 0x28=40(decimal)? Casting to pointer pointing to char ip+i = (int)ip + sizeof(int)*i Arithmetic of pointer Integral arithmetic Pointer and array [3]

Pointer and array [4] &ip[-i] is equivalent to ip – i, C can accept array index smaller than zero

Address arithmetic operationDescription p++, p--Increment (decrement) p to point to the next element, it is equivalent to p+=1 (p -= 1) p+i (p-i)Point to i-th element beyond (in front of) p but value of p is fixed p[i]Equivalent to p + i p + n (integer) n must be an integer, its meaning is offset ( 偏移量 ) p - qOffset between pointer p and pointer q p+q, p*q, p/q, p%qinvalid Relational operator of two pointers p, q valid, including p > q, p = q, p <= q mallocDynamic memory allocation freeRelease memory block which is allocated by malloc

Static and dynamic allocation z[10] 是靜態陣列, 放在堆疊 (stack) 上 ip 指向一個動態陣列, 由作業 系統從 heap 中截取 40 bytes stdio.h ip 本身的記憶體位址 ip 所指向的動態陣列位址 C guarantees that zero is never a valid address for data, so return value of zero can be used to signal an abnormal value

void * malloc( size_t size) dynamic allocation, stdlib.h [1] void free( void *p)

To allocate memory without casting causes error of type checking dynamic allocation, stdlib.h [2]

1 0x0012FF7C 2 0x0012FF78 ? ? ? ? ? ? ? ? ? ? 0x00374e08 0x0012FF74 0x0012FF70 0x0012FF6C 0x0012FF68 0x0012FF64 0x0012FF60 0x0012FF58 0x0012FF54 0x0012FF50 0x0012FF5C 0x0012FF4C x y z[9] z[8] z[7] z[6] z[5] z[4] z[3] z[2] z[1] z[0] ip addresscontentvariable ? ? 0x00374e08 0x00374e0C ? 0x00374e10 0x00374e14 ? Heap 內 dynamic allocation [3]

OutLine Memory address and pointer Pointer and array Call-by-value Pointer array: pointers to pointers Function pointer Application of pointer

Question: why don’t x and y swap? Call by value caller callee 1 0x0012FF7C 5 0x0012FF78 x of main y of main 0x0012FF28 0x0012FF24 y of swap x of swap 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable

1 0x0012FF7C 5 0x0012FF78 x of main y of main 0x0012FF28 0x0012FF24 y of swap x of swap 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable 按 F11 進入 swap Call graph trace [1] Use debugger

1 0x0012FF7C 5 0x0012FF78 x of main y of main 5 1 0x0012FF28 0x0012FF24 y of swap x of swap 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable Call graph trace: caller make a data copy to callee [2] 按 F10 Local variables in swap

1 0x0012FF7C 5 0x0012FF78 x of main y of main 5 1 0x0012FF28 0x0012FF24 y of swap x of swap x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable Call graph trace [3] temp has meaningless content 按 F10

1 0x0012FF7C 5 0x0012FF78 x of main y of main 5 1 0x0012FF28 0x0012FF24 y of swap x of swap 1 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable move value of x of swap to temp of swap Call graph trace [4] 按 F10

1 0x0012FF7C 5 0x0012FF78 x of main y of main 5 5 0x0012FF28 0x0012FF24 y of swap x of swap 1 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable 按 F10 move value of y of swap to x of swap Call graph trace [5]

1 0x0012FF7C 5 0x0012FF78 x of main y of main 1 5 0x0012FF28 0x0012FF24 y of swap x of swap 1 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable 按 F10 離開 swap move value of temp of swap to y of swap Call graph trace [6] Only swap x, y in swap, not in main

用 & 提取 x 和 y 的位址 1 0x0012FF7C 5 0x0012FF78 x of main y of main 0x0012FF28 0x0012FF24 y of swap x of swap 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable Call by value – pointer swap x and y indeed

1 0x0012FF7C 5 0x0012FF78 x of main y of main 0x0012ff78 0x0012ff7c 0x0012FF28 0x0012FF24 y of swap x of swap 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable value of object which x points to value of object which y points to 按 F10 兩次 Call graph trace [1]

1 0x0012FF7C 5 0x0012FF78 x of main y of main 0x0012ff78 0x0012ff7c 0x0012FF28 0x0012FF24 y of swap x of swap 1 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable move value of x of main to temp of swap Call graph trace [2] 按 F10

5 0x0012FF7C 5 0x0012FF78 x of main y of main 0x0012ff78 0x0012ff7c 0x0012FF28 0x0012FF24 y of swap x of swap 1 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable Question: how to check content of x in main ? Call graph trace [3] 雙擊滑鼠左鍵

5 0x0012FF7C 5 0x0012FF78 x of main y of main 0x0012ff78 0x0012ff7c 0x0012FF28 0x0012FF24 y of swap x of swap 1 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable x of main, check its address Call graph trace [4] 按 F10

5 0x0012FF7C 1 0x0012FF78 x of main y of main 0x0012ff78 0x0012ff7c 0x0012FF28 0x0012FF24 y of swap x of swap 1 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable 雙擊滑鼠左鍵 move value of temp of swap to y of main Call graph trace [5]

5 0x0012FF7C 1 0x0012FF78 x of main y of main 0x0012ff78 0x0012ff7c 0x0012FF28 0x0012FF24 y of swap x of swap 1 0x0012FF20 0x0012FF1C 0x0012FF18 temp of swap addresscontentvariable y of main, check its address Call graph trace [6]

nowisthetime\0 amessage: nowisthetime\0 pmessage: Character array v.s. character pointer [1] string constant, cannot modified Modifiable character array *pmessage is a pointer, initialized to point to a string constant; the pointer may subsequently be modified to point elsewhere, but the result is undefined uf you try to modify the string contents Compiler determine length of string and then size of amessage

n 0x0012ff70 0x00422ea4 0x0012ff6c amessage pmessage o w i 0x0012ff74 o w i n 0x00422ea4 Character array v.s. character pointer [2]

s, t are regarded as array name, fixed, use index i to sweep entire character string s, t are pointers, movable, s and t sweep entire character string Fixed pointer v.s. movable pointer Question: when s and t move in function strcpy, why array A and B in function main are fixed?

String comparison Lexicographic order of string s and t We say s < t if there exists index k such that s[k] < t[k] in ASCII code sense. A-Z occupies 0x41 ~ 0x5A a-z occupies 0x61 ~ 0x7A

OutLine Memory address and pointer Pointer and array Call-by-value Pointer array: pointers to pointers Function pointer Application of pointer

0x x0012ff7c 0x0012ff78 i name[3] 0x x x c 0x0012ff74 0x0012ff70 0x0012ff6c name[0] address contentvariable name[1] name[2] a r \0 0x e b \0 F 0x a n \0 J 0x l l e I a l g 0x C 0x M 0x o n t m Pointer array: pointers to pointers [1] h \0

Illegal month\0 Jan\0 Feb\0 Mar\0 name: Pointer array: pointers to pointers [2] String starts at 0x c Compiler determines that size of array is 4 Symbolic representaiton

Compiler finds that size of array should be 4 at least, this is conflict to number 3 defined by programmer Pointer array: pointers to pointers [3]

allocate pointer array of 4 elements 2. Macro assert is used as diagnosis, see page 253 in textbook 3. allocate character array, note that we need one more space for \0 4. First, release each string 5. Finally release pointer array Pointer array: pointers to pointers [4]

void assert( int expression) Diagnosis Macro: assert expression Print file name and line number in stderr true false assert is redudant

a.out\0 hello,\0 world\0 \0 argv: Symbolic representaiton argc: argument countargv: argument vector Command-line arguments [1]

0x003720a0 1 0x0012ff8c 0x0012ff88 i 0x0012ff84 0x0012ff80 0x0012ff7c address content argc argv 0x003720a8 0x003720a0 0x x003720a4 0x003720a8 : \ c F 0x003720ac u r s o 0x003720b0 \ e argv[0] argv[1] Command-line arguments [2] 使用 debugger, 只有執行檔, 沒有 引數, 所以 argc = 1

Command-line arguments [3] 0x003720a0 1 0x0012ff8c 0x0012ff88 i 0x0012ff84 0x0012ff80 0x0012ff7c address content argc argv 0x003720a8 0x003720a0 0x x003720a4 0x003720a8 : \ c F 0x003720ac u r s o 0x003720b0 \ e argv[0] argv[1] char** argv 等同於 char* argv[]

0xbffffeb04 3 0xbfffeac4 0xbfffeac0 address content argc argv 0xbfffeefa 0xbfffeb04 0xbfffef02 0xbfffeb08 0xbfffeb0c \0 0xbfffef08 \ a.. 0xbfffeefa u o *argv t \0 0xbfffeefb 0xbfffeefc 0xbfffeefd 0xbfffeefe 0xbfffeeff 0xbfffef00 0xbfffef01 0xbfffef02 e l l h \0 o 0xbfffef02 0xbfffef03 0xbfffef04 0xbfffef05 0xbfffef06 0xbfffef07 0xbfffef08 o r l w \0 d 0xbfffef08 0xbfffef09 0xbfffef0a 0xbfffef0b 0xbfffef0c 0xbfffef0d Command-line arguments [4]

0xbfffef08 0xbffffeb08 3 0xbfffeac4 0xbfffeac0 address content argc argv 0xbfffeefa 0xbfffeb04 0xbfffef02 0xbfffeb08 0xbfffeb0c \0 \ a.. 0xbfffeefa u o *argv t \0 0xbfffeefb 0xbfffeefc 0xbfffeefd 0xbfffeefe 0xbfffeeff 0xbfffef00 0xbfffef01 0xbfffef02 e l l h \0 o 0xbfffef02 0xbfffef03 0xbfffef04 0xbfffef05 0xbfffef06 0xbfffef07 0xbfffef08 o r l w \0 d 0xbfffef08 0xbfffef09 0xbfffef0a 0xbfffef0b 0xbfffef0c 0xbfffef0d Command-line arguments [5] argv++ print string whose address is in *argv char** argv char* (*argv) char (**argv)

0xbffffeb0c 3 0xbfffeac4 0xbfffeac0 address content argc argv 0xbfffeefa 0xbfffeb04 0xbfffef02 0xbfffeb08 0xbfffeb0c \0 0xbfffef08 \ a.. 0xbfffeefa u o *argv t \0 0xbfffeefb 0xbfffeefc 0xbfffeefd 0xbfffeefe 0xbfffeeff 0xbfffef00 0xbfffef01 0xbfffef02 e l l h \0 o 0xbfffef02 0xbfffef03 0xbfffef04 0xbfffef05 0xbfffef06 0xbfffef07 0xbfffef08 o r l w \0 d 0xbfffef08 0xbfffef09 0xbfffef0a 0xbfffef0b 0xbfffef0c 0xbfffef0d Command-line arguments [6] argv++ print string whose address is in *argv

OutLine Memory address and pointer Pointer and array Call-by-value Pointer array: pointers to pointers Function pointer Application of pointer

Function = address [1] Function pointer is pointer which points to address of a function Each function has an address, we can use reference operator & to extract its address, however since function is equal to an address (this is different from variable), sometimes we neglect operator & Question: a function name is equal to an address, why?

Function = address [2] 中斷點 main 和 &main 有相同值, 因為函數名等價於位址 Question: why main = 0x in debugger but main = 0x A in ouput 選擇組合語言表示 View  Debug Window  Disassembly

Function = address [3] 跳到函數 swap: 0x 按 F11 進入 swap

1. 按 F11 跳到 swap Function = address [4] 在位址 0x 內的值表示指令 jmp swap (0x004010b0) 在位址 0x A 內的值表示指令 jmp main (0x )

進入函數 swap 後第一個指令, 其存在位址 0x004010b0 之處, 所以 swap 的位址可為此處 Function = address [5] Question 1: How to define function pointer? Question 2: How to assign function pointer an address of another function?

Function pointer ( 函數指標 ) [1] 宣告函數指標 swap_ptr, 其函 數原型宣告用來作 type checking 2. 將目標函數 swap 的位址存入 指標 swap_ptr 內 3. 用 dereference 算子將 指標 swap_ptr 內的值視為函數位址 函數指標三部曲 括號是必要不可缺

Function pointer ( 函數指標 ) [2] 將函數 strcpy 的位址給 swap_ptr strcpy 的原型為 char* strcpy(char*, const char*) 和 swap_ptr 的原型不合 swap_ptr 的原型

Function pointer ( 函數指標 ) [3] 按 F11 進入 swap

Function pointer ( 函數指標 ) [4] 確實進入 swap 內 Question: declaration void ( *swap_ptr )(int*, int* ) is long and awkward ( 笨拙 ), can we have any other choice?

Function pointer ( 函數指標 ) [5] typedef defines a new type, You can use typedef declarations to construct shorter or more meaningful names for types already defined by C or for types that you have declared High readability ( 可讀性 ) Question: why we need function pointer? any application?

Application of function pointer: bubble sort bubble sort( 氣泡排序法 ) is simplest way for sorting, the basic idea is “push the largest element upward to last location, then recursively do the same thing over remaining unsorted sub- array”. pseudocode

Unsorted array: 4 > 3, swap > 1, swap > 2, swap < 8, no swap > 7, swap > 1, swap > 2, swap < 4, no swap < 7,no swap First pass, k = 5 second pass, k = 4 Process of bubble sort [1]

< 2,no swap < 3,no swap < 4,no swap < 2,no swap < 3,no swap third pass, k = 3 4-th pass, k = < 2,no swap 5-th pass, k = 1 Sorted array: Process of bubble sort [2]

bubble_sort_in.cpp main.cpp bubble sort: integer version Question: Can we sort string?

bubble_sort_string.cppmain.cpp bubble sort: string version x is a pointer pointing to data type char* (string) swap_string only swaps pointers, not string content

0x00422fdc Unsorted array: 0x x x x00422fd40x SeptemberJanMarFebJulyOctober Process of bubble sort : string [1] September > Jan, swap 0x x00422fdc0x x x00422fd40x September Jan MarFebJulyOctober September > Mar, swap 0x x x00422fdc0x x00422fd40x September Jan Mar FebJulyOctober September > Feb, swap 0x x x x00422fdc0x00422fd40x September Jan Mar FebJulyOctober September > July, swap

0x x x x00422fd40x00422fdc0x September Jan Mar FebJulyOctober September > October, swap Process of bubble sort : string [2] 0x x x x00422fd40x x00422fdc September Jan Mar FebJuly October Jan < Mar, no swap 0x x x x00422fd40x x00422fdc September Jan Mar FebJuly October Mar > Feb, swap 0x x x x00422fd40x x00422fdc September Jan MarFebJuly October Mar > July, swap

0x x x00422fd40x x x00422fdc September Jan Mar Feb JulyOctober Mar < October, no swap Process of bubble sort : string [3] 0x x x00422fd40x x x00422fdc September Jan Mar Feb JulyOctober Jan > Feb, swap 0x x x00422fd40x x x00422fdc September Jan Mar Feb JulyOctober Jan < July, no swap 0x x x00422fd40x x x00422fdc September Jan Mar Feb JulyOctober July < Mar, no swap

0x x x00422fd4 0x x x00422fdc September Jan Mar Feb JulyOctober Feb < Jan, no swap Process of bubble sort : string [4] 0x x x00422fd4 0x x x00422fdc September Jan Mar Feb JulyOctober Jan < July, no swap 0x x x00422fd40x x x00422fdc September Jan Mar Feb JulyOctober Feb < Jan, no swap 0x x x00422fd40x x x00422fdc September Jan Mar Feb JulyOctober sorting complete

observation Data type is immaterial, we only need to provide comparison operator. In other words, framework of bubble sort is independent of comparison operation. How can we implement an algorithm for bubble sort such that it is independent of data type, for example, string ? pseudocode User-defined comparison operator

Framework of bubble sort [1] define a new data type string_type = char* Data type for sorting

Framework of bubble sort [2] Data type for sorting Question: How about if we change int to void* ?

Framework of bubble sort [3] 強制轉型, type checking int 和 void* 有同樣的大小, 在 32 位元機器上, 其 size = 4 Bytes

Framework of bubble sort [4] 強制轉型, type checking Great! swap_string is the same as swap_int. Question: How to handle comparison operation (use function pointer) ?

Framework of bubble sort [5] comp is a function pointer, either strcmp or integer operator>

Framework of bubble sort [6] comp: function pointer prototype of this function pointer

Framework of bubble sort [7] prototype of this function pointer Standard library Question: how about floating-point array?

Framework of bubble sort [8] Wrong! Correct! sizeof(double) = 8 bytes ! = sizeof( void* ) sizeof(float) = 4 bytes = sizeof( void* )

Framework of bubble sort [9] 此處 void 可視為 any data type

0x00422fdc Unsorted string array 0x x x x00422fd40x September Jan Mar Feb July October base: Unsorted integer array base: 4.0 Unsorted double array base: bytes 4 bytes base[0] base: base[1]base[ j ]base[ j+1 ]base[ n-1 ] Framework of bubble sort [10] General unsorted array size bytes

base[0] base: base[1]base[ j ]base[ j+1 ]base[ n-1 ] General unsorted array size bytes Framework of bubble sort [11] (*cmp) ( &base[ j ], &base[ j-1 ] ) We send address of base[ j ] and base[ j-1 ] to user-defined comparison operator *cmp since we don’t know the data type so far. base[ j ]base[ j+1 ] size bytes swap( &base[ j ], &base[ j-1 ], size ) We send address of base[ j ] and base[ j-1 ] to swap function since we don’t know the data type, also in order to swap both elements we require size of data type

void qsort( void *base, size_t n, size_t size, int (*cmp)( const void *, const void * ) ) qsort sorts into ascending order an array base[0], … base[n -1] of objects of size size. The comparison function cmp is page 253 The qsort function implements a quick-sort algorithm to sort an array of n elements, each of size bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument cmp is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call. Framework of bubble sort [12]

C:\Program Files\Microsoft Visual Studio\VC98\CRT\SRC\QSORT.c Framework of bubble sort [13] Exercise: you can trace the source code to find out quick- sort algorithm

OutLine Memory address and pointer Pointer and array Call-by-value Pointer array: pointers to pointers Function pointer Application of pointer

E(14) 進位 Application: How to extract value each field of a floating number single precision Question: what is result of printf ( “%x”, *aInt_ptr )

0 3E a = & How to extract sign field &

E 3E F a = & How to extract exponent field &

0 3E FFFFF a = & How to extract fraction field &

Report value of sign, exponent and fraction fields Question: interpret value of sign, exponent and fraction after shift