Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation."— Presentation transcript:

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

2 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 3/2/2016 ECE 264: Lecture 24 2

3 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 24 33/2/2016

4 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 24 43/2/2016

5 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 3/2/2016 ECE 264: Lecture 24 5

6 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 24 63/2/2016

7 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 3/2/2016 4 bytes allocated for single int variable 160 bytes allocated for 20 double variables

8 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, 3 5 6 7 8 9 10 11 5 5 5 5 5 Why? 3/2/2016

9 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 24 93/2/2016

10 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 24 103/2/2016

11 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 24 113/2/2016

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

13 ECE 264: Lecture 24 13 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; 3/2/2016

14 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. 3/2/2016 ECE 264: Lecture 24 14


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

Similar presentations


Ads by Google