Skip to content
All tags

#c-language

17 posts

How Programs Use Memory: Addresses, the Stack, the Heap and Return Addresses

Once a program is loaded into memory, the CPU runs it by following an address that says where it is now; each function call leaves a return address on the stack, a note saying where to go back to. A small C program that prints addresses lets you check this yourself.

How a Few Extra Characters Can Hand Someone Your Program: Buffer Overflows for Beginners

A buffer overflow is data written past the space set aside for it, clobbering whatever sits next door. In the demo, one extra character in a name turns is_admin from 0 into 65; the fix is a length check plus a length-limited function. Canaries, ASLR and NX are extra layers, not guarantees, and AddressSanitizer catches an overflow of a standalone array but, in my test, missed one between fields of the same struct.

Bitwise Operators, Conversions, and Masks | Stanford CS107 Lecture 4

Lecture 4 first shows that signed/unsigned conversion can preserve bits while changing meaning, that mixed comparisons may surprise, and how sign extension, zero extension, and truncation alter width. It then derives AND, OR, NOT, XOR, and bitmask idioms for testing, setting, clearing, and combining fields.

Integers, Bytes, and Two's Complement | Stanford CS107 Lecture 3

Lecture 3 starts with 32/64-bit address spaces, derives the ranges of unsigned and two's-complement signed integers, inversion-plus-one, and shared addition hardware, then separates unsigned modular arithmetic from C signed overflow and tests the model against four failure cases.

Bit Shifts, Bit Tricks, and GDB | Stanford CS107 Lecture 5

Lecture 5 extends masks to shifts, power-of-two and popcount tricks, then uses an absolute-value example to expose signed intermediate overflow at INT_MIN. Its second half establishes a GDB workflow around breakpoints, execution control, formatted printing, memory examination, and backtraces.

A First C Program, Binary, and Hexadecimal | Stanford CS107 Lecture 2

Lecture 2 puts C back into its Unix history and development environment: headers, main, printf, argc/argv, ssh, emacs, make, and executables. It then derives 8 bits = 1 byte, 256 byte patterns, and reliable conversion among decimal, binary, and hexadecimal.

From the Course Map to the Unix Command Line | Stanford CS107 Lecture 1

Winter 2026 opens by explaining why CS107 goes below programming-language abstractions: from bytes and memory through assembly and heap allocators. It then lays out the 40/10/20/30 grading structure and closes with a first tour of the Unix command line.

From String Search to Buffer Overflows—Input Validation Is Not Capacity Checking | Stanford CS107 Lecture 7

CS107 Lecture 7 builds pointer-based string scanning with strchr, strstr, and strspn, then shows why valid content can still overflow a buffer: safety requires input rules, destination capacity, termination, and memory-error detection.

A C String Is Not a Type but a Memory Contract | Stanford CS107 Lecture 6

CS107 Lecture 6 reduces C strings to character arrays, a terminator, and an address: every convenience in strlen, strcmp, strcpy, strncpy, and strcat depends on the caller preserving capacity and termination invariants.

From Comparators to a Fully Generic Bubble Sort | Stanford CS107 Lecture 13

CS107 Lecture 13 upgrades a Boolean callback to a three-way comparator, then combines void *, element width, and const void * callbacks into a fully generic bubble sort before mapping the design to qsort, bsearch, lfind, and lsearch.

Function Pointers Inject Ordering into Generic C | Stanford CS107 Lecture 12

CS107 Lecture 12 first uses char * for byte-wise generic swap and rotate, then uses a function pointer to separate bubble sort's traversal mechanism from its ordering rule: void * abstracts data types, while callbacks abstract behavior.

How void * Gives C Generics Without Pretending Types Still Exist | Stanford CS107 Lecture 11

CS107 Lecture 11 finishes the heap contracts of calloc, strdup, free, and realloc, then turns several typed swap functions into void * plus a byte count: C generics do not preserve an unknown type; they explicitly transfer responsibility for addresses, widths, and interpretation.

A Pointer Is Not Magic, but a Copyable Address | Stanford CS107 Lecture 8

CS107 Lecture 8 starts with address-of and dereference, explains why C pointer parameters are still passed by value, and shows how int *, char *, and char ** can modify caller-owned ints, chars, and pointers respectively.

An Array Is Not a Pointer, but They Cooperate in Expressions | Stanford CS107 Lecture 9

CS107 Lecture 9 uses seven C-string rules to separate array objects, pointer variables, and string literals: arrays often convert to first-element pointers in expressions, but storage, assignment, mutability, and sizeof remain different.

Stack vs. Heap Is About Lifetime and Ownership, Not Just Speed | Stanford CS107 Lecture 10

CS107 Lecture 10 moves from sizeof and pointer arithmetic to stack-frame lifetime: returning a local array leaves a dangling pointer; malloc crosses function returns but makes NULL handling, size arithmetic, ownership, free, and leaks the programmer's responsibility.

Computer Organization and Systems: From C and Pointers to x86-64 and Writing Your Own malloc | Stanford CS107 Course Overview

CS107 is Stanford's computer systems course: it goes from bits, C strings and pointers to x86-64 assembly and the runtime stack, and ends with writing your own heap allocator, across seven assignments. Lecture slides, assignment specs and worked lab solutions are public; the assignment starter code lives on Stanford's AFS and is out of reach without a campus account.

Operating Systems Principles: From Threads and Virtual Memory to File Systems | Stanford CS111 Course Overview

CS111 is Stanford's operating systems principles course, organized into concurrency, memory management and file systems, with nine assignments. All 28 lecture handouts, the nine assignment specs and three years of midterms and finals with solutions are public; the starter code requires a login on Stanford's myth machines, and the recordings are on Canvas.