class: middle, center # Lecture 6 ### 14 September 2020 .smaller[ Admin Matters
Unit 13: **Call Stack**
Unit 14: **Pointer**
Unit 15: **Array**
Unit 16: **String**
] --- class: middle ### Assignment 2 - Due tomorrow night (23:59) (XX has completed) - Graded on correctness (including syntax, practices, approach, and logic), style, and efficiency. --- class: middle ### Reminders - Make sure that your submission compiles cleanly without errors or warnings. (0 if cannot compile; -1 per warning) - Marks will be deducted if you do not demonstrate a full understanding of what we taught so far. --- class: middle # Warning ### Plagiarism will not be tolerated. You may discuss how to solve the assignment, but make sure you code up the solution yourself. --- ### Upcoming releases: - Exercise 3 on Tuesday - Practical Exam I from 18/19 on Tuesday - Assignment 3 on Thursday - Grading criteria: same as Assignment 2 --- class: middle ### Reminders - **Catch-up II**: 21 September, 2020 (Monday) 12noon - **Midterm**: 28 September, 2020 (Monday) - **PE I**: 3 October 2020 (Saturday) --- class: middle # Warning ### Pay attention to exam regulations. Disciplinary actions will be taken against violators. --- class: middle ### Tutorial/Lab 4 - Problem Sets 12-13 - Programming Exercises --- class: middle,center # Call Stack --- .cols[ .fifty[ .tiny[ ```C int main() { long x = 1; long y; } ``` ] ] .fifty[ ]] --- .cols[ .fifty[ .tiny[ ```C int main() { long x = 1; long y; } ``` ] ] .fifty[ .fit[![call-stack](figures/stack/stack.001.png)] ]] --- .cols[ .fifty[ .tiny[ ```C long add(long a, long b) { long sum; sum = a + b; return sum; } int main() { long x = 1; long y; y = add(x, 10); } ``` ] ] .fifty[ ]] --- .cols[ .fifty[ .tiny[ ```C long add(long a, long b) { long sum; sum = a + b; return sum; } int main() { long x = 1; long y; y = add(x, 10); } ``` ] ] .fifty[ .fit[![call-stack](figures/stack/stack.002.png)] ]] --- .cols[ .fifty[ .tiny[ ```C long add(long a, long b) { long sum; sum = a + b; return sum; } int main() { long x = 1; long y; y = add(x, 10); } ``` ] ] .fifty[ .fit[![call-stack](figures/stack/stack.004.png)] ]] --- .cols[ .fifty[ .tiny[ ```C long add(long a, long b) { long sum; sum = a + b; a = 42; return sum; } int main() { long x = 1; long y; y = add(x, 10); } ``` ] ] .fifty[ .fit[![call-stack](figures/stack/stack.003.png)] ]] --- .cols[ .fifty[ .tiny[ ```C long add(long a, long b) { long sum; sum = a + b; a = 42; return sum; } int main() { long x = 1; long y; y = add(x, 10); } ``` ] ] .fifty[ .fit[![call-stack](figures/stack/stack.004.png)] ]] --- .cols[ .fifty[ .tiny[ ```C long add(long a, long b) { long sum; sum = a + b; a = 42; return sum; } int main() { long x = 1; long y; y = add(x, 10); } ``` ] ] .fifty[ .fit[![call-stack](figures/stack/stack.004.png)] ]] --- .cols[ .fifty[ .tiny[ ```C void add(long sum, long a, long b) { sum = a + b; } int main() { long x = 1; long sum; add(sum, x, 10); } ``` ] ] .fifty[ ]] --- .cols[ .fifty[ .tiny[ ```C void add(long sum, long a, long b) { sum = a + b; } int main() { long x = 1; long sum; add(sum, x, 10); } ``` ] ] .fifty[ .fit[![call-stack](figures/stack/stack.005.png)] ]] --- .cols[ .fifty[ .tiny[ ```C void add(long sum, long a, long b) { sum = a + b; } int main() { long x = 1; long sum; add(sum, x, 10); } ``` ] ] .fifty[ .fit[![call-stack](figures/stack/stack.006.png)] ]] --- .cols[ .fifty[ .tiny[ ```C void add(long sum, long a, long b) { sum = a + b; } int main() { long x = 1; long sum; add(sum, x, 10); } ``` ] ] .fifty[ .fit[![call-stack](figures/stack/stack.007.png)] ]] --- class: middle ## "Address of" operator `&` - Every variable is stored some location in the memory - `&x` gives us the address of the memory location where `x` is stored - `&x` has the type "address of T" where T is the type of `x`. --- class: middle ## "Pointer" operator `*` - Suppose we have a memory address `a`. `a` has the type address of T. - `*a` tells us the value of type T stored in address `a`. --- .cols[ .fifty[ .tiny[ ```C int main() { long x; long *ptr; ptr = &x; *ptr = 1; } ``` ] ] .fifty[ .fit[![call-stack](figures/stack/stack.008.png)] ]] --- .tiny[ ```C #include "cs1010.h" void add(long sum, long a, long b) { sum = a + b; cs1010_println_long((long)&sum); } int main() { long x = 1; long sum; add(sum, x, 10); cs1010_println_long((long)&sum); } ``` ] --- .cols[ .fifty[ .tiny[ ```C double *addr; *addr = 1.0; // danger! ``` ] ] .fifty[ .fit[![call-stack](figures/stack/stack.009.png)] ]] --- .cols[ .fifty[ .tiny[ ```C double c; double *addr; addr = &c; *addr = 1.0; // same as c = 1.0; ``` ] ] .fifty[ .fit[![call-stack](figures/stack/stack.010.png)] ]] --- class: middle A pointer to some type T can only points to a variable of type T ```C double pi = 3.1415926; long radius = 5; double *addr; addr = &pi ; // ok addr = &radius; // not ok ``` --- class: middle We cannot change the address of a variable (but can change what a pointer is pointing to) ```C long x = 1; long y = 2; &x = &y; ``` --- class: middle We can perform add and subtract on pointers ```C long x; long *ptr; x = 1; ptr = &x; ptr += 1; ``` Note: The value of `ptr` increases by one "unit", not one byte. --- class: middle, center # Array --- class: middle, center Array is a _compound data type_ that stores multiple values of the same type --- class: middle ```C long marks[10]; ``` `marks` is an array that stores 10 `long` values. --- class: middle ```C long marks[10]; // declare marks[4] = 100; // write ``` `marks[4]` refers to the 5th element in the array `marks`. --- class: middle #### Passing in array as a parameter .tiny[ ```C // function declaration long max(long list[], long len) { // empty [] : } : long marks[10]; // function invocation max(marks, 10); // just the name of the array ``` ] --- class: middle #### Using array as a lookup table .tiny[ ```C // Exercise 1: Days long days(long month) { long days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; long days_since = 0; for (long i = 0; i < month - 1; i += 1) { days_since += days_in_month[i]; } return days_since; } ``` ] --- class: middle #### Using array as a list .tiny[ ```C long max(long list[], long length) { long max_so_far = list[0]; for (long i = 1; i != length; i += 1) { if (list[i] > max_so_far) { max_so_far = list[i]; } } return max_so_far; } ``` ] --- An array is stored contiguously in memory. .fit[![array](figures/stack/stack.011.png) --- ### Array decay ``` long marks[10]; ``` `marks` decays into `&marks[0]` --- class:middle .tiny[ ```C long a[2] = {0, 1}; long b[2] = {0, 1}; if (a == b) { // always false : } b = a; // not possible ``` ] --- class: middle .tiny[ ```C long max(long list[], long len) { // only pointer is passed in } : long marks[10]; max(marks, 10); // passing in only the pointer ``` ] --- class: middle #### Common bug: Index out of bound .tiny[ ```C int main() { long a[10]; for (long i = 0; i <= 10; i += 1) { a[i] = 1; } } ``` ] --- class: middle Lots more details about C arrays in the notes. Including: - array initialization - avoid using variable-length array - how to read arrays with CS1010 I/O library - determining the size of the array --- class: middle,center # String --- class: middle ### String in C - Just an array of `char` values - Always terminated by a value 0 (aka null or `\0`) --- class: middle ### Special Characters - `\0`: null character - `\n`: new line - `\t`: tab e.g., .tiny[ ```C cs1010_println_string("group\tname\nC03\tJohn\n"); ``` ] --- class: middle ### String Literals - Unmodifiable string between two double-quotes. - Stored not on the stack but in a read-only region of memory. --- class: middle ```C char *str1 = "hello!" str1[0] = 'j'; // error ``` `str1` is a pointer on the stack, pointing to a read-only memory. --- class: middle ```C char str2[7] = "hello!" str2[0] = 'j'; // ok ``` `str2[7]` is an array and contains a copy of the string "hello" on the stack. --- class: middle ### Common Bug I ```C char src[6] = "hello!" char dst[6]; for (long i = 0; i < 6; i += 1) { dst[i] = src[i]; } ``` --- class: middle ### Common Bug II ```C char src[6] = "hello!" char dst[6]; strcpy(dst, src); ``` --- class: bottom .tiny[ Version: 1.1 Last Updated: Mon Sep 14 11:12:21 +08 2020 ]