Review 1 List Data Structure List operations List Implementation Array Linked List.

Slides:



Advertisements
Similar presentations
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Advertisements

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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 10: Pointers.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Lesson 9 Pointers CS 1 Lesson 9 -- John Cole1. Pointers in Wonderland The name of the song is called ‘Haddock’s Eyes’.” “Oh, that’s the name of the song,
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.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
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.
Pointers and Dynamic Memory Allocation. Declaring a pointer.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
POINTERS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers Data Structures CSI 312 CSI Dept. Dr. Yousef Qawqzeh.
Standard Version of Starting Out with C++, 4th Edition
Chapter 9: Pointers.
Lecture 9 Files, Pointers
Chapter 10: Pointers Starting Out with C++ Early Objects
Pointer.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Lecture 6 C++ Programming
Chapter 10: Pointers Starting Out with C++ Early Objects
Chapter 9: Pointers.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 9: Pointers.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Pointers, Dynamic Data, and Reference Types
C++ Pointers and Strings
Data Structures and Algorithms Introduction to Pointers
Standard Version of Starting Out with C++, 4th Edition
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
Pointers.
Presentation transcript:

Review 1 List Data Structure List operations List Implementation Array Linked List

Pointer 2 Pointer Variables Dynamic Memory Allocation Functions

What is a Pointer? A Pointer provides a way of accessing a variable without referring to the variable directly. The mechanism used for this purpose is the address of the variable. A variable that stores the address of another variable is called a pointer variable. 3

Pointer Variables Pointer variable: A variable that holds an address Can perform some tasks more easily with an address than by accessing memory via a symbolic name: Accessing unnamed memory locations Array manipulation etc. 4

Why Use Pointers? To operate on data stored in an array To enable convenient access within a function to large blocks data, such as arrays, that are defined outside the function. To allocate space for new variables dynamically–that is during program execution 5

Pointer Data Types and Pointer Variables Pointer variable: variable whose content is a memory address Syntax to declare pointer variable: dataType *identifier; Address of operator: Ampersand, & Dereferencing operator/Indirection operator: Asterisk, * 6

The Address-Of Operator (&) The address-of operator, &, is a unary operator that obtains the address in memory where a variable is stored. int number = 1234; int*pnumber= &number; //stores address of //number in pnumber char a = „a ‟ ; char *pa = &a;//stores address of a in pa. 7

The Indirection Operator How pointer variable is used to access the contents of memory location? The indirection operator, *is used for this purpose. cout<< *pnumber; 8

The Indirection Operator int x= 4; 4 x 1310 px Addresses cout<<“The number is:”<<x; The output is: The number is: 4 cout<<“The number accessed by pointer variable is: ”<<*px The output is: The number is: 4 4 x Addresses int *px = &x; //stores address of variable x in variable px 9

The Indirection Operator int x= 4; 4 x 1310 px Addresses *px = 3 //This statement means assign 3 to a variable “pointed to” by px. int *px = &x; //stores address of variable x in variable px 3 The data is accessed indirectly 10

Pointer Representation int x = 10; int *p; p = &x; p gets the address of x in memory. p x10 11

Example int x = 10; int *p; p = &x; *p = 20; *p is the value at the address p. p x20 12

What is a pointer? int x = 10; int *p; p = &x; *p = 20; Declares a pointer to an integer & is address operator gets address of x * dereference operator gets value at p 13

Pointers Memory Representation Statements: int *p; int num; 14

cont… 15

cont… 16

cont… 17

Pointers Summary of preceding diagrams &p, p, and *p all have different meanings &p means the address of p p means the content of p *p means the content pointed to by p, that is pointed to by the content of memory location 18

Getting the Address of a Variable Each variable in program is stored at a unique address Use address operator & to get address of a variable: int num = 23; cout << &num; // prints address // in hexadecimal 19

Pointer Variables Definition: int *intptr; Read as: “ intptr can hold the address of an int” Spacing in definition does not matter: int *intptr; // same as above 20

Pointer Variables Assignment: int *intptr; intptr = &num; Memory layout: Can access num using intptr and indirection operator * : cout << *intptr << endl; numintptr 25 0x4a00 address of num : 0x4a00 21

Assigning a value to a dereferenced pointer A pointer must have a value before you can dereference it (follow the pointer). int *x; *x=3; int foo; int *x; x = &foo; *x=3; ERROR!!! x doesn’t point to anything!!! this is fine x points to foo 22

Pointers to anything int *x; int **y; double *z; x some int some *int some int y z some double 23

The Relationship Between Arrays and Pointers Array name is starting address of array int vals[] = {4, 7, 11}; cout << vals; cout << vals[0]; 4711 starting address of vals : 0x4a00 // displays 0x4a00 0x4a00 // displays 4 24

Pointers and Arrays An array name is basically a const pointer. You can use the [] operator with a pointer: int *x; int a[10]; x = &a[2]; x is “the address of a[2] ” 25

Pointer Arithmetic Operations on pointer variables: OperationExample int vals[]={4,7,11}; int *valptr = vals; ++, -- valptr++; // points at 7 valptr--; // now points at 4 +, - (pointer and int ) cout << *(valptr + 2); // 11 +=, -= (pointer and int ) valptr = vals; // points at 4 valptr += 2; // points at 11 - (pointer from pointer) cout << valptr–val; // difference //(number of ints) between valptr // and val 26

Pointer arithmetic Integer math operations can be used with pointers. If you increment a pointer, it will be increased by the size of whatever it points to. int a[5]; a[0] a[1] a[2] a[3] a[4] int *ptr = a; *ptr *(ptr+2) *(ptr+4) 27

Initializing Pointers Can initialize at definition time: int num, *numptr = &num; int val[3], *valptr = val; Cannot mix data types: float cost; int *ptr = &cost; // won’t work 28

Comparing Pointers Relational operators ( =, etc.) can be used to compare addresses in pointers Comparing addresses in pointers is not the same as comparing contents pointed at by pointers: if (ptr1 == ptr2) // compares // addresses if (*ptr1 == *ptr2) // compares // contents 29

Operator new and new[] In order to request dynamic memory we use the operator new. new is followed by a data type specifier pointer = new type if a sequence of more than one element is required- the number of these are given within brackets []. pointer = new type [number_of_elements] 30

cont… It returns a pointer to the beginning of the new block of memory allocated. int * ptr; ptr = new int [5]; In this case, the system dynamically assigns space for five elements of type int and returns a pointer to the first element of the sequence, which is assigned to ptr. Therefore, now, ptr points to a valid block of memory with space for five elements of type int. 31

cont… The first element pointed by ptr can be accessed either with the expression ptr[0] or the expression *ptr. Both are equivalent. The second element can be accessed either with ptr[1] or *(ptr+1) and so on... 32

Operator delete and delete[] Since the necessity of dynamic memory is usually limited to specific moments within a program, once it is no longer needed it should be freed so that the memory becomes available again for other requests of dynamic memory. This is the purpose of the operator delete, whose format is: delete pointer; delete [] pointer; 33

cont… The first expression should be used to delete memory allocated for a single element, and the second one for memory allocated for arrays of elements. The value passed as argument to delete must be either a pointer to a memory block previously allocated with new, or a null pointer (in the case of a null pointer, delete produces no effect). 34

#include int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new int[i]; for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } 35

How many numbers would you like to type? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, 436, 1067, 8, 32, 36

Dynamic Memory Allocation DMA provides a mechanism to allocate memory at run- time by the programmer Memory is a Free Store During execution of program, there is unused memory in the computer. It is called free store. Dynamic Storage duration Life-span of dynamic variables is defined by the programmer The Operators new and delete are used for DMA 37

Void Pointer A void* is a generic pointer. Pointer of any type can be assigned to the void pointer Once assigned the type of void* is the same as that of assigned pointer type Dereferencing a void* is a syntax error void* sPtr; int num; int z[3]={1,2,3}; sPtr= z; num= *sPtr; // ERROR num= *(int*) sPtr; // correct version 38

Functions Pass by value A copy of argument it created The value of the passed argument is not modified The operation is performed on the copy of passed value as argument Pass by reference The address of argument is passed The value of the passed argument gets modified This may occur by Reference variable or through pointer variables 39

Pointer Parameters Pointers are passed by value (the value of a pointer is the address it holds). If we change what the pointer points to the caller will see the change. If we change the pointer itself, the caller won't see the change (we get a copy of the pointer) 40

Pointers as Function Parameters A pointer can be parameter Works like reference variable to allow change to argument from within function Requires: 1) asterisk * on parameter in prototype and heading void getNum(int*); // function prototype void getNum(int *ptr) // function header // ptr is pointer to an int 2) asterisk * in body to dereference the pointer cin >> *ptr; 3) address as argument to the function getNum(&num); // pass address of num to getNum 41

Pointers as Function Parameters void swap(int *x, int *y) {int temp; temp = *x; *x = *y; *y = temp; } int num1 = 2, num2 = 3; swap(&num1, &num2); 42

Passing pointers as arguments When a pointer is passed as an argument, it divulges an address to the called function, so the function can change the value stored at that address: void passPointer(int* iPtr){ *iPtr += 2; }... int i = 3; int* iPtr = &i; passPointer(iPtr); cout << "i = " << i << endl; // prints i = 5 passPointer(&i); // equivalent to above cout << "i = " << i << endl; // prints i = 7 43

Summary 44 Pointer Pointer Variables Dynamic Memory Allocation Functions