class: middle, center # Lecture 6 ### 16 September 2024 .smaller[ Admin Matters
Unit 13: **Call Stack**
Unit 14: **Array**
] --- ### Upcoming Events - **Catch-up**: 21 September 2024 (Saturday) - **PE 0**: 1 October 2024 (Tuesday) - **Midterm**: 7 October 2024 (Monday) --- ### Catch-Up Session - __Date__: Saturday, 21 September 2024 - __Time__: 10 AM to 12 PM - Via [Zoom](https://nus-sg.zoom.us/j/7308691994?omn=84516650559) - Ask/upvote questions on [PigeonHole](https://pigeonhole.at/ASSERTEVERYWHERE) --- ### Practical Exam 0 - **Date**: 1 October 2024 (Tuesday) - **Time**: 6 - 9 PM - **Duration**: 2 hours - **Venue**: Programming labs in AS6/COM1/COM4 - **10%** of your grade - `ssh` into restricted PE hosts with special exam account using lab PCs --- ### Midterm - **Date**: 7 October 2024 (Monday) - **Time**: 4:30 - 6:15 PM - **Duration**: 80 minutes - **Venue**: Multi-purpose Sports Hall 2B (MPSH2B) - **15%** of your grade - Pen and paper --- ### Timetable Clash? Please submit a ticket through [CS1010 RT system](https://mysoc.nus.edu.sg/app/cs1010/) before 21 September 2024 (Monday). --- ### [Anon Feedback on the Teaching Team](https://canvas.nus.edu.sg/courses/62224/quizzes/40018) - Please let us know how we are doing - Constructive criticism and encouragement are both welcomed - Due: 27 September, 2024 (Friday) --- class: ### Errata ```C // cond A while (cond B) { // ? } // ?? ``` --- class: middle,center ## Call Stack --- .cols[ .fifty[ .smaller[ ```C int main() { long x = 1; long y; } ``` ] ] .fifty[ ]] --- .cols[ .fifty[ .smaller[ ```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); cs1010_println_long(y); } ``` ] ] .fifty[ ]] --- .cols[ .fifty[ .smaller[ ```C void add(long sum, long a, long b) { sum = a + b; } int main() { long x = 1; long sum; add(sum, x, 10); cs1010_println_long(sum); } ``` ] ] .fifty[ ]] --- - Stack size is limited. - "Stack overflow" happens when we ran out of stack space. --- class: middle, center ## Array --- class: middle, center Array is a _compound data type_ that stores multiple values of the _same_ type. --- ### Declaring an Array ```C long list[10]; ``` `list` is an array that stores 10 `long` values. --- ### Accessing Elements of an Array ```C list[4] = 100; // write i = list[x]; // read ``` - Indexing starts at 0. - `list[4]` refers to the 5th element in the array `list`. --- ### Declaring and Initializing an Array .small[ ```C long list[10] = {1, 2, 3, 1, 5, 10, 10, 4, }; ``` ] Unspecified elements initialized to 0. --- class:center,middle ### Example: Array as a Lookup Table --- class: wide ### Array as Function Parameter .small[ ```C long max(long list[10]) { .. } long max(long len, long list[]) { .. } ``` ] --- .smaller[ ```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; } ``` ] --- class: wide ### Array as Argument .small[ ```C int main() { long list[9] = {4, 1, -4, 0, 9, 9, 3, 5, 8}; long list_max = max(list, 9); } ``` ] --- ### Arrays are Passed by Reference .cols[ .fifty[ .smaller[ ```C void foo(long a0, long a1) { a0 = 1; a1 = 2; } int main() { long a0 = 100; long a1 = 200; foo(a0, a1); // { a0 == 100 } } ``` ] ] .fifty[ ]] --- ### Arrays are Passed by Reference .cols[ .fifty[ .smaller[ ```C void foo(long a[2]) { a[0] = 1; a[1] = 2; } int main() { long a[2] = {100, 200}; foo(a); } ``` ] ] .fifty[ ]] --- ### Functions can now have side-effects .cols[ .fifty[ .smaller[ ```C void foo(long a[2]) { a[0] = 1; a[1] = 2; } int main() { long a[2] = {100, 200}; foo(a); // { a[0] == ?? } } ``` ] ] .fifty[ ]] --- ### C keyword `const` - indicate the intention that the array should not be modified. .cols[ .fifty[ .smaller[ ```C void foo(const long a[2]) { a[0] = 1; // now illegal } int main() { long a[2] = {100, 200}; foo(a); // { a[0] == 100 } } ``` ] ] .fifty[ ]] --- ### Recap A variable is a location in memory that holds a value. Every variable has a **value**, a **name**, a **type**, and an **address**. We refer to a variable through its **name** or its **address**. --- ### Pointers - Memory address is a type in C. - Pointers are variables that stores a memory address. ```C double *ptr; long *addr; ``` --- ### Array Decay ```C long list[10]; long *first_elem = list; ``` Elements of an array are stored contiguously in memory. `list` decays into the memory address of its first element. --- .small[ ```C long a[2] = {0, 1}; long b[2] = {0, 1}; if (a == b) { // always false : } b = a; // not possible ``` ] --- .small[ ```C long max(long list[], long len) { // only pointer is passed in } : long list[10]; max(list, 10); // pass in pointer to list ``` ] --- ### Passing an Array into Function .small[ ```C long max(long *list, long len) { // only pointer is passed in } : long list[10]; max(list, 10); // pass in pointer to list ``` ] --- ### Returning an Array (by returning the pointer to the first element of the array) .small[ ```C long* square(long list[], const long len) { for (long i = 0; i != len; i += 1) { list[i] = list[i] * list[i]; } return list; } ``` ] --- ### Common bug: VLA / Pointers to Stack .smaller[ ```C long* square(const long list[], const long len) { long new_list[len]; for (long i = 0; i != len; i += 1) { new_list[i] = list[i] * list[i]; } return new_list; } ``` ] --- ### Common bug: Index out of bound .smaller[ ```C int main() { long a[10]; for (long i = 0; i <= 10; i += 1) { a[i] = 1; } } ``` ] --- class: bottom .tiny[ ]