CS50 SECTION: WEEK 4 Kenny Yu
Announcements Problem Set 4 Walkthrough online Problem Set 2 Feedback has been sent out CORRECTION: Expect all future problem set feedback before Thursday. Quiz 0 on Wed 10/12; details announced in lecture this week Previous year’s quizzes are available: Section resources: Section feedback poll: What do you want to review in section for Quiz 0 next week?
Agenda 1. RAM 2. Pointers 1. Declaring Pointers 2. Address Operator 3. Dereferencing Pointers 4. Pointer Assignment 5. WARNING 3. Arrays Revisited 4. Heap and Stack 5. Malloc
RAM (Random Access Memory) “Active” memory Imagine RAM as being a very very big array (on a 4GB RAM machine, you have 2^32 buckets) Each address represents one byte in memory. RAM (hex) … 0A05FF00 Address (dec): …
RAM (Random Access Memory) Whenever you declare a variable, space is allocated for it in RAM. The location of the variable in RAM is the variable’s address. RAM (hex) … 6105FF00 Address: … char c = ‘a’; c Address of c is 508.
Pointers Pointers are variables that store addresses. Pointer (value = address of Variable) Pointer (value = address of Variable) Variable (value = 5) Variable (value = 5) RAM (Random Access Memory)
Pointer Syntax > Declaring Pointers Declaring a pointer: var_type* pointer_name; Example: int* ptr; // ptr has type int*; // ptr points to int types
Pointer Syntax > Address Operator The & operator returns the address of a variable (the memory location of the variable in RAM, it is actually just an integer) Example: int x = 5; // declare and assign x’s value to 5 int* ptr; // declare a pointer to int types ptr = &x; // ptr’s value is the address of x (i.e. when ptr is dereferenced, it will return the value of x, which is 5)
Pointer Syntax > Dereferencing When we dereference a pointer, we retrieve the value of the variable at the address stored by the pointer. We dereference a pointer by using the * operator Example: int x = 5; // declare and assign x to 5 int* ptr; // declare a pointer to int types ptr = &x; // ptr contains the address of x int y = *ptr; // declare y and assign it to the value pointed by ptr. Now y and x are both 5.
Pointer Syntax > Assignment We can also change the value at the address pointed to by a pointer, using the * operator on the left side of an assignment Example: int x = 5; // declare and assign x to 5 int* ptr; // declare a pointer to int types ptr = &x; // ptr contains the address of x *ptr = 6; // change the value pointed to by ptr Now x is equal to 6, and if we dereference ptr, that will also return 6.
Putting it all together int x = 5; // declare and assign x to 5 RAM (Random Access Memory) x Type: int Address: Value: 5 x Type: int Address: Value: 5
Putting it all together int x = 5; // declare and assign x to 5 int *ptr; // declare a pointer to int types x Type: int Address: Value: 5 x Type: int Address: Value: 5 RAM (Random Access Memory) ptr Type: int * Address: Value: GARBAGE ptr Type: int * Address: Value: GARBAGE
Putting it all together int x = 5; // declare and assign x to 5 int *ptr; // declare a pointer to int types ptr = &x; // ptr contains the address of x x Type: int Address: Value: 5 x Type: int Address: Value: 5 RAM (Random Access Memory) ptr Type: int * Address: Value: ptr Type: int * Address: Value: 56789
Putting it all together int x = 5; // declare and assign x to 5 int *ptr; // declare a pointer to int types ptr = &x; // ptr contains the address of x int y = *ptr; // declare y and dereference ptr. We have two copies of 5. x Type: int Address: Value: 5 x Type: int Address: Value: 5 RAM (Random Access Memory) ptr Type: int * Address: Value: ptr Type: int * Address: Value: y Type: int Address: Value: 5 y Type: int Address: Value: 5
Putting it all together int x = 5; // declare and assign x to 5 int *ptr; // declare a pointer to int types ptr = &x; // ptr contains the address of x int y = *ptr; // declare y and dereference ptr. We have two copies of 6. *ptr = 6; // change the value pointed to by ptr. x is now 6, y is still 5 x Type: int Address: Value: 6 x Type: int Address: Value: 6 RAM (Random Access Memory) ptr Type: int * Address: Value: ptr Type: int * Address: Value: y Type: int Address: Value: 5 y Type: int Address: Value: 5
A note on style I’ve been typing this: int* ptr; But most people program like this: int *ptr; These are exactly the same! Just remember that you should interpret both of these as (int *) ptr; // ptr’s type is int* ( a pointer to an int )
WARNING > * syntax Do not confuse the * syntax!!!!!! 1. Declarations: * is part of the type of the pointer int* ptr; // ptr has type (int *). This is equivalent to: int* ptr; 2. Dereferencing: retrieves the value at the address that the pointer points to (i.e. follow the arrow) int x = 5; ptr = &x; // ptr points to x int y = *ptr; // dereference operator, y == 5 now 3. Assignment: changes the value at the address that the pointer points to *ptr = 6; // change the value of x; if ptr is dereferenced now, it will return 6. y is still 5
WARNING > Uninitialized Pointers int *ptr; // ptr not yet initialized // ptr = &x; initializes it. GARBAGE? Huh? Pointers that have not yet been assigned an address (i.e. ptr = &x ) will point to a random place in RAM. Dereferencing an uninitialized pointer is a BIG NONO. The operating system will not let you read/write to memory that you are not allowed to touch; this leads to Segmentation Faults. TIP: Always initialize a pointer before dereferencing it ptr Type: int * Address: Value: GARBAGE ptr Type: int * Address: Value: GARBAGE
NULL C has a sentinel address called NULL which is memory address 0 Used to signal to programmers that the pointer is currently not set Useful in building linkedlists, binary search trees, etc. Note: Pointers are NOT automatically initialized to the value NULL WARNING Dereferencing a pointer pointing to NULL leads to a segmentation fault
NULL int *ptr = NULL; // declare ptr and set it to NULL... if (!ptr) { // !ptr evaluates to true if ptr == 0 (NULL) printf(“ptr is pointing to nothing!\n”); int pointee = *ptr; // try to dereference ptr: THIS WILL // CAUSE A SEGMENTATION FAULT } else { printf(“ptr is pointing to address %d\n”,ptr); int pointee = *ptr; printf(“the value pointed to by ptr is %d\n,pointee); }
NULL vs. ‘\0’ NULL is a sentinel memory address; NULL == 0 Used to indicate a pointer is pointing to nothing ‘\0’ is a character who’s ASCII value is 0 Used to indicate the end of a string NULL is an ADDRESS (a fake one) ‘\0’ is a VALUE (a character)
Pointer Practice abcpapbpc a = b * c; a *= c; b = *pa; pc = pa; *pb = b * c; c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;
Pointer Practice abcpapbpc a = b * c;20 45&a&b&c a *= c; b = *pa; pc = pa; *pb = b * c; c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;
Pointer Practice abcpapbpc a = b * c;20 45&a&b&c a *= c; 10045&a&b&c b = *pa; pc = pa; *pb = b * c; c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;
Pointer Practice abcpapbpc a = b * c;20 45&a&b&c a *= c; 10045&a&b&c b = *pa; 100 5&a&b&c pc = pa; *pb = b * c; c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;
Pointer Practice abcpapbpc a = b * c;20 45&a&b&c a *= c; 10045&a&b&c b = *pa; 100 5&a&b&c pc = pa; 100 5&a&b&a *pb = b * c; c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;
Pointer Practice abcpapbpc a = b * c;20 45&a&b&c a *= c; 10045&a&b&c b = *pa; 100 5&a&b&c pc = pa; 100 5&a&b&a *pb = b * c; &a&b&a c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;
Pointer Practice abcpapbpc a = b * c;20 45&a&b&c a *= c; 10045&a&b&c b = *pa; 100 5&a&b&c pc = pa; 100 5&a&b&a *pb = b * c; &a&b&a c = (*pa) * (*pc); &a&b&a *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;
Pointer Practice abcpapbpc a = b * c; 2045&a&b&c a *= c; 10045&a&b&c b = *pa; 100 5&a&b&c pc = pa; 100 5&a&b&a *pb = b * c; &a&b&a c = (*pa) * (*pc); &a&b&a *pc = a * (*pb); &a&b&a int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;
Pointer Arithmetic int x = 5; int *ptr = &x; int y = *ptr; // y gets 5 int z = *(ptr + 1); // z gets the stuff (?) in memory at address &x + 1 * sizeof(int) Could potentially segfault! So don’t use pointer arithmetic unless you know something you allocated is there.
Pointer Arithmetic int *ptr = &x; What are these addresses? ptr + 2 == ptr + 0 == ptr + 13 ==
Pointer Arithmetic int *ptr = &x; What are these addresses? ptr + 2 == &x + 2 * sizeof(int) ptr + 0 == &x ptr + 13 == &x + 13 * sizeof(int) We need to multiply the offset by the size of the type the pointer is pointing to (for int *, we multiply by sizeof(int) )
Arrays are pointers int scores[3] = {100, 99, 98}; score is actually a pointer to it’s first element, score[0] == 100 int second = scores[2]; EXACTLY THE SAME AS: int second = *(scores + 2); int zeroth = scores[0]; EXACTLY THE SAME AS: int zeroth = *scores;
Why does string == char * ? h h In cs50.h:... /* * Our own data type for string variables */ typedef char *string;...
Strings We alias string to be really a char *, a pointer to a character Arrays are really just pointers So a string is really just a pointer to the first character in a sequence of contiguous characters, terminated with ‘\0’
Strings char *s = GetString(); // user enters “cat” GetString() returns a pointer to the first character of a char array in memory. Type: char Address: Value: ‘c’ Type: char Address: Value: ‘c’ RAM (Random Access Memory) s Type: char * Address: Value: s Type: char * Address: Value: Type: char Address: Value: ‘a’ Type: char Address: Value: ‘a’ Type: char Address: Value: ‘t’ Type: char Address: Value: ‘t’ Type: char Address: Value: ‘\0’ Type: char Address: Value: ‘\0’
Strings QUESTION: But wait, where is the space being allocated for the string?
Heap and Stack Heap Contains local variables. Function calls create new ‘frames’ on the stack. Memory belonging to process. Stack Lower Memory Addresses Higher Memory Addresses Contains global variables. Dynamically allocated memory reserved on heap.
Heap and Stack Heap In main: // user enters “cat” char *s = GetString(); Memory belonging to process. Stack Lower Memory Addresses Higher Memory Addresses Space is dynamically allocated for “cat” in the heap s s ‘c’ ‘a’ ‘t’ ‘\0 ’
Heap vs. Stack Heap memory persists Memory allocated in the heap by one function will still be there after the function return (unless the memory is freed) Stack memory does not persist Memory allocated in the stack (with stack frames, e.g. local variables) by a function will not persist after the function returns
Dynamically allocating memory malloc(int size) : memory allocation On the terminal, type “man 3 malloc” takes an integer parameter and returns a pointer to an array in the heap of the requested size, or returns NULL to signal a failure occurred
This won’t work int arrsize = GetInt(); int arr[arrsize]; GCC must know at compile time the size of arrays, but arrsize is computed at runtime through user input. => Can’t allocate variable-sized arrays!
A fix: int arrsize = GetInt(); int *arr = (int *) malloc(sizeof(int) * arrsize); malloc returns a pointer to an int array of size arrsize. This array will be located in the heap. => Can allocate memory dynamically with malloc!
Should make sure malloc worked int arrsize = GetInt(); int *arr = (int *) malloc(sizeof(int) * arrsize); if (arr == NULL) { printf(“malloc failed! No memory left!\n”); } Malloc will return NULL if it fails to allocate the requested amount of memory.
Malloc’ed memory must be freed! ALL memory allocated by malloc MUST be freed Otherwise this will lead to memory leaks – VERY BAD! Have you ever wondered why your computer seems to slow down even if you have no programs open? Use the free(ptr) function; ptr must be a pointer returned by a malloc call! int arrsize = GetInt(); int *arr = (int *) malloc(sizeof(int) * arrsize); if (arr == NULL) return -1; // do some stuff free(arr);
GetString() GetString() calls malloc to allocate the memory in the heap for the string So every time you called GetString() and didn’t free the memory, you created a memory leak!!!! From now on, you must free all your strings. char *s = GetString(); // do stuff free(s);
Swap int main(void) { int x = 5; int y = 6; swap(x,y); printf(“x: %d, y: %d\n”, x, y); // what are x and y after swap? } void swap(int a, int b) { int temp = a; a = b; b = temp; }
Swap int main(void) { int x = 5; int y = 6; swap(x,y); printf(“x: %d, y: %d\n”, x, y); // x = 5, y = 6, WHAT??! } void swap(int a, int b) { int temp = a; a = b; b = temp; }
Swap: Stack frames int main(void) { int x = 5; int y = 6; swap(x,y); printf(“x: %d, y: %d\n”, x, y); } void swap(int a, int b) { int temp = a; a = b; b = temp; } main swap x = 5 y = 6 a = 5 b = 6 Swap has it’s own copies of 5 and 6!! You cannot change x and y inside swap.
Swap: Stack frames int main(void) { int x = 5; int y = 6; swap(x,y); printf(“x: %d, y: %d\n”, x, y); } void swap(int a, int b) { int temp = a; a = b; b = temp; } main swap x = 5 y = 6 a = 5 b = 6 Swap has it’s own copies of 5 and 6!! You cannot change x and y inside swap. This is an example of call-by-value: the values of the parameters are copied from the caller into the callee.
Swap: fix int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); // x = 6, y = 5 } void swap(int *a, int *b) { // a and b are pointers int temp = *a; // temp gets the old value at the address in a *a = *b; // assign the address in a to the value at the // address in b *b = temp; // assign the address in b to temp }
Swap: Stack frames int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } main swap x = 5 y = 6 a = &x b = &y
Swap: Stack frames int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } main swap x = 5 y = 6 a a b b temp = 5 a = &x b = &y
Swap: Stack frames int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } main swap x = 6 y = 6 a a b b temp = 5 a = &x b = &y
Swap: Stack frames int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } main swap x = 6 y = 5 a a b b temp = 5 a = &x b = &y
int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } Swap: Stack frames main swap x = 6 y = 5 temp = 5 b = &ya = &x
int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } Swap: Stack frames main swap x = 6 y = 5 temp = 5 This is a call-by-reference: instead of passing in a value, we pass a pointer so that the callee can read/write to the given address in the pointer. b = &ya = &x
So why use pointers? Pointers allow us to dynamically allocate memory With malloc Must be freed Can allocate variables in heap to be preserved between function calls Pointers allow us to call by reference Allows us to pass pointers into arguments and affect variables through multiple functions Swap function
Fun fun fun eek4.c