review records dynamic memory and pointers introduction to linked lists

45
Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Upload: teagan-obrien

Post on 30-Dec-2015

25 views

Category:

Documents


4 download

DESCRIPTION

Lecture 8. Review Records Dynamic Memory and Pointers Introduction to Linked Lists. Review: Records. This name is now a type which can be used anywhere a type such as “Num” can be used. What are these called?. Types. LB. Records Within Records. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Review RecordsDynamic Memory and Pointers

Introduction to Linked Lists

Page 2: Review Records Dynamic Memory and Pointers Introduction to Linked Lists
Page 3: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Review: Records

Page 4: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Records Within Records

There is nothing to prevent us from placing records inside of records (a field within a record):

Date_Type definesa record day, month, year isoftype numEndrecord

Student_Type definesa record name isoftype string gpa isoftype num birth_day isoftype Date_Type graduation_day isoftype Date_Typeendrecord

This name is now a type which can

be used anywhere a type such as “Num” can be

used.

What are these called?

Types

LB

Page 5: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Record Within Records

Date_Type:

Student_Type:

bob isoftype Student_Typebob.birth_day.month <- 6

day month year

day month year

name gpa

day month yearbirth_day

graduation_day

Page 6: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Types vs. Variables

• TYPE Definitions– Create templates for new kinds of variables– Do not create a variable – no storage space is

allocated– Have unlimited scope

• VARIABLE Declarations– Actually create storage space– Have limited scope - only module containing

the variable can “see” it– Must be based on an existing data type

Page 7: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Dynamic Memory and Pointers

Page 8: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Dynamic vs. Static

Static (fixed in size)• Sometimes we create data structures that

are “fixed” and don’t need to grow or shrink.

Dynamic (change in size)• Other times, we want the ability to

increase and decrease the size of our data structures to accommodate changing needs.

Page 9: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Static Data

• Static data is data declared “ahead of time.”

• It is declared in a module (or main algorithm) and “lives” for as long as that module is active.

• If we declare more static variables than we need, we waste space.

• If we declare fewer static variables than we need, we are out of luck.

• Often, real world problems mean that we don’t know how many variables to declare, as the number needed will change over time.

Page 10: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Dynamic Data

• Dynamic data refers to data structures which can grow and shrink to fit changing data requirements.

• We can allocate (create) additional dynamic variables whenever we need them.

• We can de-allocate (kill) dynamic variables whenever we are done with them.

• A key advantage of dynamic data is that we can always have a exactly the number of variables required - no more, no less.

• For example, with pointer variables to connect them, we can use dynamic data structures to create a chain of data structures called a linked list.

Page 11: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Note

• Dynamic data gives us more flexibility• Memory is still limited• But now we can use it where we need it • And we can determine that while the program is

running

LB

Examples?Printer QueuesAirlinersuh, everything?

Examples?Printer QueuesAirlinersuh, everything?

Page 12: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

A View of Memory

Algorithm and Module Code(What you wrote)

Stack (Static Area)(Store stuff here)

Heap (Dynamic Area)(Store stuff here)

LB

Page 13: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

A List Example

• We must maintain a list of data• Sometimes we want to use only a little memory:

• Sometimes we need to use more memory

• Declaring variables in the standard way won’t work here because we don’t know how many variables to declare

• We need a way to allocate and de-allocate data dynamically (i.e., on the fly)

Page 14: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

The Stack

• Recall the activation stack– The stack can expand, but as for the data…– Each frame contains static (fixed size) data

Algo var1 var2 var3

Proc_1 this_var that_var

The number ofvariables neededcome from the“isoftype” statements.

Page 15: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

The Stack and Heap

Main this_var that_var my_num_ptr4 7

12

•The heap is memory not used by the stack• As stack grows, heap shrinks• Static variables live in the stack• Dynamic variables live in the heap

What kind of variable is this???

Heap

Stack

LB

Page 16: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

What?

• We know (sort of) how to get a pointer variable

my_num_ptr isoftype Ptr toa Num

• But how do we get it to point at something?

LB

Page 17: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

The Built-In Function NEW()

• Takes a type as a parameter• Allocates memory in the heap for the type• Returns a pointer to that memory

my_num_ptr <- new(Num)

dynamic_string <- new(String)

list_head <- new(Node)

Page 18: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Accessing Dynamic Data via Pointers

43

Main my_num_ptr

• When we “follow a pointer”, we say that we dereference that pointer

• The carat (^) means “dereference the pointer”• my_num_ptr^ means ”follow my_num_ptr to

wherever it points”• My_num_ptr^ <- 43 is valid

Heap: Dynamic

Stack: Static

Page 19: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Ptr1 isoftype Ptr toa NumPtr2 isoftype Ptr toa NumPtr1 <- new(Num)Ptr1^ <- 5Ptr2 <- Ptr1Print(Ptr1^, Ptr2^)Ptr2^ <- 7Print(Ptr1^, Ptr2^)

Num

5 55 57 7

Ptr1

Ptr

Ptr2

Ptr

57

Pointer Animation of Numbers

static dynamic

Page 20: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

A record to hold two items of data - a name and a SSN:

Student definesa record

name isoftype String

SSN isoftype num

endrecord

And a pointer to a Student record:

current isoftype ptr toa Student

current <- new(Student)

name

SSN

Page 21: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Pointers and Records

currentBob

123456789

static dynamic

current

Page 22: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Pointers and Records

current

current^

Bob

123456789

static dynamic

Page 23: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Pointers and Records

current

current^.name <- “Bob”

Bob

123456789

static dynamic

Page 24: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Pointers and Records

current

current^.SSN <- 123456789

Bob

123456789

static dynamic

Page 25: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

What’s the big deal

• We already knew about static data• Now we see we can allocate dynamic data but• Each piece of dynamic data seems to need a

pointer variable and pointers seem to be static• So how can this give me flexibility

LB

Page 26: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Questions?

Page 27: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Introduction to Linked Lists

Page 28: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Properties of Lists• We must maintain a list of data

• Sometimes we want to use only a little memory:

• Sometimes we need to use more memory

• Declaring variables in the standard way won’t work here because we don’t know how many variables to declare

• We need a way to allocate and de-allocate data dynamically (i.e., on the fly)

Page 29: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Linked Lists “Live” in the Heap

Main this_var list_head4

12 18 21 23

•The heap is memory not used by the stack•Dynamic variables live in the heap•We need a pointer variable to access our list in the heap

Heap

Stack

Page 30: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Linked Lists

With pointers, we can form a “chain” of data structures:

List_Node definesa Record data isoftype Num next isoftype Ptr toa List_Nodeendrecord //List_Node

4 17 42

Page 31: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Linked List Record Template

<Type Name> definesa record

data isoftype <type>

next isoftype ptr toa <Type Name>

endrecord

Example:

Char_Node definesa record

data isoftype char

next isoftype ptr toa Char_Node

endrecord

Page 32: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Creating a Linked List Node

Node definesa record

data isoftype num

next isoftype ptr toa Node

endrecord

And a pointer to a Node record:

current isoftype ptr toa Node

current <- new(Node)

Page 33: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Pointers and Linked Lists

current

current^

current^.next

current^.data

static dynamic

Page 34: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Accessing the Data Field of a Node

current

current^.data <- 42

current^.next <- NIL

42

static dynamic

Page 35: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Proper Data Abstraction

Vs.

Page 36: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

The examples so far have shown a single num variable as node data, but in reality there are usually more, as in:

Node_Rec_Type definesa record this_data isoftype Num that_data isoftype Char other_data isoftype Some_Rec_Type next isoftype Ptr toa Node_Rec_Typeendrecord // Node_Rec_Type

Complex Data Records and Lists

LB

Page 37: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

A Better Approach with Higher Abstraction

One should separate the data from the structure that holds the data, as in:

Node_Data_Type definesa Record this_data isoftype Num that_data isoftype Char other_data isoftype Some_Rec_Typeendrecord // Node_Data_Type

Node_Record_Type definesa Record data isoftype Node_Data_Type next isoftype Ptr toa Node_Rec_Typeendrecord // Node_Record_Type

Page 38: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Creating a Pointer to the Heap

list_head isoftype ptr toa List_Node

Notice that list_head is not initialized and points to “garbage.”

Main list_head

?

Page 39: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Creating a New Node in the List

list_head <- new(List_Node)

Main list_head

?

Page 40: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Filling in the Data Field

list_head^.data <- 42

The ^ operator follows the pointer into the heap.

Main list_head

?42

Page 41: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Creating a Second Node

list_head^.data <- 42list_head^.next <- new(List_Node)

The “.” operator accesses a field of the record.

Main list_head

42 ?

Page 42: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Cleanly Terminating the Linked List

list_head^.next^.data <- 91list_head^.next^.next <- NIL

We terminate linked lists “cleanly” using NIL.

Main list_head

42 91

Page 43: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Deleting by Moving the Pointer

If there is nothing pointing to an area of memory in the heap, it is automatically deleted.

list_head <- list_head^.next

Main list_head

42 91

Page 44: Review Records Dynamic Memory and Pointers Introduction to Linked Lists

Questions?

Page 45: Review Records Dynamic Memory and Pointers Introduction to Linked Lists