CS31 Discussion 1E Spring 17’: Week 08

CS31 Discussion 1E Spring 17’: Week 08

CS31 Discussion 1E Spring 17’: week 08 TA: Bo-Jhang Ho [email protected] Credit to former TA Chelsea Ju 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 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; Road map of CS31 String Array Function Class Variable If / else Loops Pointer!! 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 Memory model Address Memory Contents 1000 1 byte 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 Memory model Address Memory Contents 1000 1 byte = 8 bits 1001 1002 1003 off on 1004 1005 1006 1007 1008 1009 A bit has 2 states 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 Memory model Address Memory Contents 1000 1 byte = 8 bits 1001 1002 1003 off on 1004 1005 1006 1007 1008 1009 A bit has 2 states 1010 8 1011 A byte has 256 (=2 ) states 1012 1013 1014 1015 1016 1017 1018 1019 Memory model Address Memory Contents 1000 1 byte = 8 bits 1001 1002 1003 off on 1004 1005 1006 1007 1008 1009 A bit has 2 states 1010 8 1011 A byte has 256 (=2 ) states 1012 1013 1014 A char takes 1 byte 1015 An int takes 4 bytes 1016 1017 A double takes 8 bytes 1018 1019 Data type review } Basic data types Ty p e 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 Memory model Address Memory Contents 1000 1 byte = 8 bits 1001 1002 1003 off on 1004 1005 1006 1007 1008 1009 A bit has 2 states 1010 8 1011 A byte has 256 (=2 ) states 1012 1013 1014 1015 My has 16GB ram 1016 1017 1018 1019 Memory model Address Memory Contents 1000 1 byte = 8 bits 1001 1002 1003 off on 1004 1005 1006 1007 1008 1009 A bit has 2 states 1010 8 1011 A byte has 256 (=2 ) states 1012 1013 1014 1015 My has 16GB ram 1016 1017 1018 = 16,000,000,000 bytes! 1019 What’s going on in the memory Address Memory Contents 1000 int main() { 1001 int a = 5; 1002 int b = 3; 1003 double c = 3.5; int d = b - a; 1004 1005 return 0; 1006 } 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 5 int b = 3; double c = 3.5; int d = b - a; 1004 1005 return 0; 1006 } 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 5 int b = 3; double c = 3.5; int d = b - a; 1004 b 3 return 0; } 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 5 int b = 3; double c = 3.5; int d = b - a; 1004 b 3 return 0; } 1008 c 3.5 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 5 int b = 3; double c = 3.5; int d = b - a; 1004 b 3 return 0; } 1008 c 3.5 1016 d 2 What’s going on in the memory Address Memory Contents 1000 int main() { 1001 int a = 5; 1002 int *b = &a; 1003 a++; 1004 *b += 2; 1005 1006 cout << a << endl; 1007 cout << *b << endl; 1008 1009 return 0; 1010 } 1011 1012 1013 1014 1015 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 5 int *b = &a; a++; 1004 *b += 2; 1005 1006 cout << a << endl; 1007 cout << *b << endl; 1008 1009 return 0; 1010 } 1011 1012 1013 1014 1015 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 5 int *b = &a; a++; 1004 *b += 2; 1005 1006 cout << a << endl; 1007 cout << *b << endl; 1008 1009 return 0; 1010 } 1011 1012 1013 1014 1015 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 5 int *b; // declare a pointer var b = &a; // set address 1004 a++; 1005 *b += 2; 1006 1007 cout << a << endl; 1008 cout << *b << endl; 1009 1010 return 0; } 1011 1012 1013 1014 1015 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 5 int *b; // declare a pointer var b = &a; // set address 1004 a++; *b += 2; b ?? cout << a << endl; (address) cout << *b << endl; return 0; } 1012 1013 1014 1015 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 5 int *b; // declare a pointer var b = &a; // set address 1004 a++; *b += 2; b 1000 cout << a << endl; (address) cout << *b << endl; return 0; } 1012 1013 1014 1015 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 5 à 6 int *b; // declare a pointer var b = &a; // set address 1004 a++; *b += 2; b 1000 cout << a << endl; (address) cout << *b << endl; return 0; } 1012 1013 1014 1015 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 6 à 8 int *b; // declare a pointer var b = &a; // set address 1004 a++; *b += 2; b 1000 cout << a << endl; (address) cout << *b << endl; return 0; } 1012 1013 1014 1015 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 8 int *b; // declare a pointer var b = &a; // set address 1004 a++; *b += 2; b 1000 cout << a << endl; (address) cout << *b << endl; return 0; } 1012 1013 } Output 1014 1015 } 8 1016 1017 1018 1019 What’s going on in the memory Address Memory Contents 1000 int main() { a int a = 5; 8 int *b; // declare a pointer var b = &a; // set address 1004 a++; *b += 2; b 1000 cout << a << endl; (address) cout << *b << endl; return 0; } 1012 1013 } Output 1014 1015 } 8 1016 8 1017 1018 1019 Using uninitialized variables is always dangerous Address Memory Contents 1000 int main() { 1001 int a; 1002 int b = a + 3; 1003 int *c; 1004 *c = 27; 1005 1006 return 0; 1007 } 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 Using uninitialized variables is always dangerous Address Memory Contents 1000 int main() { a int a; ?? int b = a + 3; int *c; 1004 *c = 27; 1005 1006 return 0; 1007 } 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 Using uninitialized variables is always dangerous Address Memory Contents 1000 int main() { a int a; ?? int b = a + 3; int *c; 1004 *c = 27; b ?? return 0; } 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 Using uninitialized variables is always dangerous Address Memory Contents 1000 int main() { a int a; ?? int b = a + 3; int *c; 1004 *c = 27; b ?? return 0; } 1008 c ?? (address) 1016 1017 1018 1019 Using uninitialized variables is always dangerous Address Memory Contents 1000 int main() { a int a; ?? int b = a + 3; int *c; 1004 *c = 27; b ?? return 0; } 1008 c ?? } May manipulate a piece of (address) memory not belonging to this program!! 1016 1017 1018 1019 Array v.s. Pointer Array is a special case of pointer Pointer can be treated as an array Array in memory Address Memory Contents 1000 int main() { 1002 int a = 3; 1004 int b[5] = {10, 20, 30, 40, 50}; 1006 int *c = &b[1]; 1008 *c = 100; 1010 1012 return 0; 1014 } 1016 1018 1020 1022 1024 1026 1028 1030 1032 1034 1036 1038 Array in memory Address Memory Contents a 1000 int main() { 3 int a = 3; 1004 int b[5] = {10, 20, 30, 40, 50}; 1006 int *c = &b[1]; 1008 *c = 100; 1010 1012 return 0; 1014 } 1016 1018 1020 1022 1024 1026 1028 1030 1032 1034 1036 1038 Array in memory Address Memory Contents a 1000 int main() { 3 int a = 3; int b[5] = {10, 20, 30, 40, 50}; b[0] 1004 10 int *c = &b[1]; 1008 b[1] 20 *c = 100; 1012 b b[2] 30 return 0; } 1016 b[3] 40 1020 b[4] 50 1024 1026 1028 1030 1032 1034 1036 1038 Array in memory Address Memory Contents a 1000 int main() { 3 int a = 3; int b[5] = {10, 20, 30, 40, 50}; b[0] 1004 10 int *c = &b[1]; 1008 b[1] 20 *c = 100; 1012 b b[2] 30 return 0; } 1016 b[3] 40 1020 b[4] 50 1024 1026 1028 1030 1032 1034 1036 1038 Array in memory Address Memory Contents a 1000 int main() { 3 int a = 3; int b[5] = {10, 20, 30, 40, 50}; b[0] 1004 10 int *c; // declare a pointer c = &b[1]; // get address 1008 b[1] 20 *c = 100; 1012 b b[2] 30 return 0; 1016 } b[3] 40 1020 b[4] 50 1024 1026 1028 1030 1032 1034 1036 1038 Array in memory Address Memory Contents a 1000 int main() { 3 int a = 3; int b[5] = {10, 20, 30, 40, 50}; b[0] 1004 10 int *c; // declare a pointer c = &b[1]; // get address 1008 b[1] 20 *c = 100; 1012 b b[2] 30 return 0; 1016 } b[3] 40 1020 b[4] 50 1024 c ?? (address) 1032 1034 1036 1038 Array in memory Address Memory Contents a 1000 int main() { 3 int a = 3; int b[5] = {10, 20, 30, 40, 50}; b[0] 1004 10 int *c; // declare a pointer c = &b[1]; // get

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    88 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us