cs50 section: week 4 kenny yu. announcements problem set 4 walkthrough online problem set 2...

59
CS50 SECTION: WEEK 4 Kenny Yu

Upload: virgil-lloyd

Post on 12-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

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

CS50 SECTION: WEEK 4

Kenny Yu

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

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/

Section resources: https://cloud.cs50.net/~kennyyu/section/week4/

Section feedback poll: https://www.google.com/moderator/#15/e=a9fce&t=a9fce.4

4 What do you want to review in section for Quiz 0 next week?

https://www.google.com/moderator/#15/e=a9fce&t=a9fce.45

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

Agenda

1. RAM2. Pointers

1. Declaring Pointers2. Address Operator3. Dereferencing Pointers4. Pointer Assignment5. WARNING

3. Arrays Revisited4. Heap and Stack5. Malloc

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

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)

…0A 05 FF 00

Address (dec): 508 509 510 511

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

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)

…61 05 FF 00

Address: 508 509 510 511

char c = ‘a’;

c

Address of c is 508.

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

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)

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

Pointer Syntax > Declaring Pointers Declaring a pointer:

var_type* pointer_name;

Example: int* ptr; // ptr has type int*;

// ptr points to int types

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

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 5int* ptr; // declare a pointer to int typesptr = &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)

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

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 5int* ptr; // declare a pointer to int typesptr = &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.

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

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 typesptr = &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.

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

Putting it all together

int x = 5; // declare and assign x to 5

RAM (Random Access Memory)

xType: int

Address: 56789Value: 5

xType: int

Address: 56789Value: 5

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

Putting it all together

int x = 5; // declare and assign x to 5

int *ptr; // declare a pointer to int types

xType: int

Address: 56789Value: 5

xType: int

Address: 56789Value: 5

RAM (Random Access Memory)

ptrType: int *

Address: 12345Value:

GARBAGE

ptrType: int *

Address: 12345Value:

GARBAGE

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

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

xType: int

Address: 56789Value: 5

xType: int

Address: 56789Value: 5

RAM (Random Access Memory)

ptrType: int *

Address: 12345Value: 56789

ptrType: int *

Address: 12345Value: 56789

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

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.

xType: int

Address: 56789Value: 5

xType: int

Address: 56789Value: 5

RAM (Random Access Memory)

ptrType: int *

Address: 12345Value: 56789

ptrType: int *

Address: 12345Value: 56789

yType: int

Address: 87654Value: 5

yType: int

Address: 87654Value: 5

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

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

xType: int

Address: 56789Value: 6

xType: int

Address: 56789Value: 6

RAM (Random Access Memory)

ptrType: int *

Address: 12345Value: 56789

ptrType: int *

Address: 12345Value: 56789

yType: int

Address: 87654Value: 5

yType: int

Address: 87654Value: 5

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

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 )

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

WARNING > * syntax

Do not confuse the * syntax!!!!!!1. Declarations: * is part of the type of the

pointerint* 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 xint 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

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

WARNING > Uninitialized Pointersint *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

ptrType: int *

Address: 12345Value:

GARBAGE

ptrType: int *

Address: 12345Value:

GARBAGE

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

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

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

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

}

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

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)

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

Pointer Practice

a b c pa pb pc

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;

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

Pointer Practice

a b c pa pb pc

a = b * c; 20 4 5 &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;

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

Pointer Practice

a b c pa pb pc

a = b * c; 20 4 5 &a &b &c

a *= c; 100 4 5 &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;

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

Pointer Practice

a b c pa pb pc

a = b * c; 20 4 5 &a &b &c

a *= c; 100 4 5 &a &b &c

b = *pa; 100 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;

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

Pointer Practice

a b c pa pb pc

a = b * c; 20 4 5 &a &b &c

a *= c; 100 4 5 &a &b &c

b = *pa; 100 100 5 &a &b &c

pc = pa; 100 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;

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

Pointer Practice

a b c pa pb pc

a = b * c; 20 4 5 &a &b &c

a *= c; 100 4 5 &a &b &c

b = *pa; 100 100 5 &a &b &c

pc = pa; 100 100 5 &a &b &a

*pb = b * c; 100 500 5 &a &b &a

c = (*pa) * (*pc);

*pc = a * (*pb);

int a = 3, b = 4, c = 5;int *pa = &a, *pb = &b, *pc = &c;

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

Pointer Practice

a b c pa pb pc

a = b * c; 20 4 5 &a &b &c

a *= c; 100 4 5 &a &b &c

b = *pa; 100 100 5 &a &b &c

pc = pa; 100 100 5 &a &b &a

*pb = b * c; 100 500 5 &a &b &a

c = (*pa) * (*pc);

100 500 10000 &a &b &a

*pc = a * (*pb);

int a = 3, b = 4, c = 5;int *pa = &a, *pb = &b, *pc = &c;

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

Pointer Practice

a b c pa pb pc

a = b * c; 20 4 5 &a &b &c

a *= c; 100 4 5 &a &b &c

b = *pa; 100 100 5 &a &b &c

pc = pa; 100 100 5 &a &b &a

*pb = b * c; 100 500 5 &a &b &a

c = (*pa) * (*pc);

100 500 10000 &a &b &a

*pc = a * (*pb);

50000 500 10000 &a &b &a

int a = 3, b = 4, c = 5;int *pa = &a, *pb = &b, *pc = &c;

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

Pointer Arithmetic

int x = 5;int *ptr = &x;int y = *ptr; // y gets 5int 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.

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

Pointer Arithmetic

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

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

Pointer Arithmetic

int *ptr = &x;What are these addresses?ptr + 2 == &x + 2 * sizeof(int)ptr + 0 == &xptr + 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) )

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

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;

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

Why does string == char * ? http://cdn.cs50.net/2011/fall/lectures/4/s

rc/cs50.hIn cs50.h:

...

/*

* Our own data type for string variables

*/

typedef char *string;

...

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

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’

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

Strings

char *s = GetString(); // user enters “cat”GetString() returns a pointer to the first

character of a char array in memory.

Type: charAddress: 56789

Value: ‘c’

Type: charAddress: 56789

Value: ‘c’

RAM (Random Access Memory)

sType: char *

Address: 12345

Value: 56789

sType: char *

Address: 12345

Value: 56789Type: charAddress: 56790

Value: ‘a’

Type: charAddress: 56790

Value: ‘a’

Type: charAddress: 56791

Value: ‘t’

Type: charAddress: 56791

Value: ‘t’

Type: charAddress: 56792

Value: ‘\0’

Type: charAddress: 56792

Value: ‘\0’

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

Strings

QUESTION:

But wait, where is the space being allocated for the string?

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

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.

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

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 dynamicallyallocated for “cat” in the heap

ss

‘c’‘c’

‘a’‘a’

‘t’‘t’

‘\0’‘\0’

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

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

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

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

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

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!

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

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!

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

Should make sure malloc workedint 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.

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

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

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

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 stufffree(s);

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

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;

}

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

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;

}

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

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 = 5x = 5 y = 6y = 6

a = 5a = 5 b = 6b = 6

Swap has it’s own copies of 5 and 6!!You cannot change x and y inside swap.

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

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 = 5x = 5 y = 6y = 6

a = 5a = 5 b = 6b = 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 fromthe caller into the callee.

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

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

}

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

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 = 5x = 5 y = 6y = 6

a = &xa = &x b = &yb = &y

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

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 = 5x = 5 y = 6y = 6

aa bb

temp = 5

temp = 5

a = &xa = &x b = &yb = &y

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

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 = 6x = 6 y = 6y = 6

aa bb

temp = 5

temp = 5

a = &xa = &x b = &yb = &y

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

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 = 6x = 6 y = 5y = 5

aa bb

temp = 5

temp = 5

a = &xa = &x b = &yb = &y

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

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 = 6x = 6 y = 5y = 5

temp = 5

b = &y

a = &x

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

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 = 6x = 6 y = 5y = 5

temp = 5

This is a call-by-reference: instead of passing in a value, we pass a pointerso that the callee can read/write to the given address in the pointer.

b = &y

a = &x

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

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

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

Fun fun fun

https://cloud.cs50.net/~kennyyu/section/week4/week4.c