+ All Categories
Home > Documents > Pointers (review and examples)

Pointers (review and examples)

Date post: 03-Feb-2016
Category:
Upload: betrys
View: 23 times
Download: 0 times
Share this document with a friend
Description:
Pointers (review and examples). CSE 373 Data Structures Lecture 2. Basic Types and Arrays. Basic Types integer, real (floating point), boolean (0,1), character Arrays A[0..99] : integer array. 0 1 2 3 4 5 6 7 99. …. A. A[5]. Records and Pointers. - PowerPoint PPT Presentation
26
Pointers (review and examples) CSE 373 Data Structures Lecture 2
Transcript
Page 1: Pointers (review and examples)

Pointers (review and examples)

CSE 373

Data Structures

Lecture 2

Page 2: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 2

Basic Types and Arrays

• Basic Types› integer, real (floating point), boolean (0,1),

character

• Arrays› A[0..99] : integer array

A0 1 2 3 4 5 6 7 99

A[5]

Page 3: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 3

Records and Pointers

• Record (also called a struct)› Group data together that are related

› To access the fields we use “dot” notation.

real_part : real

imaginary_part : real

X : complex pointer

X.real_partX.imaginary_part

Page 4: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 4

Record Definition

• Record definition creates a new typeDefinitionrecord complex : (

real_part : real,

imaginary_part : real

)

Use in a declaration

X : complex

Page 5: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 5

Pointer• A pointer is a reference to a variable or record (or object in Java world).

• In C, if X is of type pointer to Y then *X is of type YX : blob pointer

blob*X

Page 6: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 6

Creating a Record

• We use the “new” operator to create a record.P : pointer to blob;

P := new blob;

P

P

(null pointer)

Page 7: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 7

Simple Linked List• A linked list

› Group data together in a flexible, dynamic way.› We’ll describe several list ADTs later.

4 9 13 20

L : node pointer

record node : ( data : integer, next : node pointer)

Page 8: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 8

Application Sparse Polynomials

• 10 + 4 x2 + 20 x40 + 8 x86

010

24

4020

868

P

record poly : (

exp : integer,

coef : integer,

next : poly pointer

)

exp

coef

next

Exponents in Increasing order

Page 9: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 9

Identically Zero Polynomial

P null pointer

10

20

860

P

Page 10: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 10

Addition of Polynomials

010

24

4020

868

P

17

210

86-8

Q

10 + 4 x2 + 20 x40 + 8 x86

7 x + 10 x2 - 8 x86

Page 11: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 11

Recursive AdditionAdd(P, Q : poly pointer): poly pointer{R : poly pointercase { P = null : R := Q ; Q = null : R := P ; P.exp < Q.exp : R := P ; R.next := Add(P.next,Q); P.exp > Q.exp : R := Q ; R.next := Add(P,Q.next); P.exp = Q.exp : R := P ; R.coef := P.coef + Q.coef ; R.next := Add(P.next,Q.next);}return R}

Page 12: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 12

Example

010

24

4020

868

P

17

210

86-8

Q

Add

Page 13: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 13

Example (first call)

010

24

4020

868

P

17

210

86-8

Q

Add

R

Page 14: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 14

The Recursive Call

010

24

4020

868

P

17

210

86-8

Q

Add

R

Page 15: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 15

During the Recursive Call

010

214

4020

860

17

210

86-8

Add

R

Returnvalue

Represent return values

Page 16: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 16

After the Recursive Call

010

214

4020

860

17

210

86-8

Add

R

Returnvalue

Page 17: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 17

The final picture

010

214

4020

860

17

210

86-8

R

garbage

unneeded

Page 18: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 18

Notes on Addition

• Addition is destructive, that is, the original polynomials are gone after the operation.

• We don’t salvage “garbage” nodes. Let’s talk about this.

• We don’t consider the case when the coefficients cancel. Let’s talk about this.

Page 19: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 19

Unneeded nodes to Garbage

• How would you force the unneeded node to be garbage in the code on slide 11?

• Suggestions?

Page 20: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 20

Memory Management – Private Store

• Private store – get blocks from a private store when possible and return them when done.+ Efficiently uses blocks of a specific size- The list of unused blocks can build up

eventually using too much memory.

Page 21: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 21

Private Store

010

214

4020

860

17

210

86-8

R

garbage

unneeded

Page 22: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 22

Private Store

010

214

4020

860

17

210

86-8

R

FreeList

Page 23: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 23

Memory Management – Global Allocator

• Global Allocator’s store – always get and return blocks to global allocator+ Necessary for dynamic memory.+ Blocks of various sizes can be merged if

they reside in contiguous memory.- Allocator may not handle blocks of different

sizes well.- Allocator may be slower than a private

store.

Page 24: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 24

Memory Management – Garbage Collection

• Garbage collection – run time system recovers inaccessible blocks from time-to-time. Used in Lisp, Smalltalk, Java.+ No need to return blocks to an allocator or

keep them in a private store.- Care must be taken to make unneeded

blocks inaccessible.- When garbage collection kicks in there

may be undesirable response time.

Page 25: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 25

Solution for Polyn. Addition

P.exp = Q.exp : R := P ;

R.coef := P.coef + Q.coef ;

if R.coef = 0 then

R := Add(P.next,Q.next);

// The terms with coef = 0 have been removed from the // result

else

R.next := Add(P.next,Q.next);

}

Page 26: Pointers (review and examples)

1/8/03 Pointers - Lecture 2 26

Use of Private Store or Global Allocator

P.exp = Q.exp : R := P ;

R.coef := P.coef + Q.coef ;

if R.coef = 0 then

R := Add(P.next,Q.next);

Free(P); Free(Q);

else

R.next := Add(P.next,Q.next);

Free(Q);

}


Recommended