EGR 2261 Unit 11 Pointers and Dynamic Variables

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

Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
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.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
February 11, 2005 More Pointers Dynamic Memory Allocation.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
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.
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:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
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.
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.
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.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
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.
Dynamic Storage Allocation
Pointers and Dynamic Arrays
EGR 2261 Unit 10 Two-dimensional Arrays
EGR 2261 Unit 9 One-dimensional Arrays
Pointers.
Standard Version of Starting Out with C++, 4th Edition
Operators in c++.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
EGR 2261 Unit 4 Control Structures I: Selection
Pointers and Memory Overview
Pointers and Pointer-Based Strings
Student Book An Introduction
Values – also known as scalars (eg. int, double, char, float)
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
8 Pointers.
Dynamic Memory Allocation
Chapter 10: Pointers 1.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers and Dynamic Variables
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers.
Chapter 12 Pointers and Memory Management
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Objectives You should be able to describe: Addresses and Pointers
Overloading functions
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

EGR 2261 Unit 11 Pointers and Dynamic Variables Read pages 811-839 in Malik, Chapter 12. Homework #11 and Lab #11 due next week. Quiz next week. -Handouts: Quiz 10, Unit 11 Practice Sheet

Skipping Chapters We’re skipping from Chapter 8 to Chapter 12, but will come back and cover Chapters 9 and 10 in future weeks. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Review from Unit 10: Computer Memory Computer memory is organized as a sequence of billions of memory cells. Each memory cell has an address (or location) and holds a byte-sized unit of information.

Review from Unit 10: Where in Memory Are Variables Stored? The compiler decides which memory cells to use for the variables in your program. These cells will probably be different every time you compile the program.

Review from Unit 10: Why Should You Care? Most of the time you don’t care which cells the compiler chooses, but sometimes you do. Therefore, C++ gives you a way to find out the address at which the compiler has stored a variable….

Review from Unit 10: Address-of Operator The address-of operator & returns the address of a variable. This address will probably be different every time you recompile the program. Demo using week10Memory.

Review from Unit 10: Most Variables Occupy Several Bytes How many memory cells does a variable occupy? It depends on the variable’s data type. For example, on most systems an int variable occupies four memory cells (four bytes of memory). The sizeof operator tells you how many cells the variables of a particular data type occupy: cout << sizeof(int) << endl;

Review from Unit 10: Most Variables Occupy Several Bytes (cont’d.) On the previous slide we used the sizeof operator to find out how many cells the variables of a particular data type occupy: cout << sizeof(int) << endl; We can also use it to find out how many cells a specific variable occupies: int myNum; cout << sizeof(myNum) << endl;

Review from Unit 10: Most Variables Occupy Several Bytes (cont’d.) In an example above, we saw that num1 was stored in memory at address 3275108. Since num1 is an int variable, it needs four memory cells, and actually occupies the cells at addresses 3275108, 3275109, 3275110, and 3275111.

Review: Partial Hierarchy of Data Types Simple Data Types Integral (int, bool, char, …) Floating-Point (double, …) Enumeration Structured Data Types Arrays structs Classes Pointers Pointer variables are different from other variables because they hold addresses, not data.

Pointer Variables A pointer variable is a variable that contains a memory address (usually the address of another variable or of an array). Depending on the datatype of the thing whose address the pointer contains, we call the pointer a pointer-to-int, or a pointer-to-double, and so on. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Why Use Pointers? Experienced C++ programmers use pointers to do many different kinds of things. Some of these uses are very clever and tricky. In this course, we’re interested in pointers mainly because they let the user choose the size of an array while the program is running, rather than making us hard-code the array’s size in our code. (They let us set the array size at run-time rather than at compile-time.)

Pointers Can Be Dangerous Because pointers let you do clever, tricky things that you could not do without pointers, they are one of the more dangerous tools in the C++’s programmer’s toolkit. Many programming languages do not have pointers. You’ll sometimes hear the oversimplified statement that “Java is C++ without pointers.”

Declaring Pointer Variables Syntax to declare a pointer variable: Examples: int *myPtr1; //Declares a pointer-to-int. char *myPtr2; //Declares a pointer-to-char. In these examples, the names of the pointers we’re declaring are myPtr1 and myPtr2, not *myPtr1 and *myPtr2. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

We’re Not Multiplying Anything The previous examples: int *myPtr1; //Declares a pointer-to-int. char *myPtr2; //Declares a pointer-to-char. Note that * is also the multiplication operator, but we’re using the symbol * here in a way that has nothing to do with multiplication. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Where Do You Put the * ? When you declare a pointer variable, you can put the * next to the datatype, or next to the pointer name, or by itself between them. The following declarations are all legal and equivalent to each other: int* myPtr1; // * next to datatype. int *myPtr1; // * next to pointer name. int * myPtr1; // * by itself. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Assigning a Value to a Pointer Recall that the address-of operator & returns an address. This makes it useful for assigning values to pointer variables. Example: int x=34; //Declare and initialize an int. int *p; //Declare a pointer-to-int. p = &x; //Now p holds x’s address. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

How to Think About This The previous example: int x=34; //Declare and initialize an int. int *p; //Declare a pointer-to-int. p = &x; //Now p holds x’s address. In general we don’t care what the actual address is. We just care that p now “points to” x. p x 34 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Dereferencing Operator (*) When used as a unary operator on a pointer, * (which is called the dereferencing operator) refers to the object to which the pointer points. When you see *p in a statement other than a declaration, read it as “the thing pointed to by p.” Example: cout << *p; //Displays the value of //the thing pointed to by p. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Example: Dereferencing Operator (*) int x=34; //Declare & initialize an int. int *p; //Declare a pointer-to-int. p = &x; //Now p holds x’s address. cout << *p << endl; //Displays 34. *p = 68; //Assign a new value to x. cout << x << endl; //Displays 68. p x 34 Do practice question 1.

Assignment Operations using Pointer Variables The value of one pointer can be assigned to another pointer of the same type, using the usual assignment operator =. When you do this, you end up with two pointers pointing to the same variable. See example on next slide. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Example: Assignment Operations using Pointer Variables int x=34; //Declare & initialize an int. int *p1; //Declare a pointer-to-int. int *p2; //Another pointer-to-int. p1 = &x; //Now p1 points to x. p2 = p1; //Now p2 also points to x. Do practice question 2. p1 x 34 p2

Dynamic Variables A dynamic variable is a variable whose memory is allocated while the program is running. This is in contrast to normal variables, whose memory is allocated when the program is compiled. Example--in the following code, the compiler allocates memory for myNum before the program starts running: int myNum; C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Dynamic Variables (cont’d.) To work with dynamic variables, we use pointers and two operators: The new operator lets us create dynamic variables. The delete operator lets us destroy dynamic variables when we no longer need them and wish to free up their memory. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

The new Operator The new operator has two forms: intExp is any expression evaluating to a positive integer new allocates memory for a variable or array of the designated type and returns the address of this memory, which you will almost always want to assign to a pointer variable. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Example: The new Operator int *p; //Declare a pointer-to-int. p = new int; The second statement above does two things: It creates an unnamed int variable during program execution somewhere in memory. It stores the newly created variable’s address in the pointer p. So p now points to the unnamed int variable. p

The new Operator (cont’d.) A dynamic variable’s value cannot be accessed directly because the variable has no name. Rather we must use a pointer to access its value. Extending the previous example: int *p; //Declare a pointer-to-int. p = new int; *p = 34; Do practice questions 3 and 4. p 34 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Memory Leaks and the delete Operator A so-called memory leak occurs when a piece of previously allocated memory is no longer being used but has not been “released” by your program. To avoid memory leaks, when a dynamic variable is no longer needed, destroy it to free up its memory. Use the delete operator to do this. Syntax: C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Example of a Memory Leak In the code below, Line 23 creates a memory leak. After it executes, we have an unnamed variable holding the value 20 that is not pointed to by any pointer. To avoid the leak, do delete q; before Line 23.

Dynamic Arrays A dynamic array is an array whose memory is allocated while the program is running. Example: int *p; p = new int[5]; *p = 25; p stores 25 into the first array element C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Arithmetic Operations on Pointer Variables You can perform some arithmetic operations on pointers: You can use ++ or -- to increment or decrement a pointer variable’s value . You can add or subtract integer values to or from a pointer variable. You can subtract one pointer variable’s value from another pointer variable’s value. These are most useful when you’re using pointers in combination with arrays. (See next slide.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Arithmetic Operations on Pointer Variables (Cont’d.) We can use the ++ operator to step us through the elements in a dynamic array. Example: int *p; p = new int[5]; *p = 25; p++; //Point to next array element. *p = 35; p stores 25 into the first array element stores 35 into the second array element C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Pointer Arithmetic versus Ordinary Arithmetic Arithmetic operators work differently on pointers from how they work on ordinary variables. In previous example, note that p++ adds 4, not 1, to the value of p. int *p; p = new int[5]; *p = 25; p++; //Point to next array element. *p = 35; p Do practice questions 5, 6. stores 25 into the first array element stores 35 into the second array element C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Dynamic Arrays (cont’d.) As an alternative to arithmetic operations on pointers, you can use array subscript notation with a pointer to access the array elements. In the example on the previous slide, we can do: p[0] = 25; p[1] = 35; Stores 25 and 35 into the first and second array elements, respectively. We’re treating p as if it were the array’s name, even though it’s actually the name of a pointer to the array. Do practice question 7. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Setting an Array’s Size at Run-Time The big advantage of dynamic arrays is that you can wait until run-time to decide how big an array should be, rather than having to hard-code the array’s size in your code. Example: int size; int *p; cout << "How big is your array? "; cin >> size; p = new int[size]; //Can’t do this with //static arrays! Do practice question 8.