class: title # Managing Memory ## CSC 161 – March 31, 2026 --- # Agenda for Today 1. Department Events 2. CS Registration 2. Upcoming Work 3. Discussion 4. Lab 5. Wrap Up --- # Department Events ## CS Table: Web Bloat .indent[ Tuesday, March 31st at noon in JRC 224C ] --- # CS Registration If you want to study more computer science, what should you take? -- ## CSC 207 – Object Oriented Problem Solving Dig deeper into data structures, algorithms, and larger scale software development. -- ## CSC 211 – Computer Organization and Architecture *(spring only)* How does computation actually happen within a computer? Learn by building circuits, both real and simulated. You'll start with transistors and work up to a processor that can run programs. -- ## CSC 213 – Operating Systems and Parallel Algorithms *(fall only)* What is going on below your programs? Learn by implementing many of the features that an OS provides. You'll learn to solve problems by computing *in parallel* on the CPU and GPU. -- ## Considering a CS Major? You should aim to complete the major math requirement in your second year. You'll need to take MAT 218 or CSC 208 and one MAT/STA course numbered above 131. --- class: section blue # Upcoming Work --- # Week 8 Lab Report Submit your maze editor work by 10pm tonight. ## Morning Section You only need to complete work from Tuesday's lab. ## Afternoon Section Complete work from both lab days last week. --- # Week 9 Quiz We'll resume regular quizzes starting this Thursday. The quiz will last 10 minutes, and could cover any of these new topics: 1. Managing memory with `malloc` and `free` 2. Resizing allocated memory with `realloc` 3. The `const` keyword (from Thursday's reading) 4. AddressSanitizer's (from Thursday's reading, high level details only) You should expect to see more challenging questions about concepts we've practiced in lab today, and easier questions for any material from Thursday's reading. --- # Homework: Lightcycle **Due:** Thursday, April 9th ## Recommendations You might find it helpful *not* to use concepts from today's class on this assignment, although they are allowed if you do find a use for them. ## Questions? *None so far.* --- class: section blue # Discussion --- # Discussion: Allocating Memory **Items for think, pair, share:** 1. We already know how to reserve memory by declaring local and global variables. Why would we use `malloc` instead of just making regular variables? 2. Consider a program with variable `char* p`. Let's assume `p` points to memory returned from `malloc`. What happens when we call `free(p)`? 3. Why do we need to call `free` for all memory we allocate? --- # Box and Arrow Diagrams with `malloc` Work with your neighbor to draw a box and arrow diagram for the following code: ```c int a = 1; int* b = malloc(sizeof(int)); int* c = b; *c = 2; c = &a; free(b); ``` --- class: section green # Lab Time --- class: section blue # Wrap Up --- # Wrap Up ## Lab Report Submit your work from part D with this week's lab report. ## Lightcycle Assignment Start early! I am always happy to discuss your approach to the assignment or any issues you've run into. ## Mentor Session Attend tomorrow's mentor session from 7–9pm to practice `malloc` and `free` and review for the quiz. ## Office Hours I'm happy to meet to discuss how things are going in the class, or to help with specific concepts if you are having trouble. Book a time at
.