Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.

Similar presentations


Presentation on theme: "CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future."— Presentation transcript:

1 CS50 SECTION: WEEK 4 Kenny Yu

2 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: http://www.cs50.net/quizzes/http://www.cs50.net/quizzes/  Section resources: https://cloud.cs50.net/~kennyyu/section/week4/https://cloud.cs50.net/~kennyyu/section/week4/  Section feedback poll:  https://www.google.com/moderator/#15/e=a9fce&t=a9fce.44 https://www.google.com/moderator/#15/e=a9fce&t=a9fce.44  What do you want to review in section for Quiz 0 next week?  https://www.google.com/moderator/#15/e=a9fce&t=a9fce.45 https://www.google.com/moderator/#15/e=a9fce&t=a9fce.45

3 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

4 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): 508 509 510 511 …

5 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: 508 509 510 511 … char c = ‘a’; c Address of c is 508.

6 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)

7 Pointer Syntax > Declaring Pointers  Declaring a pointer:  var_type* pointer_name;  Example:  int* ptr; // ptr has type int*; // ptr points to int types

8 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)

9 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.

10 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.

11 Putting it all together int x = 5; // declare and assign x to 5 RAM (Random Access Memory) x Type: int Address: 56789 Value: 5 x Type: int Address: 56789 Value: 5

12 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: 56789 Value: 5 x Type: int Address: 56789 Value: 5 RAM (Random Access Memory) ptr Type: int * Address: 12345 Value: GARBAGE ptr Type: int * Address: 12345 Value: GARBAGE

13 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: 56789 Value: 5 x Type: int Address: 56789 Value: 5 RAM (Random Access Memory) ptr Type: int * Address: 12345 Value: 56789 ptr Type: int * Address: 12345 Value: 56789

14 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: 56789 Value: 5 x Type: int Address: 56789 Value: 5 RAM (Random Access Memory) ptr Type: int * Address: 12345 Value: 56789 ptr Type: int * Address: 12345 Value: 56789 y Type: int Address: 87654 Value: 5 y Type: int Address: 87654 Value: 5

15 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: 56789 Value: 6 x Type: int Address: 56789 Value: 6 RAM (Random Access Memory) ptr Type: int * Address: 12345 Value: 56789 ptr Type: int * Address: 12345 Value: 56789 y Type: int Address: 87654 Value: 5 y Type: int Address: 87654 Value: 5

16 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 )

17 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

18 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: 12345 Value: GARBAGE ptr Type: int * Address: 12345 Value: GARBAGE

19 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

20 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); }

21 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)

22 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;

23 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;

24 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;

25 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;

26 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;

27 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; 1005005&a&b&a c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;

28 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; 1005005&a&b&a c = (*pa) * (*pc); 10050010000&a&b&a *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;

29 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; 1005005&a&b&a c = (*pa) * (*pc); 10050010000&a&b&a *pc = a * (*pb); 5000050010000&a&b&a int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;

30 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.

31 Pointer Arithmetic int *ptr = &x; What are these addresses? ptr + 2 == ptr + 0 == ptr + 13 ==

32 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) )

33 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;

34 Why does string == char * ?  http://cdn.cs50.net/2011/fall/lectures/4/src/cs50. h http://cdn.cs50.net/2011/fall/lectures/4/src/cs50. h In cs50.h:... /* * Our own data type for string variables */ typedef char *string;...

35 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’

36 Strings char *s = GetString(); // user enters “cat” GetString() returns a pointer to the first character of a char array in memory. Type: char Address: 56789 Value: ‘c’ Type: char Address: 56789 Value: ‘c’ RAM (Random Access Memory) s Type: char * Address: 12345 Value: 56789 s Type: char * Address: 12345 Value: 56789 Type: char Address: 56790 Value: ‘a’ Type: char Address: 56790 Value: ‘a’ Type: char Address: 56791 Value: ‘t’ Type: char Address: 56791 Value: ‘t’ Type: char Address: 56792 Value: ‘\0’ Type: char Address: 56792 Value: ‘\0’

37 Strings QUESTION: But wait, where is the space being allocated for the string?

38 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.

39 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 ’

40 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

41 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

42 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!

43 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!

44 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.

45 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);

46 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);

47 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; }

48 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; }

49 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.

50 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.

51 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 }

52 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

53 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

54 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

55 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

56 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

57 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

58 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

59 Fun fun fun  https://cloud.cs50.net/~kennyyu/section/week4/w eek4.c


Download ppt "CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future."

Similar presentations


Ads by Google