Constant pointers and pointers to constants

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
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.
 2006 Pearson Education, Inc. All rights reserved Pointers.
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.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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 © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CS 1704 Introduction to Data Structures and Software Engineering.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
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.
Dynamic Array Allocation char *ptr; // ptr is a pointer variable that // can hold the address of a char ptr = new char[ 5 ]; // dynamically, during run.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Pointers and Dynamic Arrays
Chapter 7 Pointers and C-Strings
Standard Version of Starting Out with C++, 4th Edition
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 9: Pointers.
Lecture 9 Files, Pointers
Chapter 10: Pointers Starting Out with C++ Early Objects
Pointers Revisited What is variable address, name, value?
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
8 Pointers.
Chapter 10: Pointers Starting Out with C++ Early Objects
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9: Pointers.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Dynamic Data, and Reference Types
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
COP 3330 Object-oriented Programming in C++
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Standard Version of Starting Out with C++, 4th Edition
Dynamic Memory Allocation
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Constant pointers and pointers to constants If you have a value in your program and it should not change, or if you have a pointer and you don't want it to be reassigned to point to a different value, you should make it a constant with the const keyword.

Constant pointers and pointers to constants There are generally two places that the const keyword can be used when declaring a pointer. Consider the following declaration: char ch = 'A'; char * chPtr = &ch; This is a simple declaration of the variable chPtr. chPtr is a pointer to a character variable and, in this case, points to the character 'A'.

Constant pointers and pointers to constants Now consider the following three declarations assuming that ch has been defined as a type char variable (i.e. const char ch = 'A';) : const char * chPtr = &ch; char * const myPtr = &ch; const char * const chPtr = &ch; They are all three valid and correct declarations. Each assigns the address of ch to a character pointer. The difference is in what is constant.

Constant pointers and pointers to constants The second declaration char * const myPtr declares a constant pointer to a character. The location stored in the pointer cannot change. You cannot change where this pointer points: char ch1 = 'A'; char ch2 = 'B'; char * const myPtr = &char_A; myPtr = &char_B; // error - can't change // address of myPtr

Constant pointers and pointers to constants The third declares a pointer to a character where both the pointer value and the value being pointed to will not change. const char * const chPtr = &ch;

Pointers to Constant Must use const keyword in pointer definition: const double taxRates[] = {0.65, 0.8, 0.75}; const double *ratePtr; Use const keyword for pointers in function headers to protect data from modification from within function

Pointer to Constant – What does the Definition Mean? See pr10-13.cpp Read as: “rates is a pointer to a constant that is a double.”

Constant Pointers Defined with const keyword adjacent to variable name: int classSize = 24; int * const classPtr = &classSize; Must be initialized when defined Can be used without initialization as a function parameter Initialized by argument when function is called Function can receive different arguments on different calls

Constant Pointers While the address in the pointer cannot change, the data at that address may be changed

Constant Pointer to Constant Can combine pointer to constants and constant pointers: int size = 10; const int * const ptr = &size; What does it mean?

Dynamic Memory Allocation Can allocate storage for a variable while program is running Uses new operator to allocate memory double *dptr; dptr = new double; new returns address of memory location

Dynamic Memory Allocation Can also use new to allocate array arrayPtr = new double[25]; Program may terminate if there is not sufficient memory Can then use [ ] or pointer arithmetic to access array

Dynamic Memory Example int *count, *arrayptr; count = new int; cout <<"How many students? "; cin >> *count; arrayptr = new int[*count]; for (int i=0; i<*count; i++) { cout << "Enter score " << i << ": "; cin >> arrayptr[i]; }

Releasing Dynamic Memory Use delete to free dynamic memory delete count; Use delete [] to free dynamic array memory delete [] arrayptr; Only use delete with dynamic memory! See pr10-14.cpp

Returning Pointers from Functions Pointer can be the return type of function int* newNum(); The function must not return a pointer to a local variable in the function The function should only return a pointer to data that was passed to the function as an argument to dynamically allocated memory See pr10-15.cpp

Pointers to Class Objects Can create pointers to objects and structure variables class Square {…}; Square sq1[4]; Square *squarePtr = &sq1[0];

Structure Pointer Operator To access a member, use the form ptr->member: squarePtr->setSide(14); in place of the form (*ptr).member: (*squarePtr).setSide(14);

Dynamic Memory with Objects Can allocate dynamic objects using pointers: stuPtr = new Student; Can pass values to constructor: squarePtr = new Square(17); delete causes destructor to be invoked: delete squarePtr; See pr10-16.cpp

Object Pointers as Function Parameters Pointers to objects can be passed as parameters to functions Such pointers provide a pass-by-reference parameter mechanism Pointers must be dereferenced in the function to access the member fields See pr10-17.cpp

Controlling Memory Leaks Memory that is allocated with new should be deallocated with a call to delete as soon as the memory is no longer needed. This is best done in the same function as the one that allocated the memory. For dynamically-created objects, new should be used in the constructor and delete should be used in the destructor See pr10-18cpp

Selecting Members of Objects Situation: An object contains a pointer as a member. There is also a pointer to the object. Problem: How do we access the pointer member via the object pointer? class GradeList { private: string courseNum; int * grades; } GradeList test1, *test2 = new GradeList;

Selecting Members of Objects Expression Meaning test1->grades Access the grades pointer in test1. This is the same as (*test1).grades *test1->grades Access the value pointed at by tes1->grades. This is the same as *(*test1).grades *test1.grades Access the value pointed at by test1.grades