CS 144 Advanced C++ Programming February 12 Class Meeting

Slides:



Advertisements
Similar presentations
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Advertisements

Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
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.
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 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.
February 11, 2005 More Pointers Dynamic Memory Allocation.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
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.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
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?
Pointers and Dynamic Arrays
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers and Dynamic Arrays
CS31 Discussion Jie(Jay) Wang Week8 Nov.18.
Chapter 7 Pointers and C-Strings
EGR 2261 Unit 10 Two-dimensional Arrays
CMPE Data Structures and Algorithms in C++ September 14 Class Meeting
CMPE Data Structures and Algorithms in C++ September 7 Class Meeting
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Lecture 9 Files, Pointers
Basic Elements of C++.
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
CMPE 180A Data Structures and Algorithms in C++ February 15 Class Meeting Department of Computer Engineering San Jose State University Spring 2018 Instructor:
Pointers Psst… over there.
Basic Elements of C++ Chapter 2.
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Dynamic Memory Allocation
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Pointers and dynamic objects
Pointers and Dynamic Variables
Chapter 9: Pointers.
CS1201: Programming Language 2
Pointers, Dynamic Data, and Reference Types
Chapter 12 Pointers and Memory Management
Pointers.
CS1201: Programming Language 2
C++ Pointers and Strings
CS 144 Advanced C++ Programming February 7 Class Meeting
Pointers and Pointer-Based Strings
CS 144 Advanced C++ Programming February 5 Class Meeting
Arrays Arrays A few types Structures of related data items
CS 144 Advanced C++ Programming February 12 Class Meeting
Pointers and dynamic objects
CS 144 Advanced C++ Programming January 31 Class Meeting
Standard Version of Starting Out with C++, 4th Edition
CS 144 Advanced C++ Programming February 21 Class Meeting
CS31 Discussion 1D Winter19: week 4
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
CS 144 Advanced C++ Programming March 21 Class Meeting
Programming Fundamental
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
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CS 144 Advanced C++ Programming February 12 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor: Ron Mak www.cs.sjsu.edu/~mak

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

Pass a Scalar parameters1.cpp #include <iostream> using namespace std; void pass_by_value(int parm); void pass_by_reference(int& parm); int main() {     int i = 5;     cout << "main calling pass_by_value: i = " << i << endl;     pass_by_value(i);     cout << "main returned from pass_by_value: i = " << i << endl;     i = 5;     cout << endl;     cout << "main calling pass_by_reference: i = " << i << endl;     pass_by_reference(i);     cout << "main returned from pass_by_reference: i = " << i << endl;     return 0; } parameters1.cpp

Pass a Scalar, cont’d void pass_by_value(int parm) {     cout << "pass_by_value: parm = " << parm << endl;     parm = 55; } void pass_by_reference(int& parm)     parm = 555; main calling pass_by_value: i = 5 pass_by_value: parm = 5 pass_by_value: parm = 55 main returned from pass_by_value: i = 5 main calling pass_by_reference: i = 5 pass_by_value: parm = 555 main returned from pass_by_reference: i = 555

Pass a Pointer parameters2.cpp #include <iostream> using namespace std; int i, j;  // global variables, generally to be avoided void pass_pointer_by_value(int *parm); void pass_pointer_by_reference(int* &parm); int main() {     int *ptr = &i;     i = 5;     j = 7;     cout << "main calling pass_pointer_by_value: i = "          << i << ", *ptr = " << *ptr << ", j = " << j << endl;     pass_pointer_by_value(ptr);     cout << "main returned from pass_pointer_by_value: i = "          << i << ", *ptr = " << *ptr << ", j = " << j << endl; ...

Pass a Pointer, cont’d ptr = &i; i = 5; j = 7; cout << endl;     cout << "main calling pass_pointer_by_reference: i = "         << i << ", *ptr = " << *ptr << ", j = " << j << endl;     pass_pointer_by_reference(ptr);     cout << "main returned from pass_pointer_by_reference: i = "          << i << ", *ptr = " << *ptr << ", j = " << j << endl;     return 0; }

Pass a Pointer, cont’d void pass_pointer_by_value(int *parm) {     cout << "pass_pointer_by_value: *parm = " << *parm << endl;     *parm = 55;  // change the value of what parm points to     parm = &j;   // change what parm points to     *parm = 77; // change the value of what parm points to } main calling pass_pointer_by_value: i = 5, *ptr = 5, j = 7 pass_pointer_by_value: *parm = 5 pass_pointer_by_value: *parm = 55 pass_pointer_by_value: *parm = 7 pass_pointer_by_value: *parm = 77 main returned from pass_pointer_by_value: i = 55, *ptr = 55, j = 77

Pass a Pointer, cont’d void pass_pointer_by_reference(int* &parm) {     cout << "pass_pointer_by_reference: *parm = " << *parm << endl;     *parm = 555;  // change the value of what parm points to     parm = &j;    // change what parm points to     *parm = 777;  // change the value of what parm points to } main calling pass_pointer_by_reference: i = 5, *ptr = 5, j = 7 pass_pointer_by_reference: *parm = 5 pass_pointer_by_reference: *parm = 555 pass_pointer_by_reference: *parm = 7 pass_pointer_by_reference: *parm = 777 main returned from pass_pointer_by_reference: i = 555, *ptr = 777, j = 777

Quiz! Quiz 2 – 2019 Feb 14 18 questions 15 minutes

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;

Dynamic Arrays, cont’d #include <iostream> DynamicArray1.cpp #include <string> using namespace std; typedef string *StringPtr; int read_names(StringPtr& names); void print_names(StringPtr& names, int count); int main() {     StringPtr pet_names = nullptr;     int count = read_names(pet_names);     print_names(pet_names, count);     delete[] pet_names;     return 0; } DynamicArray1.cpp

Dynamic Arrays, cont’d int read_names(StringPtr& names) { int count;     cout << "How many? ";     cin >> count;     names = new string[count];  // create the dynamic array     // Loop to read the names.     for (int i = 0; i < count; i++)     {         cout << "Name #" << i+1 << "? ";         cin >> names[i];     }     return count; }

Dynamic Arrays, cont’d Demo void print_names(StringPtr& names, int count) {     StringPtr p = names;  // p also points to the names array     // Print the names forwards.     cout << endl << "Names printed forwards:" << endl;     while (count > 0)     {         cout << *p << endl;         count--;         p++;  // point p at the next array element     }     // p now points beyond the end of the array. Do not dereference!     // Print the names backwards.     cout << endl << "Names printed backwards:" << endl;     do         p--;  // point p at the previous array element     while (p != names); } Demo

Two-Dimensional Dynamic Array A two-dimensional dynamic array is a dynamically allocated one-dimensional array of pointers, each of which points to the first element of a dynamically allocated one-dimensional dynamic array of values. Think of each array of values as a “row” and therefore each element of the array of pointers points to the first element of a row. The rows can all have the same length, or have different lengths.

Two-Dimensional Dynamic Array, cont’d Example: A 3 x 4 dynamic array: Variable rows is a pointer to a one-dimensional array of pointers, each of which is a pointer to a one dimensional array of integer values. 1 2 4 3 8 6 12 9 int **rows int * int Dynamically allocated array of pointers to integers. Dynamically allocated rows of integers. Pointer to a pointer to an integer

Two-Dimensional Dynamic Array, cont’d DynamicArray2.cpp #include <iostream> #include <iomanip> using namespace std; int **make_mult_table(int row_count, int col_count); void print_table(int **rows, int row_count, int col_count); void delete_table(int **rows, int row_count, int col_count); int main() {     int **table = make_mult_table(3, 4);     print_table(table, 3, 4);     delete_table(table, 3, 4);     return 0; }

Two-Dimensional Dynamic Array, cont’d int **make_mult_table(const int row_count, const int col_count) {     // Dynamically allocate an array of pointers to integers.     int **rows = new int*[row_count];     // For each pointer to integer ...     for (int r = 0; r < row_count; r++)     {         // ... dynamically allocate a row of integers.         rows[r] = new int[col_count];         // For each element of a row ...         for (int c = 0; c < col_count; c++)         {             // ... calculate its value.             rows[r][c] = (r+1)*(c+1);         }     }     // Return the pointer to a pointer to an integer.     return rows; }

Two-Dimensional Dynamic Array, cont’d void print_table(int **rows, const int row_count, const int col_count) {     // For each row of the table ...     for (int r = 0; r < row_count; r++)     {         // ... get the pointer to the row of integers.         int *row = rows[r];         // For each element of a row ...         for (int c = 0; c < col_count; c++)         {             // ... print it.             cout << setw(3) << row[c];         }         cout << endl;     } }

Two-Dimensional Dynamic Array, cont’d void delete_table(int **rows, const int row_count, const int col_count) {     // For each row of the table ...     for (int r = 0; r < row_count; r++)     {         // ... delete the row pointed to by the row pointer.         delete[] rows[r];     }     delete[] rows; } 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: What is a dynamic two-dimensional array of C-strings? char *cstr; char **cstr_array;

C++ Strings and Vectors are Better Instead of C-strings, use C++ strings. Instead of dynamic arrays, use C++ vectors. A vector has a dynamic array hidden inside of it. The vector manages the dynamic array for you.