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

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 6 Data Types
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
Object-Oriented Programming in C++
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Chapter 3 Pointers and Array-Based Lists Dr. Youssef Harrath
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Linked lists.
Pointer Data Type and Pointer Variables II
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Linked lists.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

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

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 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 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 Pointers int *p; int num;

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

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

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

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

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

x 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 int x; x = 12; int* ptr; ptr = &x; *ptr = 5; //changes the value at the adddress of ptr //to 5 Example x ptr

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

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

15 char ch; ch = ‘A’; char* q; q = &ch; *q = ‘Z’; char* p; Example A Z ch ??? q p

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 A Z ch q p

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 A *studentPtr

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

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

20 Classes, structs, and Pointer Variables

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 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 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 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

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

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

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

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

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 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 x x1 NULL x2

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 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(...)