← CS50: Introduction to Computer Science

Week 4: Memory

Memory made physical: addresses as boxes on a grid, pointers as arrows, malloc carving out space, and what happens when code writes past the end.

10 topics · 37 questions

Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.

Hexadecimal Numbers

  1. How it worksHow does each hexadecimal digit stand in for exactly four binary digits, no more and no less?
  2. Worked exampleFind the hexadecimal representation of the decimal number 255.
  3. Common mistakeWhy isn't 0x10 in hexadecimal equal to 10 in decimal?
Your turn1 of 2
A 2-digit hexadecimal number has its high digit equal to 10 and low digit equal to 10 (each between 0 and 15). What decimal value does this hex number represent?

Pointers and Memory Addresses

  1. How it worksHow does the ampersand operator hand you the address where a variable actually lives in memory?
  2. How it worksHow does the dereference operator follow a pointer to reach the value sitting at the address it holds?
  3. Worked exampleWalk me through what a pointer variable holds after int·p = &x, where x lives at address 2000.
  4. Common mistakeWhy isn't a pointer just another name for the variable it points to?
  5. Going furtherWhat happens to a pointer's value the moment the variable it points to goes out of scope?
Your turn1 of 2
A pointer is set with int·p = &x, where x lives at memory address 6571. What address does p hold?

Pointer Arithmetic and Arrays

  1. How it worksHow does an array's name silently decay into a pointer to its very first element?
  2. How it worksHow does adding 1 to an int pointer move it forward by 4 bytes instead of just 1 byte?
  3. Worked exampleFind the memory address that arr + 3 points to, given arr starts at address 2000 and holds 4-byte ints.
  4. Common mistakeWhy doesn't incrementing a pointer always move it forward by exactly one byte?
Your turn1 of 2
Find the memory address that arr + 13 points to, given arr starts at address 5875 and holds 4-byte ints.

Dynamic Memory Allocation with malloc

  1. How it worksHow does malloc carve out a block of a requested size from the heap and hand back a pointer to it?
  2. Common mistakeWhy doesn't malloc automatically initialize the memory it hands you to all zeros?
  3. Worked exampleWalk me through calling malloc to allocate space for an array of 10 ints, given each int is 4 bytes.
  4. Going furtherWhat happens to a program the moment malloc runs out of memory to give and returns a null pointer?
Your turn1 of 2
A program calls malloc to allocate space for an array of 70 ints, where each int is 4 bytes. How many total bytes does malloc need to allocate?

The Stack and the Heap

  1. Worked exampleWalk me through how calling three nested functions pushes three new frames onto the stack, one atop the last.
  2. Common mistakeWhy isn't the heap just an extension of the stack that happens to be used less often?
  3. Going furtherWhat happens when the stack and the heap grow toward each other far enough to actually collide?
Your turn1 of 2
Calling 21 nested functions, each pushing exactly one new frame onto the stack, leaves how many frames on the stack right before the innermost one returns?

Dangling Pointers and Memory Leaks

  1. How it worksHow does calling free on a pointer release its memory back to the heap without erasing what was written there?
  2. Common mistakeWhy isn't calling free on a pointer the same thing as erasing the memory it points to?
  3. Going furtherWhat happens to a program's available memory when it allocates memory in a loop and never calls free?
Your turn1 of 2
A program allocates 35 separate blocks of 133 bytes each in a loop and never calls free. How many total bytes has it leaked?

Finding Memory Bugs with Valgrind

  1. How it worksHow does valgrind track every malloc and free to report exactly which allocations were never freed?
  2. Worked exampleWalk me through reading a valgrind report that says 40 bytes were definitely lost in one block.
  3. Going furtherWhat happens to valgrind's report when a program frees the same pointer twice?
Your turn1 of 2
A valgrind report says 2798 bytes were definitely lost across 14 separate blocks that were never freed, evenly split. How many bytes were lost per block?

Buffer Overflow

  1. How it worksHow does writing past the end of a stack-allocated array overwrite whatever memory sits right after it?
  2. Common mistakeWhy doesn't C stop you from writing past the last valid index of an array?
  3. Going furtherWhat happens when a buffer overflow overwrites a function's saved return address on the stack?
  4. Ties togetherHow does a buffer overflow connect back to arrays not knowing their own length in the first place?
Your turn1 of 2
A stack-allocated char array has 22 bytes reserved, but a program writes 51 bytes into it. How many bytes overflow past the end of the array?

File I/O in C

  1. How it worksHow do fopen, fread, fwrite, and fclose walk a program through a file's entire lifecycle?
  2. Worked exampleWalk me through opening a file in read mode with fopen and reading 4 bytes into a buffer with fread.
  3. Common mistakeWhy doesn't forgetting to call fclose necessarily lose the data a program already wrote?
Your turn1 of 2
A program calls fread to read 70 records of 4 bytes each from a file into a buffer. How many total bytes does it read?

Manipulating BMP Images

  1. How it worksHow does a BMP file store a header describing the image, followed by a grid of pixel values?
  2. Worked exampleFind the grayscale value produced by averaging a pixel with red 100, green 150, and blue 200.
  3. Common mistakeWhy isn't flipping an image horizontally just a matter of relabeling which side counts as left?
  4. Going furtherWhat happens to a blur filter's output at the very edge pixels of an image, where some neighbors don't exist?
  5. Ties togetherHow does reading a BMP file pixel by pixel connect back to arrays being laid out one element after another in memory?
Your turn1 of 2
Find the grayscale value produced by averaging a pixel with red 178, green 165, and blue 238.