Lecture notes · CS50: Programming in C · Lecture 4

Memory

Harvard CS50 Introduction to Computer Science, lecture 4, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: the course website.

Part 1 · 0:00–10:00

Pictures on a computer are grids of tiny dots, and each dot is stored as a pattern of zeros and ones.

Key ideas

Words to know

pixel
One tiny dot on a screen. A picture is a grid of these dots.
resolution
How many dots or pixels fit on a screen. More dots usually means a clearer picture.
RGB
Short for red, green, blue. It is a way of making any color by mixing amounts of these three colors.
hexadecimal
A way of writing numbers using 16 symbols: the digits 0 to 9 and the letters A to F. It is a shorter way to write long binary patterns.

Example

Think of a mosaic made of small square tiles, where each tile is one solid color. From far away you see a clear picture, but if you zoom in close, you only see the tiles. Images on a computer work the same way, with pixels as the tiles.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part explains hexadecimal (base 16) numbers and shows how computers use numbered addresses to find bytes in memory.

Key ideas

Words to know

hexadecimal
A way of counting in base 16, using the digits 0 to 9 and the letters A to F.
byte
A unit of computer memory that holds eight bits, or eight zeros and ones.
address
The number that tells you where a piece of information sits in memory, like a house number on a street.
dereference operator
The asterisk (*) in C. It takes an address and lets you see or use the value stored at that address.

Example

Think of a long street of mailboxes, each with a number painted on it. The number tells you where the box is, and the box holds one letter, just as an address in memory tells you where a byte is and the byte holds a value. If you know the number, you can walk straight to that box.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part explains how C uses addresses: the ampersand finds where a variable lives, a pointer stores that address, and star reaches the value stored there.

Key ideas

Words to know

address
The location of a value in the computer's memory, like a house number for data.
ampersand
The & symbol. Put in front of a variable, it gives you that variable's address.
pointer
A variable whose job is to store an address, so it points to where another value lives.
dereference
Using star on a pointer to go to the address it holds and get the value stored there.

Example

Think of a pointer as a treasure map. The map does not hold the treasure itself, but it tells you exactly where to go to find it. Using star p is like following the map to the spot and opening the box to see what is inside.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

A string is really a pointer, which is an address that shows where the string's characters start in computer memory.

Key ideas

Words to know

pointer
A variable that stores an address in memory, pointing to where some data is kept.
null character
A hidden character with all zero bits that marks the end of a string.
byte
A unit of memory that holds eight bits. In this lesson, each character in a string takes one byte.
ampersand
The & symbol. Placed before a variable, it means 'give me the address of this.'

Example

Think of a street where the houses are numbered in a row. If you know the address of the first house, you can walk to the next ones by counting along the street. A string works the same way: the pointer gives the first address, and the null character tells you when to stop.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–50:00

This part shows that the string type you have been using is really a char star, which is a memory address, and it explains how C uses addresses to work with text.

Key ideas

Words to know

char star
A variable that holds the memory address of a char, which is often the first character of a string.
address
The number that tells you where a piece of data is stored in computer memory, like a house number on a street.
typedef
A keyword that gives a type a new name of your own, such as calling an int an integer.
pointer arithmetic
Doing math with addresses, such as adding one to move from one byte to the next byte.

Example

Think of a long street where every house has a number. If you know the number of the first house, you can find the next house by adding one, and so on. A char star works like that first house number, and pointer arithmetic is the adding.

3 checked practice questions for this part are in the app.

Part 6 · 50:00–1:00:00

This part shows that pointers are addresses underneath, and that comparing two strings with == checks their addresses, so strcompare is needed to compare their letters.

Key ideas

Words to know

pointer arithmetic
doing math on an address, such as adding 1 to move to the next byte in memory.
dereference operator
the star symbol (*), which means go to the address and look at the value stored there.
syntactic sugar
an easier way to write something that the compiler quietly turns into a longer form.
strcompare
a built-in function that compares two strings letter by letter and returns zero if they match.

Example

Think of two houses on the same street with the same name written on their mailboxes. If you ask whether the two houses are the same house by checking their addresses, the answer is no, even though the names match. strcompare is like walking up to each mailbox and reading the name yourself to see whether they match.

3 checked practice questions for this part are in the app.

Part 7 · 1:00:00–1:10:00

This part shows why comparing and copying strings with the normal operators goes wrong, and how malloc and a careful copy loop fix it.

Key ideas

Words to know

address
The location in computer memory where a value is stored, written as a hexadecimal number.
malloc
A function that asks the operating system to set aside a number of bytes that your program can use.
null character
A special hidden character, written backslash zero, that marks the end of a string.
char star
The type written as char *, which means the variable holds an address of a character, such as the start of a string.

Example

Imagine two houses built from the same blueprint. They have the same layout, but they are two different houses with two different street addresses. If you compare their street addresses, they will not match, even though the blueprints do.

3 checked practice questions for this part are in the app.

Part 8 · 1:10:00–1:20:00

This part explains how a C program asks for its own memory with malloc, how to check that the request worked, and how to give that memory back with free.

Key ideas

Words to know

malloc
A built-in function that asks the computer for a chunk of memory of a size you choose, and returns the address of its first byte.
free
A function that gives a chunk of memory back to the computer so it can be reused. It is the opposite of malloc.
null
A special value that is the address zero. C uses it as a signal that something went wrong, such as there being no memory left.
valgrind
A tool that checks how your program uses memory and shows you the mistakes it finds.

Example

Think of malloc like renting a row of lockers at a gym. The gym tells you the number of the first locker, and it keeps track of how many lockers you rented. You are responsible for not going past the end of your row, and when you are finished, you give the lockers back so someone else can use them.

3 checked practice questions for this part are in the app.

Part 9 · 1:20:00–1:30:00

This part shows three common memory mistakes in C (forgetting to free memory, using variables that were never set, and using pointers that point nowhere) and how a tool called valgrind helps find some of them.

Key ideas

Words to know

valgrind
A helper tool you run before your program name. It watches your program and reports memory mistakes, like a teaching assistant checking your work.
memory leak
Memory your program asked for and then never gave back. It is like borrowing tools, never returning them, and then losing track of where they are.
garbage value
A random number left over in memory from earlier use. It is there when you have not put a number in a variable yourself.
dereference
Following a pointer to the place it points to and using what is there. In C you write a star before the pointer name to do this.

Example

Think of a used notebook someone gave you. Some pages have old scribbles on them, and you cannot tell what is important. You would not copy anything from those pages until you wrote your own notes first, which is how you should treat a variable you have not set.

3 checked practice questions for this part are in the app.

Part 10 · 1:30:00–1:40:00

This part explains how pointers work (following them to their values and pointing them at the same place), and why a swap function failed because it only received copies of the variables.

Key ideas

Words to know

pointee
The value or place that a pointer points to. Think of it as the thing the pointer's arrow leads to.
dereferencing
Following a pointer to its pointee so you can use or change the value stored there.
pass by value
Giving a function a copy of a variable's value, so changes inside the function do not affect the original variable.
stack frame
The block of memory the computer sets aside on the stack for one function call, holding that function's variables and copies of its arguments.

Example

Imagine two glasses, one blue and one orange, that need to trade their liquids. Pouring straight across fails, so you need a third empty glass called temp to hold one liquid while the other moves. The swap function had no such working plan for x and y, so it only moved copies of the values.

3 checked practice questions for this part are in the app.

Part 11 · 1:40:00–1:50:00

This part explains why a swap function that copies values fails, and how pointers (addresses) let a function change the original variables, with a short warning about memory overflows.

Key ideas

Words to know

pass by value
Giving a function a copy of a value. The function can change its copy, but the original stays the same.
pointer
A variable that holds the address of another variable, so it can lead you to where that value is stored in memory.
address
The location of a piece of data in the computer's memory, like a house number for a value.
buffer overflow
What happens when a program uses more space in a chunk of memory than that chunk can hold, which can spill into other memory.

Example

Think of a treasure map. If you give someone a copy of the treasure's value, they can change their copy, but the real treasure stays put. If you give them the map with the exact spot marked, they can go to that spot and change the real treasure. Passing a pointer works like giving the map.

3 checked practice questions for this part are in the app.

Part 12 · 1:50:00–2:00:00

This part shows how to get an integer and a string from the user in C without the CS50 library, and why a wrong memory address causes a segmentation fault.

Key ideas

Words to know

scanf
A function that reads input from the user's keyboard and stores it in a variable. You tell it what kind of input to expect with a format code.
segmentation fault
An error that happens when a program touches a part of computer memory that it was not allowed to use.
garbage value
Whatever random pattern of bits happens to be in memory before the program sets a variable to something known.
pointer
A variable that holds the address of a place in memory, rather than a normal value like a number.

Example

Imagine a parking lot that rents you four spaces, and you park five cars. The fifth car ends up in a space you never rented, and the lot manager shuts things down. In the same way, a program that asks for four bytes but writes five ends up touching memory that belongs to someone else.

3 checked practice questions for this part are in the app.

Part 13 · 2:00:00–2:10:00

This part explains why C makes reading text from users hard, and shows how to save data to a file that persists after a program ends, using a phonebook example.

Key ideas

Words to know

fopen
A C function that opens a file so a program can use it. It returns a pointer that points to the open file.
CSV
Short for comma-separated values. It is a plain text file where commas separate the values in each row, so spreadsheet programs can open it as columns.
NULL
A special value that means a pointer points to nothing. When a function returns NULL, it usually means something went wrong.
segmentation fault
A memory error that happens when a program tries to use memory that does not belong to it, which makes the program crash.

Example

Think of writing a grocery list on a notepad where you keep adding pages as you need them, instead of guessing how many pages you will need at the start. The getstring function works this way, adding memory one character at a time as the user types.

3 checked practice questions for this part are in the app.

Part 14 · 2:10:00–2:16:51

This part shows how to write a simple C program that copies a file one byte at a time, using command line arguments, fread, and fwrite.

Key ideas

Words to know

argv
A list of the words typed after a program's name on the command line. argv[1] is the first word after the program name.
binary mode
A way of opening a file that treats its contents as raw zeros and ones, instead of text. It is used for files like images.
fread
A function that reads one or more bytes from a file into memory and reports how many bytes it read.
bitmap
A type of image file, with a .bmp ending, that stores a picture as a grid of pixels.

Example

Copying a file this way is like copying a book by reading one letter at a time and writing each letter onto a new page in the same order. You keep going until you reach the last letter, and then you stop. The new book ends up identical to the original.

3 checked practice questions for this part are in the app.

← Week 3: Algorithms · Week 5: Data Structures →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. Harvard CS50 · CC BY-NC-SA 4.0 · not affiliated with or endorsed by Harvard. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York