1 / 23

CISC220 Spring 2010 James Atlas

Learn how to comfortably use pointers, call functions by value or reference, and allocate and deallocate memory in CISC220 Lecture 02.

gfowler
Download Presentation

CISC220 Spring 2010 James Atlas

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. CISC220Spring 2010James Atlas Lecture 02: Pointers, Functions, Memory Management

  2. Objectives for Today • Use Pointers comfortably • Call functions • by value, by reference • Allocate and de-allocate memory • Reading - K+W P.7-P

  3. Location (address) 104 101 102 103 104 105 ... ... ... x y 23 42 name Pointers • An address refers to a particular memory location. In other words, it points to a memory location. • Pointer: A variable that contains the address of a variable. z

  4. x y x y x y ? ? ? 3 3 Pointers • How to initialize a pointer: • can initialize to NULL (i.e. not currently pointing to anything) • &operator: get address of a variable int *x; int y = 3; x = &y;

  5. x y x y 5 3 Pointers • How to get the value that is pointed to? • * "dereference operator”: get value pointed to • * x returns 3 • How to change the variable pointed to? • Use dereference * operator to left of = *x = 5;

  6. Functions f x f(x)

  7. Call-by-value int sum(int x, int y) { return x + y; } int main(void) { cout << sum(5,6) << endl; }

  8. Call-by-reference int sum(const int& x, const int& y) { return x + y; } int main(void) { cout << sum(5,6) << endl; }

  9. Memory Allocation • Two ways: • On the stack • On the heap

  10. Memory Allocation (Stack) int main(void) { int x(5); if (x > 3) { int y(6); cout << (x + y) << endl; } }

  11. Memory Allocation (Heap) int main(void) { int *x = new int(5); if (*x > 3) { int *y = new int(6); cout << (*x + *y) << endl; } }

  12. Memory De-allocation (Heap) int main(void) { int *x = new int(5); if (*x > 3) { int *y = new int(6); cout << (*x + *y) << endl; delete y; } delete x; }

  13. Array Allocation (Heap) int main(void) { int *x = new int[5]; *x = 5; if (*x > 3) { int *y = new int(6); cout << (*x + *y) << endl; delete y; } delete [] x; }

  14. Group Review • Break into teams of 4-5 students • One person write an official answer • Swap answers with a neighboring group and keep scores!

  15. Question 1 • What is output from the following code: double x = 5.5; double *px = &x; cout << *px << endl; *px = 10.0; cout << x << endl;

  16. Question 2 • What is output from the following code: double x = 5.5; double y = 10.0; double* px, py; px = &x; py = &y; cout << *px << endl << *py << endl;

  17. Question 3 • What is output from the following code: double x = 5.5; double *px = &x; *px = 3.14; double& r = *px; r = 99.44; cout << x << endl;

  18. Question 4 • What is output from the following code: void swap(int x, int y) { int temp = x; x = y; y = temp; } int main(void) { int a = 0; int b = 5; swap(a,b); cout << a << endl; }

  19. Question 5 • Change the code to work correctly using references: void swap(int x, int y) { int temp = x; x = y; y = temp; } int main(void) { int a = 0; int b = 5; swap(a,b); cout << a << endl; }

  20. Question 6 • What is the value of temp after each assignment? char blocks[3] = {'A','B','C'}; char *ptr = &blocks[0]; char temp; /*1*/ temp = blocks[0]; /*2*/ temp = *(blocks + 2); /*3*/ temp = *(ptr + 1); ptr = blocks + 1; /*4*/ temp = *ptr; /*5*/ temp = *(ptr + 1);

  21. Question 7 • What is the value of temp after each assignment? char blocks[3] = {'A','B','C'}; char *ptr = blocks; char temp; /*1*/ temp = *++ptr; /*2*/ temp = ++*ptr; /*3*/ temp = *ptr++; /*4*/ temp = *ptr;

  22. Question 8 • Write code to reverse a string using only pointer arithmetic (no array accessors): int main(void) { char s[10] = "abcde"; for (int i = 0; s[i] != NULL; i++) { cout << s[i]; } reverseString(s); for (int i = 0; s[i] != NULL; i++) { cout << s[i]; } cout << endl; return 0; }

  23. Question 9 • What is output from the following code? int *x, *y, *z; x = new int[20]; x[10] = 0; y = x; x[10] = 5; cout << x[10] << ", " << y[10] << endl; x[10] = 15; z = new int[10]; for (int i = 0; i < 10; i++) z[i] = x[i]; z[10] = 25; cout << x[10] << ", " << y[10] << ", " << z[10] << endl;

More Related