Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Introduction to Programming Lecture 39. Copy Constructor.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Pointers Applications
Data Structures Using C++ 2E
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structures Using C++ 2E
Microsoft Visual C++.NET Chapter 61 Memory Management.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
POINTERS.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
Dynamic Array Allocation char *ptr; // ptr is a pointer variable that // can hold the address of a char ptr = new char[ 5 ]; // dynamically, during run.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lecture.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Data Structures Using C++ 2E
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 4 Linked Lists.
Data Structures and Abstract Data Types
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Indirection.
Presentation transcript:

Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses in Memory Data Addresses in Memory Declaring Pointer Variables Declaring Pointer Variables Assigning Values to Pointers Assigning Values to Pointers Accessing Data with Pointers Accessing Data with Pointers Arrays and Pointers Arrays and Pointers Operator ‘new’ Operator ‘new’ Operator ‘delete’ Operator ‘delete’ Illustrating the Destructor Illustrating the Destructor Copy Constructor / Overloaded Assignment Operator Copy Constructor / Overloaded Assignment Operator Declaration of dynamicClass Objects Declaration of dynamicClass Objects Chapter 5 – Pointers and Dynamic Memory The Pointer ‘this’ The Pointer ‘this’ dynamicClass Copy Constructor dynamicClass Copy Constructor The C++ Index Operator [] The C++ Index Operator [] Matrices Summary Slides Summary Slides Summary Slides Summary Slides (3 pages)

Main Index Contents 22 Main Index Contents Pointer Illustration

Main Index Contents 33 Main Index Contents Vertical and Horizontal View of Memory

Main Index Contents 44 Main Index Contents Data Addresses in Memory

Main Index Contents 55 Main Index Contents Declare a pointer by stating the type followed by the variable name, but with a "*" added immediately before the name. The pointer ptr is a variable whose value is the address of a data item of the designated type. Declaring Pointer Variables int *intPtr; char *charPtr; type *ptr;

Main Index Contents 6 Assigning Values to Pointers &m is the address of the integer in memory. The assignment statement sets intPtr to point at an actual data item. The Figure illustrates the status of the two variables from the declaration statement and the resulting status after the assignment of a value to intPtr. intPtr = &m; int m = 50, *intPtr;

Main Index Contents 77 Main Index Contents Accessing Data with Pointers int x = 50, y = 100, *px = &x, *py = &y;

Main Index Contents 88 Main Index Contents Arrays and Pointers

Main Index Contents 99 Main Index Contents Operator ‘new’ p = new time24; // *p is 00:00 (midnight) q = new time24(8, 15); // *q is 8:15 AM

Main Index Contents 10 Operator ‘delete’ deallocating a dynamic array, use a slightly different form of delete. Place square brackets [] between delete and the pointer variable name. The system deallocates all of the memory originally assigned to the dynamic array. arr = new T[ARRSIZE]; // allocated space for ARRSIZE objects delete [] arr; // deallocate dynamic array storage

Main Index Contents 11 Main Index Contents Illustrating the Destructor

Main Index Contents 12 Copy Constructor / Overloaded Assignment Operator Initialization and Assignment both have the effect of producing a data item that is a copy of an existing item

Main Index Contents 13 Main Index Contents Declaration of dynamicClass Objects The byte-by-byte copy performed by default by C++ is inappropriate for dynamic classes The copy of an objA in ObjB makes two pointers point at the same memory location. This creates a memory leak when one of the two objects is deleted.

Main Index Contents 14 The Pointer ‘this’ *thisobjA; this-> memberl

Main Index Contents 15 Main Index Contents dynamicClass Copy Constructor Algorithm Example: dynamicClass obj(3,5), objB=objA;

Main Index Contents 16 The MiniVector Check the code of the MiniVector – A small version of the Vector Class available in STL The MiniVector uses a function called Reserve() to allocate a dynamic array for extra space

Main Index Contents 17 The C++ Index Operator [ ] arr[i] = 30; // arr[i] is the address into which 30 is copied t = arr[i] + 4; // add 4 to the value of the element at arr[i]

Main Index Contents 18 Main Index Contents Matrices A Matrix is a two-dimensional array that corresponds to a row-column table of entries of a specified data type. Matrices are referenced using a pair of indices that specify the row and column location in the table. Example: The element mat[0][3] is 2 The element mat[1][2] is 4.

Main Index Contents 19 Main Index Contents Vectors and Matrices A dynamic Matrix is represented by using the Vector Class. – Visit the matrix.h file A dynamic Matrix can be described by using dynamic arrays as well. The code is more complex than using vectors. ( Think!) - Food for you brain – How would you define a dynamic matrix by using a dynamic array?

Main Index Contents 20 Main Index Contents Summary Slide 1 §- Pointers contain the address of data in memory… -Data is accessed by applying the dereference operator * §- Operators such as +, ++, and += apply to pointers. §- With such operators, pointers can be used for algorithms involving array traversal, but their primary application is in the allocation and maintenance of dynamic memory.

Main Index Contents 21 Main Index Contents Summary Slide 2 §- vector implementation -The miniVector class illustrates the key points. 1)It allocates dynamic memory using: destructor copy constructor overloaded assignment operator 2)It implements push_back(): Therefore it must control vector capacity in order to minimize dynamic memory reallocation. 3)It allows access to elements by using an index: Therefore the class implements an overloaded index operator

Main Index Contents 22 Main Index Contents Summary Slide 3 §- Two dimensional arrays in C++ -have the same problems as one-dimensional arrays: 1)fixed size 2)no size attribute 3)If it is a function argument, it is necessary to specify the number of columns as a constant.