Pointers, Dynamic Data, and Reference Types - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Pointers, Dynamic Data, and Reference Types

Description:

Department of Math & Computer Science. Bennett College. Goals. Pointers ... be applied to a pointer value that was obtained previously from the new operator. ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 31
Provided by: jhy9
Category:

less

Transcript and Presenter's Notes

Title: Pointers, Dynamic Data, and Reference Types


1
Pointers, Dynamic Data, and Reference Types
  • Jianhua Yang
  • Department of Math Computer Science
  • Bennett College

2
Goals
  • Pointers
  • Deep and shallow copy operations
  • Term initialization
  • Four member functions

3
15.1 Pointers
  • Pointer is a variable which contains the address
    of other variable.

4
Pointer Variables
  • Declaration
  • DataType variable
  • DataType variable1, varialbe2

5
Example
  • int intPtr
  • int p, q
  • float p1, p2, p3

6
Pointer Operation
  • int beta
  • int intPtr
  • intPtr beta

7
Memory-level view of a Pointer Variable
Variable Name
Address
5000
intPtr
5008
beta
8
Direct and indirect addressing
  • int beta
  • int intPtr
  • intPtr beta
  • intPtr 28
  • beta28
  • cout ltlt intPtr ltltendl
  • cout ltlt intPtr ltltendl

Indirect addressing
Direct addressing
9
Pointers and structured data type
  • Structured data type
  • Class
  • Struct
  • Example

10
Pointer Expressions
  • We learned
  • Arithmetic expression
  • Relational expression
  • Logical expression
  • Pointer expression
  • It is composed of pointer variables, pointer
    constants, operators, and parentheses.

11
Some Examples
  • intPtr 0 //point to nothing
  • intPtr NULL //point to nothing
  • If (intPtr NULL)
  • DoSomething()
  • Int arr100
  • int ptr
  • ptr arr
  • ptr arr0

12
Pointer operators
13
15.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
14
Advantage
  • Economizing on memory space.

15
Managing 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
16
Two 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.

17
Memory Leak
  • The loss of available memory space that occurs
    when dynamic data is allocated but never
    deallocated.

18
Example 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
19
Inaccessible object
  • A dynamic variable on the heap without any
    pointer pointing to it.

20
Dangling pointer
  • A pointer that points to a variable that has been
    deallocated.

21
15.3 Reference Types
  • Address
  • Pointer
  • Reference

22
Reference 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.

23
Reference Variable Declaration
  • DataType Variable
  • DataType Var1, Var2,

Example int ref int ref1, ref2
24
Compare Reference with Pointer
  • 1. Referencing is different.
  • 2. Compiler treats a reference variable as if it
    were a constant pointer.

25
Example
  • 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
26
Three 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.

27
15.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

28
Example
  • Message Class Definition
  • Message Class Implementation
  • Using Message Class

29
15.5 Case study
  • Examples from Page 813 to 832

30
15.6 Summary
  • Pointers
  • Dynamic Data
  • References
  • Classes and dynamic data
Write a Comment
User Comments (0)
About PowerShow.com