1 / 13

CS148 Introduction to Programming II

CS148 Introduction to Programming II. Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 10: 2/17/2003. Outline. Pointers and structs Pointer expressions Dynamic Data Chapter 15 (15.1-15.2). Pointers and Structs. struct PatientRec { Int idNum; Int height;

aiko
Download Presentation

CS148 Introduction to Programming II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Outline • Pointers and structs • Pointer expressions • Dynamic Data Chapter 15 (15.1-15.2) CS148 Spring 2003

  3. 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 • *PatientPtr struct variable of type PatientRec • (*patientPtr).weight weight member of a struct of type PatientRec (dereferencing and member selection) • () necessary because dot operator has higher precedence than the dereference operator CS148 Spring 2003

  4. Pointers and Structs arrow operator (->) (member selection operator) PointerExpression->MemberName Equivalent to (*PointerExpression).MemberName (*PatientPtr).weight is equivalent to PatientPtr->weight CS148 Spring 2003

  5. patientPtr *patientPtr 5555 64 114 patPtrArray [0] *patPtrArray[3] 6666 [1] 80 [2] 185 [3] 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; CS148 Spring 2003

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

  7. Pointer Expressions • 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]; • 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; } CS148 Spring 2003

  8. ptr_x ptr_y ptr_y x y y ptr_x x 10 20 20 10 Pointer Expressions • Example (Pointer Assignment) int x = 10; int y = 20; int* ptr_x = &x; int* ptr_y = &y; ptrx = ptry; CS148 Spring 2003

  9. Summary of Pointer Operations • = Assignment ptr = &var • ptr1 = ptr2 • ptr = NULL • * Dereference *ptr • ! Logical NOT !ptr • (1 if ptr is null pointer) • ==, !=, <, <=, >, and >= • Relational operators ptr1 == ptr2 • -> Member selection ptr->height • Pointer arithmetic not covered here ptr++,ptr+5 CS148 Spring 2003

  10. Pointers and function parameters • By Default, C++ passes function parameters by value • Pointers can be used to pass function parameters by reference • 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; } CS148 Spring 2003

  11. Dynamic Data • Pointers used to manipulated dynamic variables • Dynamic variables explicitly allocated and deallocated during program execution • Two special operators new and delete • Variable Lifetime = time between execution of new and delete • Variables allocated from within a region of memory set aside for dynamic variables (Free Store) • 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. CS148 Spring 2003

  12. Free Store Free Store intPtr intPtr Free Store intPtr ? 400 undefined name name 0 0 5 5 name 0 ‘X’ ? ‘y’ ? 5 ‘Z’ ? ‘\0’ ? ? ? ? ? ‘X’ ‘y’ ‘Z’ ‘\0’ ? ? Dynamic Data Allocation/Deallocation Example #include <cstring> int* intPtr = new int; char* name = new char[6]; *intPtr = 400; strcpy(name,”XyZ”); delete intPtr; CS148 Spring 2003

  13. ptr1 ptr1 *ptr1 *ptr1 ? 60 ptr2 ptr2 *ptr2 *ptr2 60 60 Inaccessible object (memory leak) ptr1 ptr1 (dangling pointer) 60 60 ptr2 *ptr2 ptr2 60 ?? Dynamic Data Pitfalls int *ptr1 = new int; int *ptr2 = new int; *ptr2 = 60; *ptr1 = *ptr2; ptr1 = ptr2; delete ptr2; CS148 Spring 2003

More Related