lecture 15
play

Lecture 15 No in-class files today. Reminder: Project 3 due on - PowerPoint PPT Presentation

Lecture 15 No in-class files today. Reminder: Project 3 due on Friday. Homework 4 posted, due next Monday. Questions? Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 1 Outline Static memory allocation


  1. Lecture 15  No in-class files today.  Reminder: Project 3 due on Friday.  Homework 4 posted, due next Monday.  Questions? Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 1

  2. Outline  Static memory allocation  Pointer variables  Pointers  Dynamic memory allocation  new and delete  garbage and dangling references  Dynamic arrays Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 2

  3. Static Memory Allocation  If we want to implement a class like vector<T>, we need dynamically allocated memory; otherwise the data structure could not grow indefinitely  Consider the following declarations int i = 3, j = 4; char a = 'x', b = 'y'; double d = 2.5, e = 3.6;  The sizes of these variables depends on the architecture, but often 4, 1, and 8 or 10 bytes. Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 3

  4. Static Memory Allocation  Exactly how memory is addressed also depends on the architecture. E.g., byte, 16-bit word, 32-bit word. Will assume byte- addressable, 32-bit words. Addresses will be written in hexadecimal (base 16) => 8 digits.  Statically allocated memory is allocated at compile-time before the program runs. It is allocated from a stack , which is usually at one end of memory. Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 4

  5. Static Memory Allocation 0x00001220 i 0x0000122a d 0x00001221 3 0x00001222 5 . 0x00001223 2 0x00001224 j 4 0x00001228 a 'x' 0x00001232 e 'y' b 0x00001229 6 . 3 address variable name Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 5

  6. Pointer Variables  Pointer variables are regular variables and also are allocated at compile-time.  Consider the following additional declarations: // note the repeated *'s int *iPtr1, *ptr2; char *cPtr1, *cPtr2; double *dPtr1, *dPtr2;  Pointers are usually the same size as an int; 4 bytes in our example. Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 6

  7. Pointer Variables 0x0000122a d 0x0000123a iPtr1 0x0000124a dPtr1 5 . 2 0x0000123e iPtr2 0x0000124e dPtr2 0x00001242 cPtr1 0x00001232 e 6 . 3 0x00001246 cPtr2 Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 7

  8. Pointers  What is a pointer? An address.  How do we get an address? One way is to use the & operator to get the address of a variable. E.g., iPtr1 = &i; // iPtr1 points to i iPtr2 = &j; cPtr1 = &a; cPtr2 = &b;  As in C, the type of the pointer must match the type of the pointer variable. Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 8

  9. Pointers 0x0000122a d 0x0000123a iPtr1 0x0000124a dPtr1 0x122a 0x1220 2.5 0x0000123e iPtr2 0x0000124e dPtr2 0x1224 0x1232 0x00001242 cPtr1 0x00001232 e 0x1228 3.6 0x00001246 cPtr2 0x1229 Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 9

  10. Pointers  Generally, we do not care about the actual address. Represent a pointer as an arrow starting in a pointer variable and ending at the "pointee" (i.e., the thing being pointed to) iPtr1 i pointer pointer variable "pointee" (i.e., the thing being pointed to) Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 10

  11. Using Pointers  How do we access the "pointee"? Use the unary * operator to dereference the pointer variable. E.g. double f = *dPtr2; // set f to 3.6 cout << "iPtr1 points to " << *iPtr1 << endl; // displays 3  Note: need to be careful with char pointers, since C/C++ interprets them as null-terminated strings. Won't use them much. Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 11

  12. Using Pointers  What happens when the following code is executed? // (a) pointee assignment *iPtr1 = *iPtr2; // (b) pointer variable assignment iPtr1 = iPtr2; Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 12

  13. Using Pointers iPtr1 i XX 3 4 (b) (a) iPtr2 j 4  Can test two pointer variables for equality (==, point to same location) or inequality (!=).  E.g. before statement (b) is executed, iPtr1 == iPtr2 is false, but afterwards in the situation shown above, iPtr == iPtr2 is true. Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 13

  14. Using Pointers  Special value to represent not pointing to anything. Called null pointer and is written as 0 (not NULL). E.g. iPtr1 = 0;  Drawn in a few different ways: iPtr1 iPtr2 Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 14

  15. Dynamic Memory Allocation  Pointing to an already existing variable is not very interesting. Power of pointers comes with dynamically allocated memory. I.e., memory allocated during program execution (aka run- time).  Dynamically allocated memory comes from a heap , usually located at the other end of memory from the stack. Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 15

  16. Dynamic Memory Allocation  In C++, dynamic memory allocation is done using the new operator. It has the following syntax new <type>;  This allocates an anonymous variable for one item of the given type and returns the address of (i.e., pointer to) that piece of memory. E.g., iPtr1 = new int; iPtr2 = new int; Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 16

  17. Dynamic Memory Allocation iPtr1 iPtr2  The anonymous variable is just like a statically allocated one, except that it has no name, so the only way to access the "pointee" is through dereferencing a pointer variable that points to it. Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 17

  18. Dynamic Memory Deallocation  After the program is done using an anonymous variable, it needs to deallocate the variable. I.e., give it back to the system.  This is done using the delete operator, which has syntax: delete <pointer var>; E.g. delete iPtr1; // dealloc. the pointee delete iPtr2; Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 18

  19. Dynamic Memory Deallocation iPtr1 iPtr2  Note delete deallocates the anonymous pointee, not the named pointer variable, as shown above. Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 19

  20. Garbage & Dangling References  Note that if we execute iPtr1 = iPtr2; before doing the deletes, we lose the pointer to the first allocated space and cannot deallocate it. This is called a memory leak or garbage . And the fact that both iPtr1 and iPtr2 point to deallocated space is called dangling references . iPtr1 XX garbage dangling references iPtr2 Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 20

  21. Dynamic Arrays  Dynamically allocating single variables is not very interesting either. The real power is in dynamically allocating arrays. C++ syntax is: <type> *ptr = new <type>[<size>];  The size can be any positive integer expression including a variable. E.g. cin >> n; // assume user enters 5 int *intArrPtr = new int[n]; intArrPtr [0] [1] [2] [3] [4] Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 21

  22. Array Addressing  Back up a bit. Here is a statically allocated array: array int array[5]; [0] [1] [2] [3] [4]  As noted before, the name of an array is the address of the first element of the array. Also, pointer arithmetic is scaled: (array+0) // address of array[0] (array+1) // address of array[1] (array+2) // address of array[2] // etc. Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 22

  23. Array Addressing  We can dereference these addresses to access the elements, meaning that: *(array+0) == array[0] *(array+1) == array[1] // etc.  This also is true for dynamic arrays using the pointer variable to provide the pointer to the first element of the dynamic array. *(intArrPtr+0) == intArrPtr[0] *(intArrPtr+1) == intArrPtr[1] // etc. Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 23

  24. Dynamic Arrays  Thus the syntax for accessing a dynamic array is exactly the same as for a static array. E.g., for (int i = 0; i < n; i++) intArrPtr[i] = 0;  Can even say intArrPtr = array;  Must tell the compiler when deallocating an array by giving [ ] after delete . E.g., delete [] intArrPtr;  Note: the [ ] is empty, and delete must match up with corresponding new Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 24

Recommend


More recommend