Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call 

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 3-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
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.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Microsoft Visual C++.NET Chapter 61 Memory Management.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
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.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Slide 1 Chapter 10 Pointers and Dynamic Arrays. Slide 2 Learning Objectives  Pointers  Pointer variables  Memory management  Dynamic Arrays  Creating.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers and Dynamic Arrays
Chapter 10 Pointers and Dynamic Arrays. Learning Objectives Pointers – Pointer variables – Memory management Dynamic Arrays – Creating and using – Pointer.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Static arrays.
Integer Types short month; // half of a machine word int car; // one machine word unsigned long distance; Character Types char c = ‘\0’; // one byte char.
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”
Review 1 List Data Structure List operations List Implementation Array Linked List.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Chapter 12 Pointers and Dynamic Arrays. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Pointers (12.1) Dynamic Arrays.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
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.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
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.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
Pointers and Dynamic Arrays
Pointers and Dynamic Arrays
Student Book An Introduction
COMP 2710 Software Construction Pointers
Pointers Revisited What is variable address, name, value?
Values – also known as scalars (eg. int, double, char, float)
Dynamic Memory Allocation
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers and Dynamic Variables
Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call  Dynamic variable  new keyword and delete keyword  Arrays and pointers  Pointer arithmetic  Dynamic array  Size not specified at programming time  Determined while program running

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Modify C-String in a function call or exchange the value of two C- Strings  Pass by value of the c-string (char *)?  Won’t’ work  Pass by the reference of the c-string (char * &)?  Yes, it works  Pass by pointer to the c-string (char **)?  Yes, it works  How to implement?

Copyright © 2006 Pearson Addison-Wesley. All rights reserved The new Operator  Since pointers can refer to variables…  No "real" need to have a standard variable  Can dynamically allocate variables  Operator new creates variables int * p1; p1 = new int;  Creates new "nameless" variable, and assigns p1 to "point to" it  Can access with *p1  Use just like ordinary variable

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Basic Pointer Manipulations Example: Display 10.2 Basic Pointer Manipulations (1 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Basic Pointer Manipulations Example: Display 10.2 Basic Pointer Manipulations (2 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Memory Management  Heap  Reserved for dynamically-allocated variables  All new dynamic variables consume heap memory  If too many  could use all heap memory  Problem: future "new" operations will fail if heap is "full“  Resolution: use delete operator to de-allocate spaces

Copyright © 2006 Pearson Addison-Wesley. All rights reserved delete Operator  De-allocate dynamic memory  When no longer needed  Returns memory to heap  Example: int *p; p = new int; … //Some processing… delete p;  De-allocates dynamic memory "pointed to by pointer p"  Literally "destroys" memory

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Dynamic and Regular Variables  Dynamic variables  Created with new operator; destroyed with delete operator  Created and destroyed while program runs  Local variables  Declared within function definition  Not dynamic  Created when function is called  Destroyed when function call completes

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Recall: Array Variables  Arrays stored in memory addresses sequentially  Array variable "refers to" first indexed variable  So array variable is a kind of pointer variable!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Array Variables  Pointers  Example: int a[10]; int * p;  and p are pointer variables  Can perform assignments: p = a;// Legal.  p now points where a points  To first indexed variable of array a  a = p;// ILLEGAL!  Array pointer is CONSTANT pointer!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Pointer Arithmetic  Can perform arithmetic on pointers  "Address" arithmetic  Example: double f[4] = {1.1, 2.2, 3.3, 4.4}; double * d = f;  d contains address of d[0]  d + 1 evaluates to address of d[1]  d + 2 evaluates to address of d[2]  Equates to "address" at these locations

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Alternative Array Manipulation  Use pointer arithmetic!  “Step through” array without indexing: for (int i = 0; i < arraySize; i++) cout << *(d + i) << " " ;  Equivalent to: for (int i = 0; i < arraySize; i++) cout << d[i] << " " ;

Array and Pointers Copyright © 2006 Pearson Addison-Wesley. All rights reserved  Array can be accessed using pointers; passing array as a parameter in a function is passing the address of the first element int main() { int n[4]={0,0,0,0}; int *p; p=&n[0]; // same as p =n; *p = 1; // same as p[0] = 1; *(p+1) = 1 // same as p[1] = 1; for (int i=0; i<4; i++) cout << n[i] << " "; return 0; }

Array and Pointers (continued) Copyright © 2006 Pearson Addison-Wesley. All rights reserved C-String is a special character array int main() { char s[] = "Hi There!"; char *p; p=&s[0]; // same as p = s; *p = 'B'; // what will happen here? *(p+1) = ‘C’; // what will happen here? p = "Bye Bye!"; cout << s << endl; return 0; }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Dynamic Arrays  Array limitations  Must specify size first  May not know until program runs!  Must "estimate" maximum size needed  Sometimes OK, sometimes not  "Wastes" memory  Dynamic arrays  Can determine size at runtime

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Creating Dynamic Arrays  Use new operator  Dynamically allocate with pointer variable  Treat like standard arrays  Example: double *d; d = new double[10]; //Size in brackets, can be variable  Creates dynamically allocated array variable d,with ten elements, base type double

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Deleting Dynamic Arrays  Allocated dynamically at run-time  So should be destroyed at run-time  Simple again. Recall Example: double * d = new double[10]; … //Processing delete [] d;  De-allocates all memory for dynamic array  Brackets indicate "array" is there

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Multidimensional Dynamic Arrays  Recall: "arrays of arrays" int **m = new int *[3];  Creates array of three pointers  Make each allocate array of 4 ints for (int i = 0; i < 3; i++) m[i] = new int[4];  Results in three-by-four dynamic array!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Multidimensional Dynamic Arrays: De-allocate memories  In the reverse order allocating spaces for (int i = 0; i < 3; i++) delete [] m[i]; delete[] m;