CS202 Review Session 1 Notes from [Xiangyu Gao](https://xiangyug.github.io/), TA from fall 2021 Edited by Khanh Nguyen, TA Spring 2022 Edited by Jinli Xiao, TA Spring 2023 Edited by Sophia Watts, TA Spring 2024 Edited by Sam Frank, TA Fall 2024 Edited by Yash Paz, TA Spring 2025 1. Intro & Logistics 2. My Experience & Tips for Success 3. Lab infrastructure 4. Lab 1 overview 4.1. C Basics 4.2. DEMO - from scratch & gdb —————————————————————————————————————————————————— 1. Logistics: - 7 optional review sessions in total, 1 hour each. - You can find zoom recording on Brightspace. - Purpose: help with labs overview background knowledge starting point - Ask question at any point: If I can't answer, post on Campuswire (CW). DO NOT ask me if your code is correct. Labs are opportunity for you to learn. —————————————————————————————————————————————————— 2. My Experience & Tips for Success: - I found this class extremely interesting and helpful for understanding how computers actually work. - Use this as motivation. - that being said, being interested isn't enough - To succeed in cs202 1. Start labs on time: - Read lab entirety when released Read instructions before coding Understand overall design - 5 labs - 2 weeks between lab due dates. Exception - lab 4 almost 5 weeks 2. PUT IN THE WORK: - Don't just do the lab. - Keep up with reading. - Do homework (ungraded). DIAGNOSE!!! - Review class notes / handouts. - Do the work yourself! 3. DON'T CHEAT: - not worth the risk 4. Ask questions: - Don't be afraid - I will be happy to answer them. - If I don't know answer, I will find someone who does. —————————————————————————————————————————————————— 3. Lab infrastructure: - 5 labs - You will be using: git github docker various scripts - May feel overwhelming - Don't need to dive too deep into them. - Just remember basic commands and you will be fine. - Just follow instructions on course website. - If questions, post on CW or come to office hours. - I will give overview of how these tools work together. 3. Git: - version control system - Linus Torvalds - track and manage changes to code. - revert to previous versions of their code if necessary. - widely used in industry. - working copy - commit / commit id / 40 digit hex string - local repo - remote repo - basic commands: commit: make checkpoint "snapshot" of working copy clone: make a copy of remote to local machine fetch: update the local copy from the remote push: updates remote refs add: tells git to start paying attention to a file 3. GitHub: - GitHub != Git - hosting website - stores remote Git repositories. - free each lab tests differrent topics of this class but the submission procedure is the same version control process is same 3. Lab Git Repos: - In cs202, you need to work with 3 Git repositories - 1. nyu-cs202/labs - remote repository that contains the starter code for labs. - the course staff will release labs here. - you should not push your work to this repo; push to your private repo instead. - 2. your local repo - this is a copy of your private repo that lives on your computer. - the lab setup lets you fetch the starter code (upstream) to your local machine and work on it there. - you then push your changes to the remote private repo (origin) on GitHub. - 3. nyu-cs202/labs-25sp- - remote repository contains your code/submission for labs. - this repo is private and visible only to you and the course staff. DO NOT share code in this repo with anyone else. 3. Docker: - Standardizes operating system for everyone in class - We will all appear to be using and running programs on Linux - run all labs within docker - git fetch and push outside docker 3. Scripts and Makefile: - Scripts are a set of commands you can run in terminal. By putting them in file, you can run them all at once with single command. For example, ` ./cs202-run-docker is a script that runs Docker container for you. This saves you from remembering long Docker commands and typing them manually each time. - "make" is a tool that helps you automate compilation process. We setup rules in file called 'Makefile' and you can just run each of them with single command. Eg. make, make test, make clean, etc. —————————————————————————————————————————————————— 4. Lab 1 Overview: - Lab 1 should be easier than other labs. - Still, you will - do a lot of setting up, - review your C skills, - familiarize yourself with lab workflow. - Many things can go wrong. - So remember to start on time. - The lab will help you review C and teach you to use gdb. - Try to learn gdb on the go. - It is really powerful and crucial on later labs. - Your skill will improve the most when you are using it. - Part 1: Implementation of functions, writing a basic C program from scratch - Part 2: Debugging using gdb - Part 3: Navigating around linux on shell / terminal 4.1. C Basics: - a. Declaration & initialization - Telling computer to expect/make space in memory for variable ` /* ` * Declaration. Dereferencing x will lead to undefined behavior. ` * Remember to initialize your variables. ` */ ` int* x; - b. Pointer (variable) - Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, which cannot be performed without using pointers. - Definition: An address within memory OR a variable whose value is the address of another variable. - Format: type * variable_name. - '*' denotes the pointer type. Don't confuse this with accessing/dereferencing pointers. - '*' can also be used to access contents. Example: ` int* myFavNum = 6; // pointer to int 6 ` printf("%d\n", *myFavNum); // '6' - *myFavNum is equivalent to accessing the value of what is stored at myFavNum Fun exercise: Try copy the code into your machine. Before running it, read through the code and think what would happen. Then run it to double check your understanding. Change it around and redo the process. - c. Mental Model - I like to think about variables as boxes. So pointer is a box that contains memory address Example: ` int a = 1; ` int* b = &a; ` int** c = &b; In my brain, this is what's going on: ` | var | loc | val | ` +-----+-----+-----+ ` | a | 100 | 5 | ` | b | 104 | 100 | ` | c | 112 | 104 | ` | ... | 120 | ... | - So as you see, b refers to the memory address that store a's value. c refers to address of b. And if there's a dereferencing, you just follow the trail of address. Example: ` int* x = *c; // x will refer to b's value so 104 ` int y = *x; // y will be 1. - d. Array and string - There is no built-in type string in C :O - 2 ways to access element in array: subscript and pointer arithmetic. - Character array is terminated with a null byte, indicating the end of the string. Example: ` // will compile sometimes but this is not correct ` char name[5] = "Alice"; ` ` // correct way to initialize string with length 5. Need 1 more for null byte ` char name[6] = "Alice"; ` ` /* ` * "Alice" is a string constant in read-only region. Write/modify can lead to ` * undefined behavior. ` */ ` const char* name = "Alice"; 4.2. DEMO - from scratch & gdb: DEMO: - create test dir - create main.c - edit main.c using vim - compile using gcc - create Makefile - tab separator fail - fix Makefile - compile using make - add Makefile cleanup routine - gdb is a debugger that allows you to - step through program - inspect state of program at each step. - You currently might use a lot of printf statements. - That's not always the best way - We assume you are already familiar with C. You should spent some decent time in CSO learning C. ` [live gdb demo] - Suggestions for debugging: - Don't try changing your code around without thinking about it first. It will waste lots of your time and is not cost effective. More importantly, sometimes, you won't even know why your code fails and how you ACTUALLY fix it. - Can also use printf or combination of printf + gdb. But printf is also not the greatest strategy. If you notice you rely on using printf too much, try switching to gdb. - Don't be scared of compiler warnings and errors. Compiler, most of the times, is your friend. —————————————————————————————————————————————————— 5. Q&A: References: https://www.geeksforgeeks.org/git-index/ https://cs61.seas.harvard.edu/site/ref/git/#gsc.tab=0