Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1
Outline Static memory allocation Pointer variables Pointers Dynamic memory allocation new and delete garbage and dangling references Dynamic arrays Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 2
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. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 3
Static Memory Allocation Exactly how memory is addressed also depends on the architecture. E.g., byte, 16-bit word, 32-bit word. Will assume byteaddressable, 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. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 4
Static Memory Allocation 0x00001220 i 0x0000122a d 0x00001221 3 0x00001222 0x00001223 0x00001224 j 2.5 4 0x00001228 0x00001229 'x' 'y' a b 0x00001232 e 3.6 address variable name Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 5
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. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 6
Pointer Variables 0x0000122a d 0x0000123a iptr1 0x0000124a dptr1 2.5 0x0000123e iptr2 0x0000124e dptr2 0x00001232 e 0x00001242 cptr1 3.6 0x00001246 cptr2 Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 7
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; dptr2 = &d; dptr2 = &e; As in C, the type of the pointer must match the type of the pointer variable. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 8
Pointers 0x0000122a 0x00001232 2.5 3.6 d e 0x0000123a 0x0000123e 0x00001242 0x00001246 iptr1 iptr2 cptr1 cptr2 0x0000124a 0x0000124e 0x1232 0x1220 0x1224 0x1228 0x1229 0x122a dptr1 dptr2 Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 9
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) Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 10
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. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 11
Using Pointers What happens when the following code is executed? // (a) pointee assignment *iptr1 = *iptr2; // (b) pointer variable assignment iptr1 = iptr2; Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 12
Using Pointers iptr1 XX 3 4 i (b) (a) iptr2 4 j 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. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 13
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 Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 14
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 runtime). Dynamically allocated memory comes from a heap, usually located at the other end of memory from the stack. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 15
Dynamic Memory Allocation In C++, dynamic memory allocation is done using the new operator. (DO NOT use malloc in this course.) 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; Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 16
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. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 17
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; Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 18
Dynamic Memory Deallocation iptr1 iptr2 Note delete deallocates the anonymous pointee, not the named pointer variable, as shown above. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 19
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 iptr2 XX garbage dangling references Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 20
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] Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 21
Array Addressing Back up a bit. Here is a statically allocated array: int array[5]; array [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. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 22
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. Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 23
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 Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 24