Video by freeCodeCamp.org · taught by David J. Malan · streamed from YouTube. All rights remain with the creator.
Lesson 4: Memory, Pointers and Files
About this lesson
This lecture takes the lid off the computer's memory and explains ideas that most programming languages hide. It begins with hexadecimal, a way of writing numbers in base sixteen that is convenient for describing memory, and shows why each hexadecimal digit corresponds to four bits. From there it introduces addresses, the numbered locations where every value lives, and pointers, variables whose values are addresses. You see how to find the address of a variable, how to follow a pointer to the value it refers to. The lecture then reveals what strings really are in C: pointers to the first of a sequence of characters. That explains why comparing two strings with the equals operator gives unexpected results, and why copying a string with an assignment does not really copy it. To make a true copy, programs must request memory while they run with a function that allocates it, and must later give it back, and you learn about memory leaks and a tool that detects them and other memory errors. The lecture describes how a program's memory is organised into regions, including the stack, where each function call gets its own space, and the heap, used for allocated memory. A famous example, a function meant to swap two values that appears not to work, shows why functions receive copies of their inputs and how passing addresses fixes it. The lecture also explains stack and buffer overflows and closes with reading and writing files, including recognising image files by their first bytes. Afterwards, read Chapter 4 of the course book.