Applications of Pointer in Earth Sciences Programming
E N D
Presentation Transcript
Chapter 8 Pointers Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien Liu Department of Earth Sciences National Cheng Kung University Last updated: 2 November 2004
Introduction • Pointer • A variable that holds a memory address • Ex 8-1 • “Address of” operator & • Convention of naming • Declaration • Indirection (dereference) operator * • 提領(取出指標所指物體的內容) • The value stored at … • Initialization • Wild pointer dangerous • Pointers addresses variables • Manipulating data: Ex 8-2 • Examining the address: Ex 8-3
Why use pointers • Three tasks of using pointers • Managing data on the free store • Accessing class member data and functions • Passing variables by reference to functions • Five areas of memory • Global name space global variables • The free store heap all remaining memory • Registers internal housekeeping • Code space code • The stack local variables, function parameters • Concept of free store
Using free store • Advantages of free store • Solves the problem of global and local variable • Remains available until you explicitly free it • Only functions with access to the pointer tightly controlled interface • How to use the free store • new • delete • Ex 8-4 • Ex 8-5: applying to object • Memory leak
Using free store (cont.) • Access member data of objects • dot operator “.” • points-to operator “->” • Ex 8-6 • Ex 8-7 • Stray pointers • Ex 8-9 • const pointers • Ex 8-10