Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.

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

Pointers Typedef Pointer Arithmetic Pointers and Arrays.
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.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
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 and References (1) THE REFERENCE OPERATOR REFERENCES POINTERS THE DEREFERENCE OPERATOR DERIVED TYPES RETURNING A REFERENCE.
Starting Out with C++, 3 rd Edition 1 Chapter 9 – Pointers.
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.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
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.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
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.
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.
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.
Object-Oriented Programming in C++
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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.
Slide 1 of 36. Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
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.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
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.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Operator Overloading Introduction
Pointers and Dynamic Arrays
Standard Version of Starting Out with C++, 4th Edition
Chapter 9: Pointers.
Chapter 10: Pointers Starting Out with C++ Early Objects
Pointers and References
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Pointers and dynamic objects
Standard Version of Starting Out with C++, 4th Edition
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Pointers, Variables, and Memory

Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold the address to a chunk of memory. The operator & is used to return a variable’s memory address int intVar;// allocate memory by // declaring a variable cout << " Address = " << &intVar << endl;

Variables and Pointers A pointer variable is used to hold an address to a memory location. A pointer variable has a type associated with it, and the memory address it points to, should hold data of that type int myInt = 0;// Allocates memory, stores 0 int *pMyInt;// Declares an empty pointer // variable

Dereferencing Pointers If we want to set or get the value that is stored at the memory address in the pointer then we have to use operator* int myInt = 0;//Allocates memory, stores 0 int *pMyInt;//Declares an empty pointer pMyInt = &myInt;//Puts address in the ptr variable name myInt pMyInt valuememory address 3012

Dereferencing Pointers If we want to set or get the value that is stored at the memory address in the pointer then we have to use operator* int myInt = 0;//Allocates memory, stores 0 int *pMyInt;//Declares an empty pointer pMyInt = &myInt;//Puts address in the ptr *pMyInt = 5;//puts 5 into myInt variable name myInt pMyInt valuememory address 3012

Dereferencing Pointers If we want to set or get the value that is stored at the memory address in the pointer then we have to use operator* int myInt = 0;//Allocates memory, stores 0 int *pMyInt;//Declares an empty pointer pMyInt = &myInt;//Puts address in the ptr *pMyInt = 5;//puts 5 into myInt cout << myInt << endl;//Prints 5 cout << *pMyInt << endl;//Also prints 5 cout << pMyInt << endl;//What prints? variable name myInt pMyInt valuememory address 3012

Where are the errors? int main(){ int m; int *pm; *pm = 5; int n; int *pn = &n; pn = 5; }

Where are the errors? int main(){ int m; int *pm; *pm = 5; int n; int *pn = &n; pn = 5; } ERROR! No address in pm //Correction pm = &m; *pm = 5;

Where are the errors? int main(){ int m; int *pm; *pm = 5; int n; int *pn = &n; pn = 5; } ERROR! No address in pm //Correction pm = &m; *pm = 5; ERROR! Missing operator* //Correction *pn = 5;

Pointers & Arrays Pointers are simply, by definition, variables that hold addresses. A name of an array also holds the address of the first element in the array. This value cannot change. Therefore, an array name can be considered to be a pointer constant.

Pointers & Arrays const int CAPACITY = 5; int myArray[CAPACITY];//Declare an array cout << myArray;//Prints its address for( int i=0; i<CAPACITY; ++i ) myArray[i] = i;//initialize the elements cout << *myArray << endl;//Prints 0 cout << myArray[0] << endl;//Prints myArray

Pointer Arithmetic Pointer arithmetic allows a few arithmetic operators for manipulating the addresses in pointers. In an array a, a is a constant pointer to the first element and a+1 is a constant to the second element. In the same way, if p points to the second element in an array, then p-1 point to the preceding element in that array, and p+1 points to the succeeding element.

Pointer Arithmetic int a[5] = {2, 4, 6, 8, 10}; int *p; p = &a[1]; cout << a[0] << ", " << p[-1]<< ", " << *(p - 1) << endl; cout << a[1] << ", " <<p[0]<< ", " << *(p) << endl; cout << a[2] << ", " << p[1]<< ", " << *(p + 1) << endl; Output: 2, 2, 2 4, 4, 4 6, 6, 6

Incrementing and Decrementing Addresses with Postfix and Prefix Operators Adding 1 to a pointer causes the pointer to point to the next element of the type being pointed to. Therefore p++ should point to the next element. However, when a combination of indirection and postfix operators are used ( as is often done), use the precedence table to figure out what gets executed first. Postfix operators have higher precedence than the indirection operator.

Incrementing and Decrementing Addresses with Postfix and Prefix Operators int *p; int a[5] = {12, 4, 16, 98, 50}; p = a; cout << (*p)++ << endl;// prints 12 cout << *p << endl;// print 4 cout << *++p << endl;// prints 16 cout << *p << endl; // prints 16 cout << (*p)-- << endl; // prints 16 cout << *p << endl; // prints 4 cout << *--p << endl; // prints 12 cout << *p << endl; // prints 12 Is this the same as *p++? Is this the same as *(--p) ?

Dynamically Allocating Arrays An array can be created “dynamically” with “new” “new” allocates a section of memory for the array, and then returns a pointer to it. This is a major advantage when you don’t know how large the array will be until the program is running int capacity, *myData; cin >> capacity; myData = new int[capacity];//Creates the array for( int i=0; i<capacity; ++i ) //Initialize it myData[i] = i; cout << myData[0] << endl; //Prints 0 delete [] myData;

Using delete Memory allocated with “new” must always be recovered by “delete” Always “delete” an array when you don’t need it anymore if you created it with “new” delete [] myData;

Function Arguments The variables that are used to pass data into a function or to return results Example bool isPalindrome( string forw, string rev ){ if( forw == rev ) return true; } Arguments can be passed – By value – the default in C++ – By reference arguments or parameters function name return type

Passing Arguments by Value Used to pass data only into a function When a variable is passed by value, a copy of it is made inside the function. The copy is destroyed when the function returns. Example: passing an argument by value void noChange( int n ){ n = n + n; } int main() { int num = 5; noChange( num ); cout << num << endl; //prints 5 }

Reference Argument Syntax int &count Indicates that an alias for the argument is used inside the function

Using References Used primarily for function arguments to implement “passing by reference” Advantage: Efficiency – Passing variables to functions by reference is very useful if you want to change or update a variable through a function. – If your variable is a relatively large variable, or a whole class or struct or array, it is always advisable to pass the variable by reference since big amounts of data need not be copied when evaluating them.

Passing Arguments by Reference # include void Square(int &pVal); main() { int Number=10; printf("Number is %d\n", Number); Square(Number); printf("Number is %d\n", Number); } void Square(int &pVal) { pVal *= pVal; printf("Number is %d\n", pVal); }

Passing Arguments by Pointer #include void Square(int *pVal); main() { int Number=10; printf("Number is %d\n", Number); Square(&Number); printf("Number is %d\n", Number); } void Square(int *pVal) { *pVal *= *pVal; printf("Number is %d\n", *pVal); }

Reference vs. Pointers References cannot be re-assigned. They are like constant pointers. A good rule of the thumb is to use references when you can, and pointers when you ‘have to’

Passing an Array to a Function Since an array name is actually a pointer to its first element, when an array is passed to a function, an address operator is not needed. int findMax ( int [], int ); int main() { int a[5] = {12, 4, 16, 98, 50}; cout << "The maximum value is " << findMax(a, 5) << endl; return 1; } int findMax ( int vals[], int numElem ) { int max = vals[0]; for (int i=1; i<numElem; i++ ) if (max < vals[i] ) max = vals[i]; return max; }

Passing an Array to a Function However, since an address is actually passed, the code can also be written thus: int findMax ( int [], int ); int main() { int a[5] = {12, 4, 16, 98, 50}; cout << "The maximum value is " << findMax(a, 5) << endl; return 1; } int findMax ( int *vals, int numElem ) { int max = vals[0]; for (int i=1; i<numElem; i++ ) if (max < vals[i] ) max = vals[i]; return max; }

Passing an Array to a Function Here are two other versions of findMax, this time using pointers instead of array subscripts: int findMax ( int *vals, int numElem ) { int max = *vals; for (int i=1; i<numElem; i++ ) if (max < *(vals + i) ) max = *(vals + i); return max; }

Passing an Array to a Function int findMax ( int *vals, int numElem ) { int max = *vals++;// gets the first element and // then increments for (int i=1; i<numElem; i++, vals++ ) if (max < *vals ) max = *vals; return max; }

Dynamically Allocated structs We can dynamically create a structure with “new” Use “new” to allocate memory and return a pointer Person *pDonald = new Person; When a pointer is used to set values stored in a structure, each field must be accessed by the “ -> ” operator pDonald->ID = 1005; pDonald->firstName = "Donald"; pDonald->lastName = "Knuth"; cout firstName << endl; delete pDonald;//Always delete after new Optional syntax: cout << (* pDonald ).firstName;

Dynamic Memory Allocation Data structures that use arrays, structs, and objects to store their data often use dynamic memory allocation to create the storage space More efficient – We don’t need to know in advance how much space to set aside – Pointers can be passed to functions more efficiently because pointer parameters are passed by reference

Struct Constructors Creating a structure that initializes itself struct Person{ int ID; string name; Person(int i = 0000, string n = "Adam") : ID(i), name(n) { } }; Constructor is called whenever a Person is declared Person adam;//Default values used cout << adam.name << endl;//Prints Adam Person david(1002,"David");//Arguments used Person *pFrank = new Person;//Default values used pFrank->name = "Frank"; Initializer list Default value Parameter Constructor name