@ Zhigang Zhu, 2002-2011 1 CSC212 Data Structure - Section RS Lectures 6/7 Pointers and Dynamic Arrays Instructor: Zhigang Zhu Department of Computer Science.

Slides:



Advertisements
Similar presentations
Xiaoyan Li, CSC211 Data Structures Lectures 6/7 Pointers and Dynamic Arrays Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
Pointers and Memory Allocation -L. Grewe. Objectives Why and What are Pointers Create Pointers in C++ Memory Allocation Defined Memory Allocation/Deallocation.
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
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.
Reviews for Exam 1 Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, Fall 2010.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Zhigang Zhu Department.
Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Pointers1 Pointers & Dynamic Arrays Allocating memory at run-time.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d 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 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
February 11, 2005 More Pointers Dynamic Memory Allocation.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
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.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Pointers OVERVIEW.
Edgardo Molina, CSC212 Data Structure - Section AB Lectures 6/7 Pointers and Dynamic Arrays Instructor: Edgardo Molina Department of Computer Science.
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.
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.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
C++ Pointers Read Chapter 2 (P ). COP3530 – C++ Pointers Pointers Pointers provide a method of referencing the memory location of variables provide.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
Edgardo Molina, CSC212 Data Structure - Section AB Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Edgardo Molina Department.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Xiaoyan Li, CSC211 Data Structures Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Prof. Xiaoyan Li Department of Computer Science.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Dynamic Storage Allocation
Pointers and Dynamic Arrays
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointers Revisited What is variable address, name, value?
CSCE 210 Data Structures and Algorithms
Dynamic Memory Allocation
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Pointers, Dynamic Data, and Reference Types
CSC212 Data Structure - Section FG
CSC212 Data Structure - Section RS
Dynamic Memory.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Pointers and References
Pointers, Dynamic Data, and Reference Types
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
Presentation transcript:

@ Zhigang Zhu, CSC212 Data Structure - Section RS Lectures 6/7 Pointers and Dynamic Arrays Instructor: Zhigang Zhu Department of Computer Science City College of New York

@ Zhigang Zhu, Why Pointers and Dynamic Memory p Limitation of our bag class p bag::CAPACITY constant determines the capacity of every bag p wasteful (if too big) and hard to reuse (if too small) p need to change source code and recompile p Solution: p provide control over size in running time p <= dynamic arrays p <= pointers and dynamic memory

@ Zhigang Zhu, Outline (Reading Ch 4.1 – 4.2) p Pointers p *(asterisk) and &(ampersand) operators  Dynamic Variables and new Operator p Dynamic Arrays and Dynamic Objects p Stack (local) vs. heap (dynamic) memory  Garbage Collection and delete Operator p Parameters revisited p Pointers and Arrays as Parameters

@ Zhigang Zhu, Pointer Variable p First let’s have a look at local variables p Q: What’s the value of i? ? … int i;i By this declaration, a cell of 4 adjacent bytes (in some machines) are allocated in the local memory (called stack memory) Address 9## is just for illustration. Real address may have 64 bits

@ Zhigang Zhu, Pointer Variable p First let’s have a look at local variables p Q: How to get the address? … int i; i = 42;i The assignment put number 42 in the cell. The memory address of the 1 st byte is the address of the variable i – the pointer to i

@ Zhigang Zhu, Pointer Variable p First let’s have a look at local variables p Q: Where can we store &i? … int i; i = 42; cout << &i;i & (ampersand) operator - “address of ” operator - &i is 900 ! -Note: two meanings of &

@ Zhigang Zhu, Pointer Variable p The memory address can be stored a special pointer variable p Q: How to point i_ptr to i? 42 ? … int i=42; int *i_ptr;ii_ptr 1. the type of the data that the pointer points to: int 2. an asterisk (*) 3. the name of the newly declared pointer: i_ptr

@ Zhigang Zhu, Pointer Variable p Assign the address of i to i_ptr 42 ? … int i=42; int *i_ptr; i_ptr = &i;ii_ptr What are the results of -cout << i; -cout << i_ptr; -cout << &i_ptr;

@ Zhigang Zhu, Pointer Variable p The i_ptr holds the address of an integer, not the integer itself … int i=42; int *i_ptr; i_ptr = &i;ii_ptr Two ways to refer to i -cout << i; -cout << *i_ptr; - dereferencing operator * - dereferencing operator * - two meanings of * - two meanings of *

@ Zhigang Zhu, Operators * and & p Operator * p Pointer declaration int *i_ptr; p dereferencing operator cout << *i_ptr; p Two different meanings! p Operator & p Reference parameter void funct(int& i); void funct(int& i); p “address of ” operator i_ptr = &i; p Just coincidence? p Will see in parameter passing

@ Zhigang Zhu, Syntax and Naming Issues p How to declare two pointers in a line char *c1_ptr, *c2_ptr; p instead of char* c1_ptr, c2_ptr;  For clarity, use _ptr or cursor for pointer variables

@ Zhigang Zhu, Assignment Operators with Pointers p p2 = p1 int i = 42; int *p1, *p2; p1 = &i; p2 = p1; 42i 900 address value name ?p1904?p2908 Both p1 and p2 point to the same integer 900p p ? ? … ip1 p

@ Zhigang Zhu, Assignment Operators with Pointers p *p2 = *p1 int i = 42; int *p1, *p2; p1 = &i; *p2 = *p1; 42i 900 address value name ?p1904?p2908 p2 doesn’t point to anywhere, so assigning value to *p2 will cause a running time error! 900p ? ? … ip1 p2 900 X

@ Zhigang Zhu, ?p p1908 ?p p2912 Assignment Operators with Pointers p *p2 = *p1 int i = 42; int j = 20; int *p1, *p2; p1 = &i; p2 = &j; *p2 = *p1;42i900 Both i (*p1) and j (*p2) will have the same integer values ? ? … ij p1 p j904 42j904 42

@ Zhigang Zhu, Outline (Reading Ch 4.1 – 4.2) p Pointers p *(asterisk) and &(ampersand) operators  Dynamic Variables and new Operator p Dynamic Arrays and Dynamic Objects p Stack (local) vs. heap (dynamic) memory  Garbage Collection and delete Operator p Parameters revisited p Pointers and Arrays as Parameters

@ Zhigang Zhu, Dynamic Variables p We cannot use a pointer if not initialized p need to point to a declared variable p How to use a pointer without connecting with a declared ordinary variable? p Solution: Dynamic (allocated) variables p not declared, therefore no identifier p created during execution p Real power of pointers is with dynamic variables

@ Zhigang Zhu, The new Operator p allocates memory and return a pointer ?p1900 int *p1; p1 = new int; *p1 = 20;?? p1 points to a dynamic integer variable without any identifier (name) - p1 points to a dynamic integer variable without any identifier (name) - dynamic memory comes from the programs’ heap (free store) 20?10500 ? … … p1?

@ Zhigang Zhu, Dynamic Arrays p new can allocate an entire array all at once ?p1900 int *p1; p1 = new int[4]; p1[2] = 20; cout<<*(p1+2); p1 points to 1st entry of dynamic array - p1 points to 1st entry of dynamic array - number of entries in a pair of sq. brackets - two ways to access p1 (array or pointer) ? … … p1?

@ Zhigang Zhu, Accessing Dynamic Array p Use array notation p the 1 st entry p1[0] = 18; p the 3 rd entry p1[2] = 20; p1[2] = 20; p the ith entry p1[i-1] = 19; p Use pointer notation p the 1 st entry *p1 = 18; p the 3 rd entry *(p1+2) = 20; *(p1+2) = 20; p the ith entry *(p1+i-1) = 19; A demo for pointers and dynamic arrays: test_pointer.cxx

@ Zhigang Zhu, Dynamic Array Example:Quiz p A program read ages of each of CCNY classes, with varying sizes, calculate the average, and then print out the average. size_t size; int *ages; float average; cin >> size; ages = new int[size]; // input ages of all students // calculate average // print average …

@ Zhigang Zhu, Dynamic Objects of a class p new can also allocate a dynamic object ?p1900 point *p1; p1 = new point(1.0, 2.0); cout<< (*p1).get_x(); cout get_x(); - p1 points to dynamic object without name - p1 points to dynamic object without name - parameters can be used as in declaration - two ways to access p1 (* and ->) ? … … p1?

@ Zhigang Zhu, Dynamic Object Arrays of a class Q: Are the followings correct? point3 demo Q: Are the followings correct? point3 demopoint3 demopoint3 demo p Ten points with default coordinates? p1 = new point[10]; p1 = new point[10]; p Ten points with the same coordinates? p1 = new point(1.0, 2.0)[10]; p1 = new point(1.0, 2.0)[10]; p Ten points on the x axis with interval 1? p1 = new point[10]; p1 = new point[10]; for (i=0; i<10; i++) p1[i].set(i, 0); for (i=0; i<10; i++) p1[i].set(i, 0); Assume we have a member function void point::set(double x_init, double y_init); VXV

@ Zhigang Zhu, Failure of the new Operator p Dynamic memory via new operator comes from heap of a program p Heap size from several K to 1GB, however fixed  Could run out of room therefore cause a bad_alloc exception p error message and program halts p Good practice 1: document which functions uses new  Good practice 2: garbage collection by delete operator

@ Zhigang Zhu, Outline (Reading Ch 4.1 – 4.2) p Pointers p *(asterisk) and &(ampersand) operators  Dynamic Variables and new Operator p Dynamic Arrays and Dynamic Objects p Stack (local) vs. heap (dynamic) memory  Garbage Collection and delete Operator p Parameters revisited p Pointers and Arrays as Parameters

@ Zhigang Zhu, The delete Operator p Release any dynamic memory (heap memory) that is no longer needed int *i_ptr; double *d_ptr; point *p_ptr; i_ptr = new int; d_ptr = new double[20]; p_ptr = new point(1.0, 2.0); … delete i_ptr; delete [ ] d_ptr; // empty brackets delete p_ptr; Questions( true or false): 1. 1.delete resets these pointers 2. 2.delete removes dynamic objects pointed by the pointers 3. 3.nothing happens to the pointers themselves XVV

@ Zhigang Zhu, Outline (Reading Ch 4.1 – 4.2) p Pointers p *(asterisk) and &(ampersand) operators  Dynamic Variables and new Operator p Dynamic Arrays and Dynamic Objects p Stack (local) vs. heap (dynamic) memory  Garbage Collection and delete Operator p Parameters revisited p Pointers and Arrays as Parameters

@ Zhigang Zhu, Pointers and Arrays as Parameters p Value parameters that are pointers p Array parameters p Pointers and arrays as const parameters p Reference parameters that are pointers

@ Zhigang Zhu, Value parameters that are pointers p Compare ordinary and pointer variables void print_int_42(int i) { cout << i<<endl ; i = 42 ; cout << i <<endl; } void set_int_42(int* i_ptr) { cout << *i_ptr <<endl; *i_ptr = 42 ; cout << *i_ptr <<endl; } Calling program: int m = 80; print_int_42(m); cout << m<<endl<<endl; set_int_42(&m); cout << m<<endl<<endl;

@ Zhigang Zhu, Array Parameters p Compare ordinary and Dynamic arrays Calling program: int ages[30]; make_all_20(ages, 30); void make_all_20(int data[ ], size_t size) { for (int i = 0 ; i< size; i++) { data[i] = 20; } - - An array parameter automatically treated as pointer to the first entry (– value or reference?) - - In the function prototype and implementation, size of the array is not specified inside bracket but by another parameter Calling program: int *ages; ages = new int[30] make_all_20(ages, 30);

@ Zhigang Zhu, Pointers or Array as const Parameters p to make sure they will not be changed Calling program: int *ages, *i_ptr; double aver_age; ages = new int [ 30 ];... aver_age = average(ages, 30); i_ptr = &ages[12]; // i_ptr = (ages+12); if (is_20(i_ptr)) cout <<“Sudent No. 13 is 20!”<<endl; Protoptyes: bool is_20(const int* i_ptr); double average(const int data[ ], size_t size);

@ Zhigang Zhu, Reference Parameters that are Pointers p if we want to change the pointer to a new location Calling program: int *ages; int jone = 20; // assume &jone is 904 now ages = &jone; cout << “address that ages points to is ”<< ages<<endl; allocate_int_array(ages, 30); cout << “address that ages points to is ”<< ages<<endl; void allocate_int_arrary(int* i_ptr, size_t size) { i_ptr = new int[size]; } X

@ Zhigang Zhu, Reference Parameters that are Pointers p if we want to change the pointer to a new location Calling program: int *ages; int jone = 20; // assume &jone is 904 now ages = &jone; cout << “address that ages points to is ”<< ages<<endl; allocate_int_array(ages, 30); cout << “address that ages points to is ”<< ages<<endl; void allocate_int_arrary(int*& i_ptr, size_t size) { i_ptr = new int[size]; } V

@ Zhigang Zhu, Reference Parameters that are Pointers p if we want to change the pointer to a new location Calling program: int *ages; int jone = 20; // assume &jone is 904 now ages = &jone; cout << “address that ages points to is ”<< ages<<endl; allocate_int_array(ages, 30); cout << “address that ages points to is ”<< ages<<endl; typedef int* integer_ptr; void allocate_int_arrary(integer_ptr& i_ptr, size_t size) { i_ptr = new int[size]; } V

@ Zhigang Zhu, Reading and Programming Assignments p Reading before the next lecture p Chapter 4. Sections p Programming Assignment 2 p Detailed guidelines online! p Due Feb 24 (Thursday)