Presentation is loading. Please wait.

Presentation is loading. Please wait.

EGR 2261 Unit 11 Pointers and Dynamic Variables

Similar presentations


Presentation on theme: "EGR 2261 Unit 11 Pointers and Dynamic Variables"— Presentation transcript:

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

2 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

3 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.

4 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.

5 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….

6 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.

7 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;

8 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;

9 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 Since num1 is an int variable, it needs four memory cells, and actually occupies the cells at addresses , , , and

10 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.

11 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

12 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.)

13 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.”

14 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

15 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

16 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

17 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

18 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

19 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

20 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.

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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.

30 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

31 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

32 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

33 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

34 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

35 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.


Download ppt "EGR 2261 Unit 11 Pointers and Dynamic Variables"

Similar presentations


Ads by Google