Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.

Slides:



Advertisements
Similar presentations
C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.
Advertisements

Dynamic memory allocation
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Introduction to Programming Lecture 39. Copy Constructor.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Chapter 10.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
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 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT], MPhil (Comp. Sci), PGDCA, ADCA, Dc. Sc. & Engg.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
Pointers. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program execution.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
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.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
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.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
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.
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.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
POINTERS.
Review 1 List Data Structure List operations List Implementation Array Linked List.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
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.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Pointers What is the data type of pointer variables?
Student Book An Introduction
8 Pointers.
Pointer Data Type and Pointer Variables
Dynamic Memory Allocation
Chapter 12 Pointers and Memory Management
C Programming Pointers
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Presentation transcript:

Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson and Introduction to C++ Programming by Roberge and Smith

Lecture – Pointers2 Areas for Discussion Pointers: Introduction Declaring Pointers Dereferencing a Pointer Pointer Arithmetic The Address Operator Pointers and Arrays Dynamic Memory Allocation De-allocating Memory Allocation

Lecture – Pointers3 Pointers: Introduction

Pointers –Are variables that holds the memory address of a data value rather than the data value itself –point to the data value –Are useful for: Manipulating array elements Processing strings Dynamically allocating system memory –Are declared by preceding the identifier with the indirection operator * Lecture – Pointers4

5 Declaring Pointers

The declaration: int *iptr; creates a pointer iptr that can store the address of an integer We note: –At this stage iptr does not contain an address –We have to assign an address to iptr –We can assign an address by using the address- of operator & Lecture – Pointers6

Declaring Pointers Example int *iptr; //Declaring iptr as a pointer to an integer value int num = 5;// num contains the integer value 5 iptr = &num;// iptr now points to num To access the value pointed to by a pointer we can use the indirection operator * cout << *iptr << “\n”; Lecture – Pointers7

8 Dereferencing Pointers

Dereferencing a Pointer cout << *iptr << “\n”; Here the indirection operator is used to: –Output the integer value (5) –Stored in the memory location (num) –Pointed to by iptr Accessing a value pointed to by a pointer is called dereferencing a pointer The * operator is referred to as the dereferencing operator Lecture – Pointers9

10 Assigning Pointers

Assigning a Pointers contents to another Pointer This maybe achieved by using the assignment operator = int *iptrv2; //iptrv2 is a pointer to an integer value iptrv2 = iptr // iptrv2 points to what iptr points to (num) The above creates a second pointer iptrv2 to the integer variable num Lecture – Pointers11

Lecture – Pointers12 Pointers and Arrays

Recall an array variable is a pointer to the first element in an array char section[11] = “A Title”, // sample string *strptr;// pointer to a char in the string strptr = section; Here both section and strptr point to the first character in the array Lecture – Pointers13

Pointers and Arrays ‘A’ ‘ ’‘ ’ ‘T’ ‘i’ ‘t’ ‘l’ ‘e’ ‘\0’ Lecture – Pointers14 sectionstrptr

Pointer Arithmetic #include Using namespace std; Void main() { char section[11] = “A Title”, //sample string *strptr; //pointer to character in string //set strptr to point to the first character in string strptr = section; //display each character in the string. Stop when strptr points to the null character at the end of the string while(*strptr != ‘\0’) { cout << *strptr; //output character that pointer strptr points to strptr++; // Advance to the next character } cout << “\n” } Lecture – Pointers15

Pointers and Arrays ‘A’ ‘ ’‘ ’ ‘T’ ‘i’ ‘t’ ‘l’ ‘e’ ‘\0’ Lecture – Pointers16 section strptr points to this memory location after third execution of statement strptr++

Pointers and Arrays ‘A’ ‘ ’‘ ’ ‘T’ ‘i’ ‘t’ ‘l’ ‘e’ ‘\0’ Lecture – Pointers17 section Strptr points to this memory location after seventh execution of statement strptr++

Lecture – Pointers18 Dynamically Allocating Memory Storage and De-allocating Memory Storage

Dynamically Allocating Storage So far we allocate space for an array at compile time. –For example: const int MaxLength = 11; int num1; double list[500]; char inputstring[MaxLength]; Signals to the compiler to reserve enough memory for: A pair of integers An array of 500 double prcision floating type numbers A string containing 11 characters Lecture – Pointers19

Dynamically Allocating Storage This approach to memory allocation is inefficient, particularly for arrays and strings It is often the case that –the size of an array is not known until run-time –The size varies and although its maximum size may be, say, 500 it could regularly be much smaller Rather than –specifying as much memory as one might need at compile time, thereby regularly wasting lots of memory –better to specify the array size at run-time and dynamically allocate memory Lecture – Pointers20

Dynamically Allocating Storage We can dynamically allocate memory space for an array at run-time by using the new operator list = new double [listsize] –Allocates an array containing listsize double precision numbers –Assigns list to point to the array where list is of type double* Lecture – Pointers21

dynamic.cpp – code fragment.... int listsize;//input list size double *list;//pointer to dynamically allocated array cout << “Enter the array size: ”; //get size of array needed cin >> listsize; list = new double[listsize]; //allocate an arrray of specified size cout << “Enter the array elements: ”; //read in array elements For(int i=0; i < listsize; i++) cin >> list[i];.... Lecture – Pointers22

De-allocating Storage When we no longer require dynamically allocated memory we have to de-allocate it This is achieved using the delete operator delete [ ] list; –Note use of [ ] to indicate that list points to an array not a single double precision number Lecture – Pointers23

Lecture – Pointers24 Summary Pointers: Introduction Declaring Pointers Dereferencing a Pointer Pointer Arithmetic The Address Operator Pointers and Arrays Dynamic Memory Allocation De-allocating Memory Allocation