ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Advertisements

ECE 264 Object-Oriented Software Development
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
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.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
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.
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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CS352-Week 2. Topics Heap allocation References Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Pointers OVERVIEW.
1 8. Run-Time Arrays — Intro. to Pointers For declarations like double doubleVar; char charVar = 'A'; int intVar = 1234; the compiler____________the object.
Object-Oriented Programming in C++
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
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.
Pointers and Dynamic Memory Allocation. Declaring a pointer.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
ECE Application Programming
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
Object Oriented Programming COP3330 / CGS5409.  Aggregation / Composition  Dynamic Memory Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 18: More on inheritance and Polymorphism.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 16: Destructors, Copy Constructors and Exam 2 Review.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
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 Dynamic Memory Allocation
ECE 264 Object-Oriented Software Development
Chapter 9: Pointers.
Introduction to Programming
Dynamic Memory Allocation
An Introduction to Pointers
Object Oriented Programming COP3330 / CGS5409
Pointers and dynamic objects
Dynamic Memory.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Standard Version of Starting Out with C++, 4th Edition
Dynamic allocation (continued)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Pointers and dynamic objects
Pointers and References
Pointers and References
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Presentation transcript:

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation

Lecture outline Announcements / reminders  Contact your project groups—design due 11/19  Meeting with project group next Wednesday in my office (during the lab session) regarding your project questions and design 2:00-2:15 pm meeting with group 1 2:15-2:30 pm meeting with group 2 2:30-2:45 pm meeting with group 3 2:45-3:00 pm meeting with group 4  Exam 2: 11/12  Lab 7 due Tuesday, 11/06 Today  Review: pointers Basic usage Pointer arithmetic Common problems  Continue with pointers Dynamic memory allocation Destructors 12/15/2015 ECE 264: Lecture 24 2

Refresher on pointers Allocators ( malloc, new ) return pointer to allocated space  Pointer: address of another object We implicitly use these when we pass function arguments by reference in C++ Can get address of existing object using & Can get value of existing pointer using * Pointer declaration: *  Base type determines how reference is interpreted  Be careful when declaring multiple pointers  Be sure to initialize pointer before use ECE 264: Lecture /15/2015

Refresher on pointers, pt. 2 Can assign pointers to one another; e.g. int x, *xp, *ip; xp = &x; ip = xp; Array/pointer duality  Array name is pointer to first element Can dereference array name ( *arr )  Can treat pointers like arrays ( ptr[i] )  Can perform pointer arithmetic on both If ptr and arr point to first element of same array  ptr + i = arr + i = address of element arr[i]  ptr++  moves pointer to next array element  arr++  illegal: can’t change array name ECE 264: Lecture /15/2015

Dynamic memory allocation Up until now, allocated memory statically  Assumed we knew data size at compile time What if data size is input-dependent and unknown until run time?  In C, dynamic memory allocation handled through malloc and free  In C++, we use new and delete  Allocator ( new ) returns pointer to allocated space 12/15/2015 ECE 264: Lecture 24 5

Allocation with new In C, malloc() allocates space on heap  malloc(sizeof(int)) allocates space for 1 integer  malloc(20*sizeof(int)) allocates an array of 20 integers  Both calls return pointer to first byte of element In C++, we use new  new int allocates space for 1 integer  new int[20] allocates an array of 20 integers  As with malloc(), new returns pointer to first byte Directly initialize with value in parentheses  e.g. new int(3) ECE 264: Lecture /15/2015

Example int *iPtr; iPtr = new int;// 4 bytes are allocated // iPtr points to 1 st byte double *dPtr; dPtr = new double[20]; // 160 bytes allocated // dPtr points to 1 st byte ECE 264: Lecture 24 7 ? ? ? ? ? … ? heap iPtr dPtr 12/15/ bytes allocated for single int variable 160 bytes allocated for 20 double variables

Dynamic allocation example int main() { int *iPtr, *jPtr, i; iPtr = new int; jPtr = new int(3); double *dPtr; dPtr = new double[6]; *iPtr = 7; cout << *iPtr << ',' << *jPtr << endl; for(i=0; i<6; i++) dPtr[i] = 5; for(i=0; i<6; i++) cout << (*dPtr)++ << ' '; cout << endl; for(i=0; i<6; i++) cout << dPtr[i] << ' '; return 0; } ECE 264: Lecture 24 8 OUTPUT 7, Why? 12/15/2015

Dynamically allocated objects May want to dynamically allocate objects  Ex. array of Points where size is unknown at compile time Point *pointArray; int numPoints; cin << numPoints; pointArray = new Point[numPoints];  Ex. linked list data structure—to add an element to the list, must allocate new element class linkedList { private: int elem; linkedList *next; public: void addElement(int i)... } void linkedList::addElement(int i) { next = new linkedList(i); } ECE 264: Lecture /15/2015

Referencing objects through pointers Recall: use dot operator (.) to reference members of object, e.g: Point p1; p1.setX(2); With pointers, use -> linkedList *list1 = new linkedList; list1->addElement(2); ECE 264: Lecture /15/2015

Deallocation with delete Space allocated using new should be freed  In C, we used free()  In C++, we use delete  You should only use delete to free memory allocated by new  Any other use will result in an error ECE 264: Lecture /15/2015

ECE 264: Lecture delete example int *ptr; ptr = new int (100); 100 ptr delete ptr;//free the memory ? ptr 12/15/2015 delete frees space on heap but ptr still points to same address!  Solution: assign freed pointers to NULL: ptr = NULL;

ECE 264: Lecture Example: delete with arrays double *dptr; const int SIZE = 10; dptr = new double[SIZE];//80 bytes for(int i=0; i<SIZE; ++i) cin >> dptr[i]; fun1(dptr, SIZE); // pass array to fun1 delete [] dptr; //free all 10 elements dptr = NULL; 12/15/2015

Example: Dynamically resizing an array to resize this so that the array called list has space for 5 more numbers int * list = new int[size]; Create an entirely new array of the appropriate type and of the new size. int * temp = new int[size + 5]; Copy the data from the old array into the new array (keeping them in the same positions). This is easy with a for-loop. for (int i = 0; i < size; i++) temp[i] = list[i]; Delete the old array -- you don't need it anymore! delete [] list; // this deletes the array pointed to by "list" Change the pointer. You still want the array to be called "list" (its original name), so change the list pointer to the new address. list = temp; 12/15/2015 ECE 264: Lecture 24 14

Example of pointer arguments void Swap(int *p1, int *p2); void main () { int x, y; cin >> x >> y; cout << x << " " << y << endl; Swap(&x,&y); // passes addresses of x and y explicitly cout << x << " " << y << endl; } void Swap(int *p1, int *p2) { int temp = *p1; *p1 = *p2; *p2 = temp; } ECE 264: Lecture 24 15

Example of reference arguments void Swap(int &a, int &b); void main () { int x, y; cin >> x >> y; cout << x << " " << y << endl; Swap(x,y); // passes addresses of x and y implicitly cout << x << " " << y << endl; } void Swap(int &a, int &b) { int temp = a; a = b; b = temp; } ECE 264: Lecture 24 16

More example ECE 264: Lecture void main () { int r, s = 5, t = 6; int *tp = &t; r = MyFunction(tp,s); cout << r << "," << s << "," << t << endl; r = MyFunction(&t,s); cout << r << "," << s << "," << t << endl; r = MyFunction(&s,*tp); cout << r << "," << s << "," << t << endl; } int MyFunction(int *p, int i) { *p = 3; i = 4; return i; } Output (r, s, t) 4, 5, 3 4, 3, 3

Final notes Next time  Dynamic allocation code examples Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 12/15/2015 ECE 264: Lecture 24 18