Before class · do this at home
Watch, read, then come
CS50x 2026 · Week 4: Memory by David J. Malan. Watch the lecture on YouTube (you are also enrolled on edX), read the notes, skim the slides. Our lecture builds on it instead of repeating it.
- This is the hardest week of the course. Watch the lecture twice if needed; the second time, draw the memory diagrams on paper as Malan draws them.
- Open Python Tutor for C and step through the lecture's
swap.c.
Learning objectives
By the end of this week you can:
- 1 Read and write hexadecimal, and convert between hex, binary and decimal.
- 2 Explain what an address is, and use & (address of) and * (dereference) correctly.
- 3 State that a string is a char*, and explain why string comparison needs strcmp and copying needs a loop or strcpy.
- 4 Allocate memory with malloc, release it with free, and use valgrind to find leaks.
- 5 Draw the stack and the heap, and explain why swap needs pointers and what a segmentation fault is.
- 6 Read and write files with fopen, fread, fwrite and fclose, and describe an image as a header plus pixel bytes.
★ the wow moment
‘Enhance!’ is impossible: a photo is a finite grid of bytes, and there is nothing behind them.
Every crime show zooms into a reflection to read a license plate. A 3-megapixel image is 9 million bytes and that is all the information there is; zooming shows bigger squares. This week you open image files byte by byte and see that a photo is just a header saying 'width, height' followed by red, green, blue, red, green, blue. Once you see it, you can recover deleted photos from a memory card, which is exactly what CS50's recover.c does.
Topics
- Hexadecimal: base 16,
0xprefix, two hex digits per byte, RGB as#RRGGBB. - Addresses and pointers:
&x,int *p = &x;,*p = 50;. Pointers are just numbers that happen to be addresses. - Strings, revealed:
stringischar *;s[i]is*(s + i); comparing with==compares addresses (wrong);strcmp,strcpy, and copying by hand. - Dynamic memory:
malloc,free,NULLchecks, memory leaks,valgrind. - Stack vs heap: local variables and function frames on the stack;
mallocon the heap; whyswap(int a, int b)cannot work andswap(int *a, int *b)can. - Files:
fopen,fread,fwrite,fclose; JPEG signatures; BMP headers and pixels;scanfand whyget_stringexists.
Full notes are published before the lecture. The lab, Lab 5: Pointers and Pixels, covers swap, string copying, malloc/free with valgrind, and a grayscale filter on a BMP.
In industry
-
Memory bugs are the security headlines
Microsoft and Google report that about 70% of their serious security vulnerabilities are memory-safety bugs: use-after-free, buffer overflows, dangling pointers. Everything you learn this week is what those bugs are made of.
-
malloc is a whole subsystem
Allocators like jemalloc and tcmalloc are engineering marvels that every server process depends on. Facebook and Google each maintain their own.
-
Hex is the language of the low level
Memory addresses, colors in CSS (#1a2b3c), MAC addresses, error codes (0xC0000005), file signatures (FF D8 for JPEG). Reading hex fluently is a mark of a real engineer.
-
Rust exists because of this week
Rust is C's ideas with a compiler that refuses to build programs with these bugs. It is now in the Linux kernel, Android and Windows. You cannot appreciate it until you have felt the pain.
Lab 5: Pointers and Pixels
Swap through pointers, copy a string by hand, allocate and free memory without leaks, and turn a color BMP image gray by editing its bytes. C · due Fri, Oct 30 · 23:59 · 10 points · opens in week 6 Open lab instructions →Go deeper
- CS50x 2026 · Week 4 notes
- Pointer Fun with Binky (video) · Stanford's 3-minute claymation on pointers. Yes, really.
- Python Tutor for C · Step through any C program and watch memory.
- Valgrind quick start
Tags:pointershexadecimaladdressmallocfreevalgrindstackheapsegmentation faultstrcmpfile I/OBMP