Title: Pointers, Dynamic Data, and Reference Types
1Pointers, Dynamic Data, and Reference Types
- Jianhua Yang
- Department of Math Computer Science
- Bennett College
2Goals
- Pointers
- Deep and shallow copy operations
- Term initialization
- Four member functions
315.1 Pointers
- Pointer is a variable which contains the address
of other variable.
4Pointer Variables
- Declaration
- DataType variable
- DataType variable1, varialbe2
5Example
- int intPtr
- int p, q
- float p1, p2, p3
6Pointer Operation
- int beta
- int intPtr
- intPtr beta
7Memory-level view of a Pointer Variable
Variable Name
Address
5000
intPtr
5008
beta
8Direct and indirect addressing
- int beta
- int intPtr
- intPtr beta
- intPtr 28
- beta28
- cout ltlt intPtr ltltendl
- cout ltlt intPtr ltltendl
Indirect addressing
Direct addressing
9Pointers and structured data type
- Structured data type
- Class
- Struct
- Example
10Pointer Expressions
- We learned
- Arithmetic expression
- Relational expression
- Logical expression
- Pointer expression
- It is composed of pointer variables, pointer
constants, operators, and parentheses.
11Some Examples
- intPtr 0 //point to nothing
- intPtr NULL //point to nothing
- If (intPtr NULL)
- DoSomething()
- Int arr100
- int ptr
- ptr arr
- ptr arr0
-
12Pointer operators
1315.2 Dynamic Data
- We learned
- Static data
- Automatic data
- New category of program data
- Dynamic data
Variables created during execution of a program
by means of special operations. They are in HEAP,
a region of memory set aside for dynamic variables
14Advantage
- Economizing on memory space.
15Managing Dynamic Data
int intPtr char nameStr intPtr new
int nameStr new char6
- Creating
- new DataType
- new DataTypeIntExpression
- Destroying
- delete Pointer
- delete Pointer
int intPtr char nameStr intPtr new
int nameStr new char6 delete intPtr delete
nameStr
16Two rules about delete
- 1. Applying delete to the null pointer does no
harm - 2. The delete operator must only be applied to a
pointer value that was obtained previously from
the new operator.
17Memory Leak
- The loss of available memory space that occurs
when dynamic data is allocated but never
deallocated.
18Example of using pointer
- include ltcstddefgt //for NULL
-
- int ptr1 new int
- int ptr2 new int
- ptr2 44
- ptr1 ptr2
- delete ptr1
- ptr1 ptr2
- delete ptr2
- ptr1 NULL
Result in an inaccessible object
Result in a dangling pointer
19Inaccessible object
- A dynamic variable on the heap without any
pointer pointing to it.
20Dangling pointer
- A pointer that points to a variable that has been
deallocated.
2115.3 Reference Types
- Address
- Pointer
- Reference
22Reference Type
- A simple data type consisting of an unbounded set
of values, each of which is the address of a
variable of a given type. - The only operation defined on a reference
variable is initialization.
23Reference Variable Declaration
- DataType Variable
- DataType Var1, Var2,
Example int ref int ref1, ref2
24Compare Reference with Pointer
- 1. Referencing is different.
- 2. Compiler treats a reference variable as if it
were a constant pointer.
25Example
- Using a Reference Variable
- int gamma 26
- int intRef gamma
- intRef 35
- intRef intRef 3
Using a Pointer Variable int gamma 26 int
intPtr gamma intPtr 35 intPtr
intPtr 3
26Three more complicated Examples
Using Reference Swap(alpha,
beta) Void Swap(float x, float y) float
tempx xy ytemp
Using Pointer Swap(alpha,
beta) Void Swap(float x, float y) float
tempx xy ytemp
Using neither Reference nor Pointer S
wap(alpha, beta) Void Swap(float x, float
y) float tempx xy ytemp
- Purpose Exchange the values of two variables
alpha, and beta using a function.
2715.4 Classes and Dynamic Data
- To introduce the following concepts through an
Example - A class constructor
- A class destructor
- A deep copy operation
- A class copy-constructor
28Example
- Message Class Definition
- Message Class Implementation
- Using Message Class
2915.5 Case study
- Examples from Page 813 to 832
3015.6 Summary
- Pointers
- Dynamic Data
- References
- Classes and dynamic data