Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 10: 2/17/2003CS148 Spring 20031 CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.

Similar presentations


Presentation on theme: "Lecture 10: 2/17/2003CS148 Spring 20031 CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture."— Presentation transcript:

1 Lecture 10: 2/17/2003CS148 Spring 20031 CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 10: 2/17/2003

2 CS148 Spring 20032 Outline n Pointers and structs n Pointer expressions n Dynamic Data Chapter 15 (15.1-15.2)

3 Lecture 10: 2/17/2003CS148 Spring 20033 Pointers and Structs struct PatientRec { Int idNum; Int height; Int weight; }; PatientRec patient; // struct variable PatientRec* patientPtr = &patient; // pointer variable to a struct variable PatientRec* patPtrArray[20]; // array of pointers n *PatientPtr struct variable of type PatientRec n (*patientPtr).weight weight member of a struct of type PatientRec (dereferencing and member selection) n () necessary because dot operator has higher precedence than the dereference operator

4 Lecture 10: 2/17/2003CS148 Spring 20034 Pointers and Structs arrow operator (->) (member selection operator) PointerExpression->MemberName Equivalent to (*PointerExpression).MemberName (*PatientPtr).weight is equivalent to PatientPtr->weight

5 Lecture 10: 2/17/2003CS148 Spring 20035 Pointers and Structs Example patientPtr->idNum = 5555; patientPtr->height = 64; patientPtr->weight = 114; patPtrArray[3]->idNum = 6666; patPtrArray[3]->height = 80; patPtrArray[3]->weight = 185; 5555 patientPtr*patientPtr 64 114 6666 *patPtrArray[3] 80 185 patPtrArray [0] [1] [2] [3]

6 Lecture 10: 2/17/2003CS148 Spring 20036 Pointer Expressions n A pointer expression is a combination of pointer variables, pointer constants, operators and parentheses. n One literal pointer constant 0 intPtr = 0; //null pointer, points to nothing n For better program readability and documentation use named constant NULL

7 Lecture 10: 2/17/2003CS148 Spring 20037 Pointer Expressions n Array name without any index brackets considered to be constant pointer expression int arr[100]; int* ptr; ptr = arr; has same effect as ptr = &arr[0]; n Indexing a Pointer (pointer points to array) int numbers[30]; ZeroOut(numbers,30); void ZeroOut (int *arr, int size) { int i; for (i = 0; i < size ; i++) arr[i] = 0; }

8 Lecture 10: 2/17/2003CS148 Spring 20038 Pointer Expressions n Example (Pointer Assignment) int x = 10; int y = 20; int* ptr_x = &x; int* ptr_y = &y; 10 ptr_xx 20 ptr_yy 10 ptr_xx 20 ptr_yy ptrx = ptry;

9 Lecture 10: 2/17/2003CS148 Spring 20039 Summary of Pointer Operations n = Assignment ptr = &var n ptr1 = ptr2 n ptr = NULL n * Dereference *ptr n ! Logical NOT !ptr n (1 if ptr is null pointer) n ==, !=,, and >= n Relational operators ptr1 == ptr2 n -> Member selection ptr->height n Pointer arithmetic not covered here ptr++,ptr+5

10 Lecture 10: 2/17/2003CS148 Spring 200310 Pointers and function parameters n By Default, C++ passes function parameters by value n Pointers can be used to pass function parameters by reference n Can achieve the same using reference variables. Swap(alpha,beta); void Swap(int x, int y) { //this code does not work //copies of alpha and beta values are passed //local contents of x and y are swapped //actual parameters remain unchanged int temp = x; x = y; y = temp; } Swap(&alpha,&beta); void Swap(int* x, int* y) { //pass by reference //addresses of alpha and beta are passed //actual parameters changed int* temp = *x; *x = *y; *y = *temp; }

11 Lecture 10: 2/17/2003CS148 Spring 200311 Dynamic Data n Pointers used to manipulated dynamic variables n Dynamic variables explicitly allocated and deallocated during program execution n Two special operators new and delete n Variable Lifetime = time between execution of new and delete n Variables allocated from within a region of memory set aside for dynamic variables (Free Store) n Optimize the program’s memory consumption. The size of an array or a string of characters might not be known, or the maximum size might be very large compared to the actual used size.

12 Lecture 10: 2/17/2003CS148 Spring 200312 Dynamic Data Allocation/Deallocation Example #include int* intPtr = new int; char* name = new char[6]; intPtr name ? Free Store ?????? 05 intPtr name 400 Free Store ‘X’‘y’‘Z’‘\0’?? 05 undefined intPtr Free Store ‘X’‘y’‘Z’‘\0’?? 05name *intPtr = 400; strcpy(name,”XyZ”); delete intPtr;

13 Lecture 10: 2/17/2003CS148 Spring 200313 Dynamic Data Pitfalls int *ptr1 = new int; int *ptr2 = new int; *ptr2 = 60; ? ptr1*ptr1 60 ptr2*ptr2 *ptr1 = *ptr2; ptr1 = ptr2; delete ptr2; 60 ptr1*ptr1 60 ptr2*ptr2 60 ptr1 60 ptr2*ptr2 Inaccessible object (memory leak) 60 ptr1 (dangling pointer) ?? ptr2


Download ppt "Lecture 10: 2/17/2003CS148 Spring 20031 CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture."

Similar presentations


Ads by Google