Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p. 133-151.

Similar presentations


Presentation on theme: "1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p. 133-151."— Presentation transcript:

1 1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p. 133-151

2 2 Dynamic Data: Linked Lists A list of nodes in which each node has two components: one to store the info one to store the address of the next node in the list Structure of a node: Structure of a linked list: A pointer (head) points to the first node

3 3 Dynamic Data How might this be better than an array for storing a list: –use just the memory we need –flexibility wrt delete/insert What do we need: –variables that point to memory locations –ways to assign them –ways to find what they point to Is this used anywhere else in computers? –file storage (did you ever defrag your disk?)

4 4 Pointer Data Types and Variables The most radical new ideas in this course –data types: classes –implementing data types: pointers Pointer variable: a variable whose content is a memory address dataType *identifier; //declaration int *p; //p is a pointer variable of type int "Address of" operator: & &p is the address of p Dereferencing operator * *p is the content in the memory location pointed to by p pointer this: personType& personType::setFirstName(string first) { firstName = first; return *this; //return content pointed to by this }

5 5 Pointers int *p; int num;

6 6 Pointers int *p; int num; num=78;

7 7 Pointers int *p; int num; num=78; p= #

8 8 Pointers int *p; int num; num=78; p= # *p=24;

9 9 Example 1 int x; x = 12; int* ptr; 2000 12 x 3000 ptr

10 10 Example 1 int x; x = 12; int* ptr; ptr = &x; Because ptr holds the address of x, we say: ptr “points to” x 2000 12 x 3000 2000 ptr

11 11 2000 12 x 3000 2000 ptr int x; x = 12; int* ptr; ptr = &x; cout << *ptr; //prints 12 The value pointed to by ptr is denoted by *ptr. *ptr: get the object pointed to by ptr Example 1

12 12 int x; x = 12; int* ptr; ptr = &x; *ptr = 5; //changes the value at the adddress of ptr //to 5 Example 1 2000 12 5 x 3000 2000 ptr

13 13 char ch; ch = ‘A’; char* q; Example 2 4000 A ch 5000 q

14 14 char ch; ch = ‘A’; char* q; q = &ch; Example 2 4000 A ch 5000 4000 q

15 15 char ch; ch = ‘A’; char* q; q = &ch; *q = ‘Z’; char* p; Example 2 4000 A Z ch 5000 6000 4000 ??? q p

16 16 char ch; ch = ‘A’; char* q; q = &ch; *q = ‘Z’; char* p; p = q; // the right side has value 4000 // p and q both point to ch Example 2 4000 A Z ch 5000 6000 4000 4000 q p

17 17 struct studentRec{ char name[27]; double gpa;... }; studentRec *studentPtr; studentPtr is the pointer *studentPtr is the record Two ways to access the gpa: (*studentPtr).gpa studentPtr → gpa Accessing Members of a Struct studentPtr Fred Fink 3.9 123456 A *studentPtr

18 18 Classes, structs, and Pointer Variables classExample *cExpPtr; classExample cExpObject;

19 19 Classes, structs, and Pointer Variables What happens after cExpPtr → setX(5) ? (assumes classExample has a member function setX)

20 20 Classes, structs, and Pointer Variables

21 21 Null Pointer The pointer constant that doesn't point to anything: NULL or 0 NULL is not memory address 0. int *x; x is undefined x = NULL; or x points to nothing x = 0; (not rec.) x = &y; x points to y ? NULL x x addr of y x

22 22 Have we ever had "temporary" data in C++? Yes: functions have local variables that are in scope only during function lifetime What use is this? –we get to reuse space Are there other ways to explicitly use memory and then release it when we don't need it? Yes, pointers Dynamically Allocated Data

23 23 Dynamically Allocated Data operator new: new dataType; //to allocate a single variable new dataType[intExp]; //to allocate a dynamic array E.g. int *x; //x is unassigned x = new int; //finds empty memory and sets the value //of x to its address x Or: x = new int[5]; //finds enough empty memory for an array of 5 integers and sets the value of x to its beginning address. x x[0] x[1] x[2] x[3] x[4]

24 24 Dynamically Allocated Data: syntax operator delete: delete ptr; //to destroy a single dynamic variable delete [] ptr; //to destroy a dynamically created array The memory is returned The pointer variable is unassigned (not NULL) Are these different? delete ptr; ptr=NULL We get a memory leak (example on slide 29) –nothing points to it and it cannot be reused NULL

25 25 2000 x Dynamically Allocated Data char* x; x = new char; *x = ‘B’; cout << *x;

26 26 Dynamically Allocated Data char* x; x = new char; *x = ‘B’; Dynamic data has no variable name 'B' 2000 x

27 27 Dynamically Allocated Data char* x; x = new char; *x = ‘B’; cout << *x; B is printed 'B' 2000 x

28 28 Dynamically Allocated Data char* x; x = new char; *x = ‘B’; cout << *x; delete x; Delete deallocates the memory pointed to by x. 2000 x ?

29 29 Memory Leaks int* x1 = new int; *x1 = 8; int* x2 = new int; *x2 = -5; x1 = x2; // the 8 becomes inaccessible 8 x1 -5 x2 8 x1 -5 x2

30 30 int* x1 = new int; *x1 = 8; int* x2 = new int; *x2 = -5; x1 = x2; delete x2; // ptr is left dangling x2 = NULL; Dangling Pointers 8 x1 -5 12 8 x1 NULL x2

31 31 Operations on Pointers int *p, *q; assignment: p=q; //copies q's address into p dereferencing: *p //retrieves the object pointed to by p boolean p= =q; //true if they point to the same address p++; //adds 4 bytes (the size reserved for int) to p p=p+1; //the same as p++

32 32 Functions and Pointers void example(int* &p, double *q); * indicates a pointer & indicates reference p is reference. q is value Does this fit with CS 131 reference parameters? Yes! 131 &: "give the function the address of" 132 &: "the address of " Pointers can be returned: int* example(...)


Download ppt "1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p. 133-151."

Similar presentations


Ads by Google