Introduction to Computer Science YMT113 · Fall 2026 Assoc. Prof. Ferhat Uçar
06 Algorithms & Memory Monday, October 26, 2026

Memory: Pointers, the Heap and Hexadecimal

The week that takes the hood off. Addresses, pointers, why a string was a char* all along, malloc and free, the stack and the heap, segmentation faults, and files and images as bytes.

CS50Week 4 · Memory ReadK&R chapter 5.1–5.6; CS50 notes >_ Lab 5: Pointers and Pixels

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

  1. Hexadecimal: base 16, 0x prefix, two hex digits per byte, RGB as #RRGGBB.
  2. Addresses and pointers: &x, int *p = &x;, *p = 50;. Pointers are just numbers that happen to be addresses.
  3. Strings, revealed: string is char *; s[i] is *(s + i); comparing with == compares addresses (wrong); strcmp, strcpy, and copying by hand.
  4. Dynamic memory: malloc, free, NULL checks, memory leaks, valgrind.
  5. Stack vs heap: local variables and function frames on the stack; malloc on the heap; why swap(int a, int b) cannot work and swap(int *a, int *b) can.
  6. Files: fopen, fread, fwrite, fclose; JPEG signatures; BMP headers and pixels; scanf and why get_string exists.

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

Tags:pointershexadecimaladdressmallocfreevalgrindstackheapsegmentation faultstrcmpfile I/OBMP