Chapter 12 Pointers and Memory Management

Slides:



Advertisements
Similar presentations
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Advertisements

6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
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.
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.
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.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
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.
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.
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.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
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.
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.
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.
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.
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Pointers What is the data type of pointer variables?
Dynamic Storage Allocation
Pointers and Dynamic Arrays
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers and Dynamic Arrays
Chapter 7 Pointers and C-Strings
Computing Fundamentals with C++
Standard Version of Starting Out with C++, 4th Edition
Pointers & Arrays.
Chapter 9: Pointers.
Motivation and Overview
Lecture 9 Files, Pointers
Learning Objectives Pointers Pointer in function call
Pointers and Memory Overview
CSC113: Computer Programming (Theory = 03, Lab = 01)
COMP 2710 Software Construction Pointers
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Pointers Psst… over there.
8 Pointers.
Pointers Psst… over there.
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 9: Pointers.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers and Dynamic Variables
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Chapter 6 Class Definitions and Member Functions
Chapter 4 Implementing Free Functions
Pointers and Dynamic Variables
Dynamic Memory.
Pointers & Arrays.
Pointers and dynamic objects
Standard Version of Starting Out with C++, 4th Edition
CS 144 Advanced C++ Programming February 12 Class Meeting
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Chapter 12 Pointers and Memory Management 3rd Edition Computing Fundamentals with C++ Rick Mercer Franklin, Beedle & Associates

Goals Understand pointers store addresses of other objects Use primitive C++ arrays with no range checking Use several methods for initializing pointers Use the new and delete operators for memory management

Memory Considerations In addition to name, state, and operations, every object has an address, where the values are stored Objects also have a lifetime beginning with construction to when they are no longer accessible With the following initialization, we see that the name charlie, state 99, and operations like = + cout << are known int charlie = 99; // But where is 99 stored?

Addresses An object's address is the actual memory location where the first byte of the object is stored The actual memory location is something we have not needed to know about until now We can't predict addresses, but ints are four bytes so two integers could have addresses 4 bytes apart int a = 123; int b = 456 Address Type Name State 6300 int a 123 6304 int b 456

Static and Dynamic Memory Allocation Some objects take a fixed amount of memory at compiletime: char int double Other objects require varying amounts of memory, which is allocated and deallocated dynamically, that is, at runtime, string for example We sometimes use pointers to allow for such dynamic objects

Pointers Pointer store addresses of other objects and are declared with * as follows: class-name* identifier ; int anInt = 123; // The int object is initialized int* intPtr; // intPtr stores an address anInt stores an integer value intPtr stores the address of variable So pointer objects may store the address of other objects

About Pointer Types Pointer objects store the address of other objects which are the same type as the type of the pointer An int pointer hold an addresses to a int object int intP = 25; int* intPtr = &intP; A double pointer hold an addresses to a double double doubleD = 25.45; double* doublePtr = &doubleD; A Grid pointer hold an addresses to a Grid object Grid gridG(5, 5, 0, 0, south); Grid* gridPtr = &gridG;

The State of Pointers At this point, the value of intPtr may have or become one of these values Undefined (as intPtr exists above) The special value nullptr to indicate the pointer points to nothing: intPtr = nullptr; The address of the int object: intPtr=&anInt; & means address of

Pointer Values Currently, we may depict the undefined value of intPtr as follows: intPtr The & symbol is called the address-of operator when it precedes an existing object This assignment returns the address of anInt and stores that address into intPtr: intPtr = &anInt; ???

Defining Pointer Objects The affect of this assignment intPtr = &anInt; is represented graphically like this: intPtr anInt Now intPtr is defined 123

The State of Pointers We can change the value of anInt indirectly with the dereference operator * *intPtr = 97; // The same as anInt = 97 Now both objects are defined intPtr anInt or *intPtr 97

The dereference Operator intPtr anInt 97 The following code displays 97 and 96 cout << (*intPtr) << (*intPtr-1) << endl; This code changes 97 to 98 *intPtr = *intPtr + 1;

The & operator The & operator has different meanings depending on how you use it. When you use & to create a variable, you are creating a reference When you use & in front of an existing variable the & is called the address-of operator and returns the address of the variable and not the value stored in the variable

The * operator The * operator also has different meanings depending on how you use it. When you use * to create a variable, you are creating a pointer When you use * in front of an existing pointer, you get the value stored at the address the pointer contains and not the address stored in the pointer The * is also used in math operations when between numeric types

Address-of and Dereference What is the output generated by this program? #include <iostream> using namespace std; int main() { int *p1, *p2; int n1, n2; p1 = &n1; *p1 = 5; n2 = 10; p2 = &n2; cout << n1 << " " << *p1 << endl; cout << n2 << " " << *p2 << endl; return 0; }

Pointers to Objects Pointers can also store the addresses of objects with more than one value Because function calls have a higher precedence than dereferencing, override the priority scheme by wrapping the pointer dereference in parentheses BankAccount anAcct("Ashley", 123.45); BankAccount* bp; bp = &anAcct; (*bp).deposit(123.43); cout << (*bp).getBalance(); // 246.88

Arrow Operator -> C++ also has an arrow operator to send message to object via its address (location in memory) BankAccount anAcct("Ashley", 123.45); BankAccount* bp; bp = &anAcct; bp->deposit(123.43); cout << bp->getBalance(); // 246.88

The Primitive C Array C++ has primitive arrays string myFriends[20]; // store up to 20 strings double x[100]; // store up to 100 numbers There is no range checking with these

Compare C arrays to vector Difference vector Example C Array Example vectors can initialize all vector elements at construction; arrays cannot. vector <int> x(100, 0); All elements are 0   int x[100]; All garbage vectors can be easily resized at runtime; arrays take a lot more work. int n; cin >> n; x.resize(n); Can "grow" an array with more code vectors can be made to prevent out-of-range subscripts. You are told something is wrong cin >> x.at(100); Destroys other memory cin >> x[100]; vectors require an #include primitive, built-in arrays do not. #include <vector> No #include required

The Array/Pointer Connection A primitive array is actually a pointer The array name is actually the memory location of the very first array element Individual array elements are referenced like this address of 1st array element + ( subscript * size of 1 element) Arrays are automatically passed by reference when the parameter has [] void init(int x[], int & n) // Both x and n are reference parameters

Array parameters are reference parameters When passing arrays as parameters, you don't need & x and anArray reference the same array object

Allocating Memory with New Pointers can also be set with the C++ new operator This code allocates a contiguous block of memory to store the state. It also returns the address, or a pointer to the object int* intPtr = new int; *intPtr = 123; cout << *intPtr; // 123 This code allocates a new array int* nums = new int[10]; nums ? [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

The delete Operator General form for recycling memory new allocates memory at runtime delete deallocates that memory to avoid memory leaks so it can be used by other new objects General form for recycling memory delete pointer; delete[] pointer-to-array; For the programs you write, you won't notice any difference by forgetting to delete In a future course with destructors, or in an internship or job, you probably will