Lessons and the learning game

Pointers and memory in C

What is really in an Arduino Uno's 2 KB of RAM, and the idea that makes C what it is: a variable that holds an address.

How it works

Pointers and memory in C follows C from zero, which met & and * once. Here they are the whole story. Every lesson runs on the code bench: an Uno with a red lamp on pin 13, yellow on 12, green on 11 and a button on pin 2.

Each lesson opens a working C sketch that prints what it is doing: addresses, sizes, the bytes of a number. You run it, read the serial monitor, then change a line and run again. The checks read the serial output and the lamps. No check asks for an exact address, because addresses move when a sketch changes; they ask for the gaps, sizes and facts the sketch works out.

The lessons

  1. Addresses: where globals sit, neighbors in an array, little-endian byte order, and the map of RAM from 0x100 to 0x8FF.
  2. Pointers: & and *, writing through a pointer, a swap that works, NULL and the check before you follow it, and a button that fires at whatever the pointer aims at.
  3. Arrays are pointers: an array's name is the address of its first element, a[i] is *(a + i), walking an array with a pointer, the sizeof trap in a function, and a lamp chaser that is a moving pointer.
  4. Strings by hand: chars and a zero byte. Length, upper case and reverse with pointers, a buffer overflow you can watch trample its neighbor, and the bounded copy that stops it.
  5. Structs and pointers: a fleet of ships, sizeof with no padding, the arrow operator, a copy against a pointer, and a search that returns NULL.
  6. Stack and static: globals low and the stack high, a frame per call, a local that forgets against a static that remembers, and a buffer that eats the free RAM.
  7. Function pointers: one call that adds or multiplies, a sort that takes its order as a function, callbacks, and a table of lamp modes.

The project: a command parser

The finale builds the little shell every serious piece of firmware has. A line is read into a fixed 32-byte buffer, split in place into words with pointers (the same argc and argv a PC program gets), and looked up in a table of commands, each row holding a function pointer. It lights lamps by name, adds numbers it parsed by hand, and answers bad input with a useful message. You add an echo command of your own, then make it yours: blink, peek at any address, show the free RAM, or repeat the last line.

What the Uno does differently

  • RAM is 2048 bytes at 0x100 to 0x8FF; below 0x100 are the chip's hardware registers. A pointer is 2 bytes, not the 8 of a 64-bit laptop.
  • String literals are copied into RAM at startup, because the chip reads flash with a different instruction. Text costs RAM unless you use F().
  • The 8-bit AVR adds no padding to structs, so a struct can be a different size here than on a PC. Never send one raw between two machines.
  • There is no memory protection. Following NULL or running off an array does not crash with a message; it quietly reads or writes the wrong bytes.

After this course

Under the hood takes the Arduino wrapper off and reads the chip's registers through pointers, and the C++ courses show what references and classes add on top.