Announcements assign due tonight No late submissions Labs start this week Very helpful for assign1
Goals for Today Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic
Pointers In C++, from 16B/X Linked lists, trees, graphs Shared data Also in C Arrays, strings Pass by reference
Pointer Operators int i = 42, j = 17; int *p = &i; int *q = &j; & -- address-of & on int gives int * Address Memory 94 916 9 98 94 17 9 42 q p j i
Pointer Operators int i = 42, j = 17; int *p = &i; int *q = &j; printf( %d\n, *p); // 42 & -- address-of & on int gives int * * -- dereference * on int * gives int Address Memory 94 916 9 98 94 17 9 42 q p j i
Exercise int i = 42, j = 17; int *p = &i; int *q = &j; // What do these lines do? p = q; // (1) *p = *q; // (2) *p = q; // (3) p = *q; // (4) Address Memory 94 916 9 98 94 17 9 42 q p j i
Exercise (1a) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 9 p // What do these lines do? p = q; // (1) 98 94 17 j 9 42 i
Exercise (1b) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 94 p // What do these lines do? p = q; // (1) 98 94 17 j 9 42 i p and q are aliases
Exercise (2a) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 9 p // What do these lines do? *p = *q; // (2) 98 94 17 j 9 42 i
Exercise (2b) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 9 p // What do these lines do? *p = *q; // (2) 98 94 17 j 9 17 i
Exercise (3a) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 9 p // What do these lines do? *p = q; // (3) 98 94 17 j 9 42 i
Exercise (3b) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 9 p // What do these lines do? *p = q; // (3) 98 94 17 j 9 94 i warning: assignment makes integer from pointer without a cast
Exercise (4a) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 9 p // What do these lines do? p = *q; // (4) 98 94 17 j 9 42 i
Exercise (4b) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 17 p // What do these lines do? p = *q; // (4) 98 94 17 j 9 42 i warning: assignment makes pointer from integer without a cast
Exercise (4c) int i = 42, j = 17; int *p = &i; int *q = &j; // What do these lines do? p = *q; // (4) printf( %d\n, *p); // seg fault Address Memory 94 916 17 98 94 17 9 42 q p j i
Exercise int i = 42, j = 17; int *p = &i; int *q = &j; p = q; // (1) *p = *q; // (2) *p = q; // (3) p = *q; // (4) Address Memory 94 916 9 98 94 17 9 42 q p j i
Uninitialized Pointers int i; int *p; // How bad are these? printf( %d\n, i); printf( %d\n, *p); Address Memory???? 94 9???? p i
Uninitialized Pointers int i; int *p; // How bad are these? printf( %d\n, i); // unpredictable, no crash printf( %d\n, *p); Address Memory???? 94 9???? p i
Uninitialized Pointers int i; int *p; // How bad are these? printf( %d\n, i); // unpredictable, no crash printf( %d\n, *p); // probably crash Address Memory???? 94 9???? p i
NULL int *p = NULL; printf( %d\n, *p); // seg fault if (p == NULL) { // ok } Address 9 Memory p
Pointee Types int i = 5; double d = 2.5; int *ip = &i; double *dp = &d; dp = ip; warning: assignment from incompatible pointer type
Allocating Memory So far: the stack Lasts until function returns Automatically cleaned up Now: the heap Recall new from C++
malloc malloc(size) Argument is number of bytes needed Returns pointer to space i nheap Memory stays around until freed
malloc malloc(size) Argument is number of bytes needed Returns pointer to space i nheap Memory stays around until freed sizeof(type) Number of bytes a given type needs
malloc and free malloc(size) Argument is number of bytes needed Returns pointer to space i nheap Memory stays around until freed sizeof(type) Number of bytes a given type needs free(ptr) Mark memory as no longer in use
Using malloc and free int *p = malloc(sizeof(int)); *p = 17; Address 9 Memory 2 p 2 17
Using malloc and free int *p = malloc(sizeof(int)); *p = 17; free(p); Address 9 Memory 2 p 2
Using malloc and free int *p = malloc(sizeof(int)); *p = 17; free(p); // BUGGY printf( %d\n, *p); *p = 35; free(p); Address 9 Memory 2 p 2
Using malloc and free int *p = malloc(sizeof(int)); *p = 17; int *q = p; free(p); free(q); // BUGGY Address 98 9 Memory 2 q 2 p 2
Arrays? Address int *p = malloc(4 * sizeof(int)); 9 Memory 2 p 2
Arrays? Address int *p = malloc(4 * sizeof(int)); // What if we could do p[] = 2; p[1] = 4; 9 p[2] = 6; p[3] = 8; Memory 2 p 2
Arrays! Address int *p = malloc(4 * sizeof(int)); p[] = 2; p[1] = 4; p[2] = 6; p[3] = 8; 9 Memory 2 p 2 8 6 4 2
Arrays Address int *p = malloc(4 * sizeof(int)); p[] = 2; p[1] = 4; p[2] = 6; p[3] = 8; 9 Memory 2 p 2 212 8 2 28 6 2 24 4 2 2
Pointer Arithmetic Address int *p = malloc(4 * sizeof(int)); p[] = 2; p[1] = 4; p[2] = 6; p[3] = 8; int *q = p + 2; Memory 98 2 9 2 212 8 2 28 6 2 24 4 2 2 q p
Pointer Arithmetic Address Memory int *p = malloc(4 * sizeof(int)); 28 q p[] = 2; p[1] = 4; 98 p[2] = 6; p[3] = 8; 2 p int *q = p + 2; printf( %d\n, *q); // 6 9 2 212 8 2 28 6 2 24 4 2 2
Pointer Arithmetic Address Memory int *p = malloc(4 * sizeof(int)); 28 q p[] = 2; p[1] = 4; 98 p[2] = 6; p[3] = 8; 2 p int *q = p + 2; printf( %d\n, *q); // 6 // p[2] == *(p + 2) 9 2 212 8 2 28 6 2 24 4 2 2
Pointer Arithmetic Address Memory int *p = malloc(4 * sizeof(int)); 28 q p[] = 2; p[1] = 4; 98 p[2] = 6; p[3] = 8; 2 p int *q = p + 2; printf( %d\n, *q); // 6 9 2 212 8 // p[2] == *(p + 2) // p[] == *(p + ) == *p 2 28 6 2 24 4 2 2
Pointer Arithmetic Address Memory int *p = malloc(4 * sizeof(int)); 28 q p[] = 2; p[1] = 4; 98 p[2] = 6; p[3] = 8; 2 p int *q = p + 2; printf( %d\n, *q); // 6 9 2 212 8 // p[2] == *(p + 2) // p[] == *(p + ) == *p 2 28 6 // &p[2] == p + 2 2 24 4 // &p[] == p 2 2
Subarrays Address Memory int *p = malloc(4 * sizeof(int)); 28 q p[] = 2; p[1] = 4; 98 p[2] = 6; p[3] = 8; 2 p int *q = p + 2; printf( %d\n, q[1]); // 8 9 2 212 8 2 28 6 2 24 4 2 2
Stack Arrays int arr[4]; arr[] = 5; arr[1] = 1; arr[2] = 15; arr[3] = 2; // arr == &arr[] Address Memory 912 2 98 94 15 1 9 5 arr 2
Strings Recall char * from Friday Called C string Pointer to character(s) How does printf know when to stop printing? \ (null terminator) at end char *str = hello ; str 1 h e l l o \
Summary Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic Lab: Strings Next time: More pointer uses Pass by reference, stack vs. heap