CMPE Data Structures and Algorithms in C++ September 14 Class Meeting

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
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.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic 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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
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.
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.
Dynamic memory allocation and Pointers Lecture 4.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
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:
Introduction to Programming
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
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.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
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.
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
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Last week: We talked about: History of C Compiler for C programming
Pointers and Dynamic Arrays
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers and Dynamic Arrays
Computer Organization and Design Pointers, Arrays and Strings in C
The Machine Model Memory
EGR 2261 Unit 10 Two-dimensional Arrays
CS 153: Concepts of Compiler Design November 28 Class Meeting
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
Pointers and Pointer-Based Strings
Pointers Revisited What is variable address, name, value?
CMPE 180A Data Structures and Algorithms in C++ February 15 Class Meeting Department of Computer Engineering San Jose State University Spring 2018 Instructor:
Lecture 6 C++ Programming
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers and Dynamic Variables
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Programming in C Pointer Basics.
Chapter 12 Pointers and Memory Management
C++ Pointers and Strings
Pointers and Pointer-Based Strings
COP 3330 Object-oriented Programming in C++
CS 144 Advanced C++ Programming February 12 Class Meeting
The C Language: Intro.
Chapter 9: Pointers and String
Pointers and dynamic objects
CS 144 Advanced C++ Programming February 12 Class Meeting
CMPE 135: Object-Oriented Analysis and Design March 14 Class Meeting
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
CMPE 152: Compiler Design April 18 – 30 Labs
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
CS 144 Advanced C++ Programming April 30 Class Meeting
CS 144 Advanced C++ Programming April 30 Class Meeting
CMPE 152: Compiler Design March 19 Class Meeting
CMPE 152: Compiler Design May 2 Class Meeting
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CMPE 180-92 Data Structures and Algorithms in C++ September 14 Class Meeting Department of Computer Engineering San Jose State University Spring 2017 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Assignment #3 Sample Solutions

Pointers Pointers are an extremely powerful feature of C and C++ programs. You would not be a competent C or C++ programmer if you did not know how to use pointers effectively. Pointers can also be extremely dangerous. Many runtime errors and program crashes are due to misbehaving pointers. Pointers are a prime cause of memory errors.

An int vs. Pointer to an int A graphical representation of an int variable named num and its value: A graphical representation of a pointer variable named ptr that points to an int value of a variable named num: 5 num 5 ptr num

Declaring and Assigning Pointers After the following statements are executed: We have this situation: int num = 5; int *ptr = # 5 ptr num

Pointers are Addresses To declare that a variable is a pointer, use a * before the variable name: ptr can point to an int value ptr2 can point to a double value The statement assigns the address of variable num to pointer variable ptr Make ptr point to the address of variable num. int *ptr; double *ptr2; & is the address-of operator ptr = #

The Dereferencing Operator 5 ptr num int num = 5; int *ptr = # To get the value that pointer ptr is pointing to: Now the * is the dereferencing operator. “Follow the pointer to get what it’s pointing to.” We can use *ptr in an expression. Example: *ptr + 2 gives the value 7. *ptr

The Dereferencing Operator, cont’d 5 ptr num int num = 5; int *ptr = # In the above example, both *ptr and num refer to the same value 5. What happens if we execute the statement? Now both num and *ptr are 9. *ptr = 9; 9 ptr num

A Pointer Declaration Warning You can declare several pointer variables in one line: How many pointer variables do we have? Only ptr1 is a pointer to a double value. ptr2 and ptr3 are simple double variables. double *ptr1, *ptr2, *ptr3; double* ptr1, ptr2, ptr3;

Break

The new Operator So far, all our variables have names and are created automatically when we declare them: We can also create nameless variables. The new operator returns a pointer to the variable it just created. This is ideal for pointer variables. int num; int *ptr = new int(42); 42 ptr a nameless variable

The delete Operator If your program creates nameless variables, then it must remove them from memory when the program no longer needs them. Delete from memory the nameless variable that ptr points to. If your program doesn’t get rid of all the nameless variables it created, those variables clutter up memory, and therefore you are said to have a memory leak. delete ptr;

Pointer Parameters We can pass a pointer by value to a function: We can change the value of the variable that ptr1 points to. We can also pass a pointer by reference: We can change what variable ptr1 points to. Ugly syntax! void foo(int *ptr1, double *ptr2); void bar(int* &ptr1, double* &ptr2);

typedef Use typedefs to simplify pointer notation: Now you can use IntPtr in place of int * and DoublePtr in place of double * typedef int *IntPtr; typedef double *DoublePtr; void foo(IntPtr ptr1, DoublePtr ptr2); void bar(IntPtr& ptr1, DoublePtr& ptr2);

Using Pointers to Pass-by-Reference C programmers used pointers to pass parameters by reference. Example: A call to the function needed the address of the corresponding argument: Because parm points back to the actual argument, the function can use *parm to change the value of the actual argument. function baz(int *parm); int arg; baz(&arg);

Pointers and Arrays An array variable is actually a pointer variable. The array/pointer variable points to the first element of the array. int a[3]; a int a[3]; int *p; p = a; a p

Pointer Arithmetic int a[3]; int *p; p = a; a p The following expressions all access the third array element: a[2] p[2] *(p+2) *(a+2) What is *p+2 ?

Pointer Arithmetic, cont’d int a[3]; int *p; p = a; a p Use a pointer to iterate through an array. In the above example, p initially points to the first element of the array. Then p++ points to the second element. And next, p++ points to the third element.

Dynamic Arrays Up until now, whenever we declared an array, we explicitly gave its size. Example: But suppose we don’t know until run time how many elements we need. Example: At run time, your program reads in a count of names, and then the names. You want to create an array that can hold exactly that many names. You can use a dynamic array (instead of a vector). int a[10];

Dynamic Arrays, cont’d If the size of the array you want is in variable n whose value you don’t know until run time, use the new operator to create an array of size n. Use a pointer variable to point to the first element of the dynamic array. When you’re done with the array, use the special form of the delete operator to remove the array from memory: string *names = new string[n]; delete [] names; Demo

char* and char** Recall that C programs didn’t have C++ style strings, but instead had arrays of characters. The declaration is for a dynamic character array, a C-string. If you have a dynamic array of C-strings, you need a pointer to a pointer of characters: char *cstr; char **cstr_array;

Assignment #4. Big Pi You will compute and print the first 1,000 decimal digits of pi. Algorithm: Nonic convergence at https://en.wikipedia.org/wiki/Borwein's_algorithm You will use the Multiple-Precision Integers and Rationals (MPIR) library. http://mpir.org/ The library is distributed as C source files. Use the library to create and work with numbers with arbitrarily long precision.

Assignment #4. Big Pi, cont’d You will learn how to download the source files, compile them, and configure, build, and install the MPIR library. Useful skills to have, because you will most likely need to use other libraries in the future. graphics libraries circuit simulation libraries numerical computing libraries etc.

Assignment #4. Big Pi, cont’d Building and installing the MPIR library is straightforward on Linux and Mac OS. Therefore, if you are on Windows, use VirtualBox to run Linux as a virtual machine. VirtualBox: https://www.virtualbox.org/wiki/VirtualBox Debian Linux: https://www.debian.org/ Ubuntu Linux: https://www.ubuntu.com/ Download and install a Linux disk image (.iso file) into VirtualBox.

Assignment #4. Big Pi, cont’d Please work together to help each other to build and install MPIR. Programs must be individual work, as usual. Extra credit: Compute one million digits of pi.