CS31 Discussion 1E - CS | Computer...

88
CS31 Discussion 1E Spring 17’: week 08 TA: Bo-Jhang Ho [email protected] Credit to former TA Chelsea Ju

Transcript of CS31 Discussion 1E - CS | Computer...

Page 1: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

CS31 Discussion 1ESpring 17’: week 08

TA: Bo-Jhang [email protected]

Credit to former TA Chelsea Ju

Page 2: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Project 5 -Map cipher to crib

} Approach 1: For each pair of positions, check two letters in cipher and crib are both identical or different} For each pair of positions pos1 and pos2,

cipher[pos1] == cipher[pos2] should equal to crib[pos1] == crib[pos2]

} Approach 2: Get the positions of the letter and compare} For each position pos,

indexes1 = getAllPositions(cipher, pos, cipher[pos])indexes2 = getAllPositions(crib, pos, crib[pos])indexes1 should equal to indexes2

Page 3: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Project 5 -Map cipher to crib

} Approach 3: Generate the mapping} We first have a mapping array char cipher2crib[128] = {‘\0’};} Then whenever we attempt to map letterA in cipher to letterB

in crib, we first check whether it violates the previous setup:} Is cipher2crib[letterA] != ‘\0’? // implies letterA has been used} Then cipher2crib[letterA] should equal to letterB

} If no violation happens,} cipher2crib[letterA] = letterB;

Page 4: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Road map of CS31

Variable If / else Loops Pointer!!

Array Function Class

String

Page 5: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Cheat sheet

Data type

} int *a;} a is a variable, whose data type is int *} a stores an address of some integer

“Use as verbs”

} & - address-of operator} &b means I want to get the memory address of variable b

} * - dereference operator} *c means I want to retrieve the value in address c

Page 6: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Memory modelAddress Memory Contents

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1 byte

Page 7: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Memory modelAddress Memory Contents

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1 byte = 8 bits

off on

A bit has 2 states

Page 8: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Memory modelAddress Memory Contents

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1 byte = 8 bits

off on

A bit has 2 states

A byte has 256 (=28) states

Page 9: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Memory modelAddress Memory Contents

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1 byte = 8 bits

off on

A bit has 2 states

A byte has 256 (=28) states

A char takes 1 byte

An int takes 4 bytes

A double takes 8 bytes

Page 10: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Data type review

} Basic data typesType Bytes Bits Value range

char 1 8 -128 to 127

short 2 16 -32,768 to 32,767

int 4 32 -2,147,483,648 to 2,147,483,647

long long 8 64 -9 * 10^18 to 9 * 10^18

float 4 32 -3.4 * 10^38 to 3.4 * 10^38

double 8 64 -1.7 * 10^308 to 1.7 * 10^308

Page 11: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Memory modelAddress Memory Contents

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1 byte = 8 bits

off on

A bit has 2 states

A byte has 256 (=28) states

My has 16GB ram

Page 12: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Memory modelAddress Memory Contents

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1 byte = 8 bits

off on

A bit has 2 states

A byte has 256 (=28) states

My has 16GB ram

= 16,000,000,000 bytes!

Page 13: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int b = 3;double c = 3.5;int d = b - a;

return 0;}

Address Memory Contents1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

Page 14: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int b = 3;double c = 3.5;int d = b - a;

return 0;}

Address Memory Contents1000

5

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

a

Page 15: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int b = 3;double c = 3.5;int d = b - a;

return 0;}

Address Memory Contents1000

5

1004

3

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

a

b

Page 16: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int b = 3;double c = 3.5;int d = b - a;

return 0;}

Address Memory Contents1000

5

1004

3

1008

3.5

1016

1017

1018

1019

a

b

c

Page 17: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int b = 3;double c = 3.5;int d = b - a;

return 0;}

Address Memory Contents1000

5

1004

3

1008

3.5

1016

2

a

b

c

d

Page 18: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int *b = &a;

a++;*b += 2;

cout << a << endl;cout << *b << endl;

return 0;}

Address Memory Contents1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

Page 19: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int *b = &a;

a++;*b += 2;

cout << a << endl;cout << *b << endl;

return 0;}

Address Memory Contents1000

5

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

a

Page 20: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int *b = &a;

a++;*b += 2;

cout << a << endl;cout << *b << endl;

return 0;}

Address Memory Contents1000

5

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

a

Page 21: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int *b; // declare a pointer var

b = &a; // set address

a++;*b += 2;

cout << a << endl;cout << *b << endl;

return 0;}

Address Memory Contents1000

5

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

a

Page 22: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int *b; // declare a pointer var

b = &a; // set address

a++;*b += 2;

cout << a << endl;cout << *b << endl;

return 0;}

Address Memory Contents1000

5

1004

??(address)

1012

1013

1014

1015

1016

1017

1018

1019

a

b

Page 23: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int *b; // declare a pointer var

b = &a; // set address

a++;*b += 2;

cout << a << endl;cout << *b << endl;

return 0;}

Address Memory Contents1000

5

1004

1000(address)

1012

1013

1014

1015

1016

1017

1018

1019

a

b

Page 24: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int *b; // declare a pointer var

b = &a; // set address

a++;*b += 2;

cout << a << endl;cout << *b << endl;

return 0;}

Address Memory Contents1000

6

1004

1000(address)

1012

1013

1014

1015

1016

1017

1018

1019

a

b

5 à

Page 25: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int *b; // declare a pointer var

b = &a; // set address

a++;*b += 2;

cout << a << endl;cout << *b << endl;

return 0;}

Address Memory Contents1000

8

1004

1000(address)

1012

1013

1014

1015

1016

1017

1018

1019

a

b

6 à

Page 26: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int *b; // declare a pointer var

b = &a; // set address

a++;*b += 2;

cout << a << endl;cout << *b << endl;

return 0;}

Address Memory Contents1000

8

1004

1000(address)

1012

1013

1014

1015

1016

1017

1018

1019

a

b

} Output} 8

Page 27: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

What’s going on in the memory

int main() {int a = 5;int *b; // declare a pointer var

b = &a; // set address

a++;*b += 2;

cout << a << endl;cout << *b << endl;

return 0;}

Address Memory Contents1000

8

1004

1000(address)

1012

1013

1014

1015

1016

1017

1018

1019

a

b

} Output} 8

8

Page 28: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Using uninitialized variablesis always dangerous

int main() {int a;int b = a + 3;int *c;

*c = 27;

return 0;}

Address Memory Contents1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

Page 29: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Using uninitialized variablesis always dangerous

int main() {int a;int b = a + 3;int *c;

*c = 27;

return 0;}

Address Memory Contents1000

??

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

a

Page 30: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Using uninitialized variablesis always dangerous

int main() {int a;int b = a + 3;int *c;

*c = 27;

return 0;}

Address Memory Contents1000

??

1004

??

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

a

b

Page 31: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Using uninitialized variablesis always dangerous

int main() {int a;int b = a + 3;int *c;

*c = 27;

return 0;}

Address Memory Contents1000

??

1004

??

1008

??(address)

1016

1017

1018

1019

a

b

c

Page 32: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Using uninitialized variablesis always dangerous

int main() {int a;int b = a + 3;int *c;

*c = 27;

return 0;}

Address Memory Contents1000

??

1004

??

1008

??(address)

1016

1017

1018

1019

a

b

c } May manipulate a piece of memory not belonging to this program!!

Page 33: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array v.s. Pointer

Array is a special case of pointer

Pointer can be treated as an array

Page 34: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = &b[1];

*c = 100;

return 0;}

Address Memory Contents1000

1002

1004

1006

1008

1010

1012

1014

1016

1018

1020

1022

1024

1026

1028

1030

1032

1034

1036

1038

Page 35: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = &b[1];

*c = 100;

return 0;}

Address Memory Contents1000

31004

1006

1008

1010

1012

1014

1016

1018

1020

1022

1024

1026

1028

1030

1032

1034

1036

1038

a

Page 36: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = &b[1];

*c = 100;

return 0;}

Address Memory Contents1000

31004

101008

201012

301016

401020

501024

1026

1028

1030

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

Page 37: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = &b[1];

*c = 100;

return 0;}

Address Memory Contents1000

31004

101008

201012

301016

401020

501024

1026

1028

1030

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

Page 38: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c; // declare a pointer

c = &b[1]; // get address

*c = 100;

return 0;}

Address Memory Contents1000

31004

101008

201012

301016

401020

501024

1026

1028

1030

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

Page 39: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c; // declare a pointer

c = &b[1]; // get address

*c = 100;

return 0;}

Address Memory Contents1000

31004

101008

201012

301016

401020

501024

??(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

Page 40: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c; // declare a pointer

c = &b[1]; // get address

*c = 100;

return 0;}

Address Memory Contents1000

31004

101008

201012

301016

401020

501024

1008(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

Page 41: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c; // declare a pointer

c = &b[1]; // get address

*c = 100;

return 0;}

Address Memory Contents1000

31004

101008

1001012

301016

401020

501024

1008(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

20 à

Page 42: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Time and Delta analogy

Time Delta Time + =

Delta Delta Delta+ =

Delta Time Time+ =

Time Time Doesn’tmake sense!+ =

Page 43: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Time and Delta analogy

+ =

+ =

+ =

Doesn’tmake sense!+ =

Page 44: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Time and Delta analogy

Pointer Int Pointer+ =

Int Int Int+ =

Int Pointer Pointer+ =

Pointer Pointer Doesn’tmake sense!+ =

Page 45: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Time and Delta analogy

Time Delta Time - =

Delta Delta Delta- =

Delta Time- =

Time Time- = ??

Doesn’tmake sense!

Page 46: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Time and Delta analogy

Time Delta Time - =

Delta Delta Delta- =

Delta Time- =

Time Time- = Delta

Doesn’tmake sense!

Page 47: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Time and Delta analogy

=

=

=

=

-

-

-

-

Doesn’tmake sense!

Page 48: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Time and Delta analogy

Pointer Int Pointer- =

Int Int Int- =

Int Pointer- =

Pointer Pointer- = Int

Doesn’tmake sense!

Page 49: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = &b[1];

c = c + 1; // Or,// c += 1;// c++;

*c = 27;

return 0;}

Address Memory Contents1000

31004

101008

201012

301016

401020

501024

1008(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

Page 50: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = &b[1];

c = c + 1; // Or,// c += 1;// c++;

*c = 27;

return 0;}

Address Memory Contents1000

31004

101008

201012

301016

401020

501024

1012(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c 1008à } What? 1008 + 1 = 1012?

Page 51: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = &b[1];

c = c + 1; // Or,// c += 1;// c++;

*c = 27;

return 0;}

Address Memory Contents1000

31004

101008

201012

301016

401020

501024

1012(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c 1008 à } Pointer arithmetic} We should interpret as adding

the memory size of 1 integer

Page 52: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = b;

c += 2; *c = 27;

return 0;}

Address Memory Contents1000

31004

101008

201012

301016

401020

501024

1004(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

} Array name is a pointer} b can be treated as an int*

Page 53: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = b;

c += 2; *c = 27;

return 0;}

Address Memory Contents1000

31004

101008

201012

301016

401020

501024

1012(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

} Array name is a pointer} b can be treated as an int*

1004à

Page 54: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = b;

c += 2; *c = 27;

return 0;}

Address Memory Contents1000

31004

101008

201012

271016

401020

501024

1012(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

} Array name is a pointer} b can be treated as an int*

30 à

Page 55: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

More about array} Array name can be considered as start point

} Technically, it’s the base address

} The index can be considered as the offset

Page 56: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

More about array

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = b;

cout << b[-1] << endl;

return 0;}

Address Memory Contents1000

31004

101008

201012

271016

401020

501024

1004(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

b[0]

b[-1]

b[1]

b[2]

b[3]

b[4]

b[5]

b[6]

b[7]

b[8]

b[9]

b[-2]

b[-3]

b[-4]

Page 57: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Use pointer like an array} If x is a pointer, you can treat x as an array

} Meaning, you can have something like x[3]

Page 58: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Treat pointer as an array

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = b + 2;

cout << c[-2] << endl;

return 0;}

Address Memory Contents1000

31004

101008

201012

271016

401020

501024

1012(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

Page 59: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Treat pointer as an array

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = b + 2;

cout << c[-2] << endl;

return 0;}

Address Memory Contents1000

31004

101008

201012

271016

401020

501024

1012(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

c[0]

Page 60: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Treat pointer as an array

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = b + 2;

cout << c[-2] << endl;

return 0;}

Address Memory Contents1000

31004

101008

201012

271016

401020

501024

1012(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

c[0]

c[1]

c[2]

c[3]

c[4]

c[5]

c[6]

c[7]

Page 61: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Treat pointer as an array

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = b + 2;

cout << c[-2] << endl;

return 0;}

Address Memory Contents1000

31004

101008

201012

271016

401020

501024

1012(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

c[-2]

c[-3]

c[-1]

c[0]

c[1]

c[2]

c[3]

c[4]

c[5]

c[6]

c[7]

c[-4]

c[-6]

Page 62: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Treat pointer as an array

int main() {int a = 3;int b[5] = {10, 20, 30, 40, 50};int *c = b + 2;

cout << c[-2] << endl;

return 0;}

Address Memory Contents1000

31004

101008

201012

271016

401020

501024

1012(address)

1032

1034

1036

1038

a

b[0]

b[1]

b[2]

b[3]

b[4]

b

c

c[-2]

c[-3]

c[-1]

c[0]

c[1]

c[2]

c[3]

c[4]

c[5]

c[6]

c[7]

c[-4]

c[-6]

} It is actually a valid memory access

Page 63: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Summary} Array name is a pointer} Pointer arithmetic

} Let’s say x is a pointer, n is an integer} x + n is a pointer (memory address) after n elements of x} x - n is a pointer before n elements of x

} Treat a pointer as an array} Again, let’s say x is a pointer, n is an integer} x[n] means to access nth element counted from x} x[n] equivalent to *(x + n)

Page 64: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Remind a previous example 01 -Pass an array to a function

} From caller:

} In the function:

or

or

int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};foo(arr);

void foo(int params[10]) { … }

void foo(int params[]) { … }

void foo(int *params) { … }

Page 65: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Remind a previous example 02 -Mapping of cipher and crib in project 5

} Approach 3: Generate the mapping} We first have a mapping array char cipher2crib[128] = {‘\0’};

} Whenever we attempt to map letterA in cipher to letterB in crib:} cipher2crib[letterA] = letterB;

} But there are only 26 letters. Why do we want to have an array with 128 elements?} We use ascii as a key (a.k.a index) in the array} We don’t need to worry about the offset (i.e., x - ‘a’)

} Is there any solution to reduce the size to 26?

Page 66: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Remind a previous example 02 -Mapping of cipher and crib in project 5

bool check(const char cipher[], const char crib[]) {// assume cipher and crib have the same length

char cipher2crib[128] = {'\0'};for (int i = 0; cipher[i] != '\0'; i++) {

char letterA = cipher[i];char letterB = crib[i];if (cipher2crib[letterA] != '\0’

&& cipher2crib[letterA] != letterB)return false;

cipher2crib[letterA] = letter}

return true;}

Page 67: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Remind a previous example 02 -Mapping of cipher and crib in project 5

bool check(const char cipher[], const char crib[]) {// assume cipher and crib have the same length

char trueArray[26] = {'\0'};char *cipher2crib = trueArray – 'a';for (int i = 0; cipher[i] != '\0'; i++) {

char letterA = cipher[i];char letterB = crib[i];if (cipher2crib[letterA] != '\0’

&& cipher2crib[letterA] != letterB)return false;

cipher2crib[letterA] = letter}

return true;}

26 elements

trueArraycipher2crib

97 elements

Memory footprint

Page 68: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Question 01} If we declare int *ptr; and int val;, the

following two usages are valid:} ptr = &val;} *ptr = val;

} What is &ptr?

Page 69: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Question 01} If we declare int *ptr; and int val;, the

following two usages are valid:} ptr = &val;} *ptr = val;

} What is &ptr?} &ptr means the address of ptr} The type of &ptr is int** (we call it double pointer)} For example, int** strongPtr = &ptr;

Page 70: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Double pointer

int main() {int a = 3;int *b = &a;int c[5] = {10, 20, 30, 40, 50};int** d = &b;

return 0;}

Address Memory Contents1000

31004

1006

1008

1010

1012

1014

1016

1018

1020

1022

1024

1026

1028

1030

1032

1034

1036

1038

a

Page 71: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Double pointer

int main() {int a = 3;int *b = &a;int c[5] = {10, 20, 30, 40, 50};int** d = &b;

return 0;}

Address Memory Contents1000

31004

1000(address)

1012

1014

1016

1018

1020

1022

1024

1026

1028

1030

1032

1034

1036

1038

a

b

Page 72: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Double pointer

int main() {int a = 3;int *b = &a;int c[5] = {10, 20, 30, 40, 50};int** d = &b;

return 0;}

Address Memory Contents1000

31004

1000(address)

101210

101620

102030

102440

102850

1032

1034

1036

1038

a

b

c[0]

c[1]

c[2]

c[3]

c[4]

c

Page 73: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Double pointer

int main() {int a = 3;int *b = &a;int c[5] = {10, 20, 30, 40, 50};int** d = &b;

return 0;}

Address Memory Contents1000

31004

1000(address)

101210

101620

102030

102440

102850

1032

1004(address)

a

b

c[0]

c[1]

c[2]

c[3]

c[4]

c

d

Page 74: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Question 02} If we declare int *ptr; and int val;, the

following two usages are valid:} ptr = &val;} *ptr = val;

} What is *val?

Page 75: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Question 02} If we declare int *ptr; and int val;, the

following two usages are valid:} ptr = &val;} *ptr = val;

} What is *val?} It won’t compile} * operator (dereference) implies that what it stores is a

memory address} Only pointer variables store memory address

Page 76: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Different levels of pointersIf we have

int val;int* ptr1;int** ptr2;int*** ptr3;int**** ptr4;int***** ptr5;

Then they have the following relations

ptr1 = &val;

ptr2 = &ptr1;

ptr3 = &ptr2;

ptr4 = &ptr3;

ptr5 = &ptr4;

val = *ptr1;

ptr1 = *ptr2;

ptr2 = *ptr3;

ptr3 = *ptr4;

ptr4 = *ptr5;

Address of Deference

Page 77: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Question 03} If we declare int *ptr;, can we hardcode an address

and assign to ptr?} For example, ptr = 1234;

} No, it won’t compile} For security issue} It doesn’t make sense that we can get an address beforehand} The same variable can reside in different parts of memory in

different executions

Page 78: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Question 03

int main() {int a;

cout << &a << endl;

return 0;}

Page 79: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Question 04} If we declare int *ptr; and double val;, is the

following code valid?} ptr = &val;

Page 80: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Question 04} If we declare int *ptr; and double val;, is the

following code valid?} ptr = &val;

} No, it won’t compile} Pointers are type-aware} We can cast the type: ptr = (int*) &val;} However, that means we use the way we interpret integer to

intepret a piece of memory which stores a double

Page 81: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {double a = 3.5;int *b = (int*) &a;

cout << *b << endl;

return 0;}

Address Memory Contents1000

3.5

1008

1000(address)

1016

1018

1020

1022

1024

1026

1028

1030

1032

1034

1036

1038

a

b

Page 82: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Array in memory

int main() {double a = 3.5;int *b = (int*) &a;

cout << *b << endl;

return 0;}

Address Memory Contents1000

3.5

1008

1000(address)

1016

1018

1020

1022

1024

1026

1028

1030

1032

1034

1036

1038

a

b

Page 83: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Question 05} If we declare int *ptrI; and double *ptrD;,

can we have the following assignment?} ptrI = ptrD;

} No, it won’t compile} Pointer type doesn’t match} Though both store memory addresses, how they interpret the

memory content are different} We can cast the type: ptrI = (int*) ptrD;

Page 84: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Checkpoint – Looping over the array

double* findFirstNegativePtr(double a[], int n) {for (double* p = a; p < a + n; p++) {

if (*p < 0)return p;

}return nullptr;

}

} Return a pointer

int findFirstNegativeIdx(double a[], int n) {for (int i = 0; i < n; i++) {

if (a[i] < 0)return i;

}return -1;

}

} Return an index

Page 85: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Project 6

} Problem 1b probably is the most tricky question.

Page 86: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Road map of CS31

Variable If / else Loops Pointer!!

Array Function Class

String

Page 87: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Class

} Define a data structure} A data structure groups different “variables” together } For example, when we describe a 2d coordinate, naturally

we use 2 numbers to represent it} We can also say we declare a new data type

Page 88: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week08_bo.pdfCheat sheet Data type}int*a;} ais a variable, whose data type is int*} astores an

Example// create a new data typeclass Point {

public:double x;double y;

};

// how we use itint main() {

Point p;p.x = 1.1;p.y = 2.2;

Point r = p;r.y = 3.3;

cout << "Point 1: " << p.x << " " << p.y << endl;cout << "Point 2: " << r.x << " " << r.y << endl;

return 0;}