Pointers & Dynamic Arrays Shinta P.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 6 Data Types
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers1 Pointers & Dynamic Arrays Allocating memory at run-time.
Starting Out with C++, 3 rd Edition 1 Chapter 9 – Pointers.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
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.
Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 10: Pointers.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Pointer Data Type and Pointer Variables
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
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.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
1 CSC241: Object Oriented Programming Lecture No 05.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
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.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
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.
Dynamic Storage Allocation
Pointers Revisited What is variable address, name, value?
CSCE 210 Data Structures and Algorithms
Pointers and References
Dynamic Memory Allocation
Object Oriented Programming COP3330 / CGS5409
understanding memory usage by a c++ program
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Standard Version of Starting Out with C++, 4th Edition
Pointers and References
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Pointers & Dynamic Arrays Shinta P. Data Structures in C++ Pointers & Dynamic Arrays Shinta P.

Static and Dynamic Memory Static Allocation allocated by the compiler at compile time once allocated, does not change Dynamic Allocation allocated by program at run time ‘new’ allocates desired amount from heap amount depends on class/type ‘delete’ deallocates an object and returns to storage manager for reallocation

Where or When Static Dynamic data stricture sizes are fixed little chance for storage needs to grow prototypes Dynamic amount of data changes from run to run data relationships change frequently

Pointers a built-in primitive type; 32 bit used to hold the storage address of a variable to define a pointer variable use the * operator in the definition eg. int *airplane_ptr ; (airplane_ptr is a variable that will point to an integer)

Pointers (cont.) To assign a value to the pointer variable use the address operator & eg. int F15; int Airplane_ptr; Airplane_ptr = &F15; Airplane_ptr F15

Pointers (cont.) Note that F15 has not been assigned a value yet to do this we must use the dereferencing operator * ex. *Airplane_ptr = 5 (dereferencing * reads: location pointed to by var) 5 Airplane_ptr F15

Alamat=123f , 4byte Alamat=421f , 4byte Int nilai=50; int *pnilai; cout<<nilai; // cetak 50 cout<<&nilai; // 123f pnilai=&nilai; // pointer pnilai menunjuk alamat nilai cout<<pnilai; // 123f cout<<*pnilai; // 50 nilai =50 *pnilai=123f

char *head; char data1=‘A’; char data2=‘B’; head=&data1; head= &data2; Without pointer char head; char data1=‘A’; char data2=‘B’; head= data1; head= data2; head= ‘C’; cout<<data1; cout<<data2; char *head; char data1=‘A’; char data2=‘B’; head=&data1; head= &data2; *head= ‘C’; cout<<data1; cout<<data2; With pointer

char *head; char data1=‘A’; char data2=‘B’; head=&data1; head= &data2; Without pointer Data1 Data2 A Display: AB Head B char head; char data1=‘A’; char data2=‘B’; head= data1; head= data2; head= ‘C’; cout<<data1; cout<<data2; char *head; char data1=‘A’; char data2=‘B’; head=&data1; head= &data2; *head= ‘C’; cout<<data1; cout<<data2; With pointer

char *head; char data1=‘A’; char data2=‘B’; head=&data1; head= &data2; Without pointer Data1 Data2 A Display: AB Head C char head; char data1=‘A’; char data2=‘B’; head= data1; head= data2; head= ‘C’; cout<<data1; cout<<data2; char *head; char data1=‘A’; char data2=‘B’; head=&data1; head= &data2; *head= ‘C’; cout<<data1; cout<<data2; Display: AC With pointer

Pointers (cont.) ...so whats the big deal?? Which is exactly equivalent to: F15 = 5; ...so whats the big deal??

The Big Deal.... We’ve been looking at the trivial case Pointers to primitives aren’t very useful things get more interesting with arrays we can make : an array that grows as the application needs more room for data an array that shrinks as the application needs less room for data and much better with dynamic objects p1

More Pointers int i = 50; int j = 75; int *p1 ; int * p2 ; p1 = &i ; p2 = & j; cout << *p1; p1 = p2 ; *p2 =0; cout <<*p1; i j 50 75 p2 p1

More Pointers int i = 50; int j = 75; int *p1 ; int * p2 ; p1 = &i ; p2 = & j; cout << *p1; p1 = p2 ; *p2 =0; cout <<*p1; i j 50 Display: 500 p2 p1

int i = 50; int j = 75; int *p1 ; int * p2 ; p1 = &i ; p2 = & j; cout << *p1; p2 = p1 ; *p2 =0; cout <<*p1; i j 75 Display: 500 p2 p1

Pointers to arrays The name of an array is a pointer to the 0th element of the array (the beginning of the array) int array[5] ; // array is equivalent to & array[0] *array = 5; is like array[0] = 5; int j = *(array+4) is like int j = array[1] cout << *array; is like cout << array[0];

Pointers to arrays Pass by reference - in C++ arrays are always pass by reference (there is no pass by value for an array) this is a big improvement over C in C to pass an array to a function it had to be passed by passing a pointer to the beginning of the array then doing pointer arithmetic to manipulate the contentsd of the array

new returns the address of a piece of dynamically allocated storage ex. int *i; //create a pointer i = new int // get a new integer *i = 75 // assign it a value 75 i

Dynamic Arrays arrays can be allocated at run time double * p; int count ; cout << “how many elements? “ << “\n”; cin >> count; p = new double[count];

Dynamic Arrays You can effectively change the size of an array at run-time if it was originally allocated dynamically. … from previous example double * temp; temp = new double[20]; /* copy the contents of the old array into the new one */ for (int I=0 ; I < 10 ; I++) temp[I] = p[I]; /* dispose of the original array */ delete p; p = temp; /* now the array has twice as many elements */

Value semantics The value semantics of a class determine how values are copied from one object to another. In C++ the value semantics consist of two operations: the assignment operator the copy constructor The copy constructor is a constructor that creates and initializes an object to the value of another (existing) object the copy constructor has one parameter whose type is the same as the class name Ex. Date Today; Today.month=5 ; Today.year=2000; Today.day = 21; Date Tomorrow(Today)

Copy Constructor Date :: Date(const & Date t) { month = t.month; day = t.month; year = t.year; }