Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 指標.

Similar presentations


Presentation on theme: "Chapter 指標."— Presentation transcript:

1 Chapter 指標

2 P-0 指標基本概念 指標是一種間接使用資料的方法,指標變數是用 來儲存記憶體位址
指標運作步驟: 取出指標中所儲存的記憶體位置,到這個記憶體位置讀寫資料。 記憶體來源有兩種: 1). 記錄其他非指標變數的記憶體位置 2). 程式執行中動態配置一塊記憶體

3 P-1 使用指標 記錄其他非指標變數的記憶體位置 程式說明 將指標指向已宣告的記憶體,則可以利用指標來間接地 使用記憶體
integer, pointer :: p integer, target :: a p => a 宣告p為一個指標,其指標所指向的記憶體的資料型態為整數 宣告a為一個可以當成目標的變數 將指標p指到變數a

4 P-1 使用指標 < Ex. 完整程式> 記憶體 位置 變數 內容 001 a 1 002 p 3 2 001
program ex1001 implicit none integer, pointer :: p integer, target :: a=1 p=>a write(*,*) p a=2 p=3 write(*,*) a end program ex1001 記憶體 位置 變數 內容 001 a 1 002 p 指標的宣告 3 2 001 < Ex. 執行結果> 1 2 3 把指標p指到變數a 改變a的值 改變指標p指向的記憶體內容

5 P-1 使用指標 程式執行中動態配置一塊記憶體 程式說明 將指標指向未知的記憶體,可以在程式執行中動態配置
或釋放,但無法直接使用記憶體變數,必須藉由指標來 使用記憶體 程式說明 allocate(p) deallocate(p) 配置一塊記憶體空間給指標p。注意使用指標之前,一定要先設定好指標的目標,否則會出現記憶體使用錯誤的訊息,如 segmentation fault 經由allocate得到的記憶體空間,需用deallocate釋放回去,否則此記憶體空間一直被佔用,無法使用,直到整個程式結束為止

6 P-1 使用指標 < Ex. 完整程式> 記憶體 位置 變數 內容 001 a 1 002 b 2 003 p ??? 001
program ex1002 implicit none integer, target :: a=1, b=2 integer, pointer :: p allocate(p) p=100 write(*,*) p deallocate(p) p => a end program 記憶體 位置 變數 內容 001 a 1 002 b 2 003 p ??? 配置一塊可以存放integer的記憶體空間給指標p 001 ??? 得到記憶體後指標p可以像一般整數一樣來使用 100 < Ex. 執行結果> 100 釋放記憶體 把指標p指到變數a

7 P-2 指標相關函式 associated ( pointer, target ) 檢查指標pointer是否有設定指向目標變數target,傳回值為邏輯值。若省略目標變數target,則只檢查這個指標是否已經指定好方向 null ( ) Fortran 95 新增的函數。會傳回一個不能使用的記憶體位址,在指標還沒有指向前設定成這個值,可以讓 assocaited 函式判斷不會出錯 nullify ( pointer1, pointer2, … ) 用來把指標設定成還沒有指向任何記憶體位址。Fortran 90只能使用nullify而不能使用null函數來設定指標 Ex. integer, pointer :: p => null( ) Ex. integer, pointer :: p nullify(p)

8 P-3 指標陣列 指標陣列使用方法一 程式說明 把指標指向已宣告的陣列,配置記憶體空間來使用
integer, pointer :: a (:) integer, target :: b(5)=(/ 1,2,3,4,5 /) a => b 宣告a為一個一維指標陣列,其指標所指向的記憶體的資料型態為整數 宣告b為一個可以當成目標的陣列 將指標a內的元素指到b內相對應的元素

9 P-3 指標陣列 a a a b 1 2 3 4 5 < Ex. 完整程式> < Ex. 執行結果> 指標陣列的宣告
program ex1004 implicit none integer, pointer :: a(:) integer, target :: b(5)=(/ 1,2,3,4,5 /) a=>b write(*,*) a a=>b(1:3) a=>b(1:5:2) end program a a 指標陣列的宣告 b a(1~5)=>b(1~5) < Ex. 執行結果> 1 2 3 1 3 5 a(1)=>b(1) a(2)=>b(2) a(3)=>b(3) a(1)=>b(1) a(2)=>b(3) a(3)=>b(5)

10 P-3 指標陣列 指標陣列使用方法二 程式說明 把指標指向未知的記憶體,在程式執行中動態配置或釋放 allocate(a(5))
deallocate(a) 配置5個整數的空間給指標陣列a allocate得到的記憶體要記得歸還

11 P-3 指標陣列 a ??? 1 2 3 4 5 < Ex. 完整程式> < Ex. 執行結果>
program ex1005 implicit none integer, pointer :: a(:) allocate( a(5) ) a = (/ 1,2,3,4,5 /) write(*,*) a deallocate( a ) end program 配置5個整數的空間給指標陣列a < Ex. 執行結果> 得到記憶體後指標陣列a可以像一般陣列一樣來使用 allocate得到的記憶體要記得歸還

12 P-4 指標與函式 指標變數可以當成參數在 subroutine 之間傳遞, 也可以當成 function 的傳回值。
使用原則: 1). 要把指標傳遞給 subroutine 時,要宣告這個 subroutine 的參數使用介面 interface。 2). 指標參數宣告時不需 intent 這個形容詞。 3). Function 傳回值若為指標時,需要定義 function 的 interface。

13 P-3 指標陣列 < Ex. 完整程式> <接下頁> program ex1007 implicit none
integer, target :: a(8)=(/ 10, 15, 8, 25, 9, 20, 17, 19 /) integer, pointer :: p(:) interface function getmin(p) integer, pointer :: getmin end function end interface p=>a(1:8:2) write(*,*) getmin(p) end program <接下頁> p(1)=>a(1) p(2)=>a(3) p(3)=>a(5) p(4)=>a(7)

14 P-3 指標陣列 < Ex. 完整程式> < Ex. 執行結果> function getmin(p)
implicit none integer, pointer :: p(:) integer, pointer :: getmin integer :: i, s, min_value s = size(p,1) min_value = 2**30 do I = 1, s if ( min_value > p(i) ) then min_value = p(i) getmin => p(i) end if end do return end function 查尋陣列的大小 先把 min_value 設定成一個很大的值 < Ex. 執行結果> 8

15 P-3 指標陣列 < Ex. 完整程式> <接下頁> module func contains
function getmin(p) implicit none integer, pointer :: p(:) integer, pointer :: getmin integer :: i, s, min_value s=size(p,1) min_value = 2**30 do i=1,s if ( min_value > p(i) ) then min_value = p(i) getmin => p(i) end if end do <接下頁> 把function寫在module裡面,是比較好的寫法

16 P-3 指標陣列 < Ex. 完整程式> < Ex. 執行結果> return end function
end module program ex1008 use func implicit none integer, target :: a(8)=(/ 10, 15, 8, 25, 9, 20, 17, 19 /) integer, pointer :: p(:) p => a(1:8:2) write(*,*) getmin(p) end program < Ex. 執行結果> 8


Download ppt "Chapter 指標."

Similar presentations


Ads by Google