Your computer is, at its core, a processor (the Central Processing Unit or CPU) and a vast meadow of switches (the Random-Access memory or RAM) that can be turned on or off by the processor. We say that a switch holds one bit of information.You’ll often see 1 used to represent “on” and 0 used to represent “off.”
Eight of these switches make a byte of information. The processor can fetch the state of these switches, do operations on the bits, and store the result in another set of switches. For example, the processor might fetch a byte from here and another byte from there, add them together, and store the result in a byte way over someplace else.
The memory is numbered, and we typically talk about the address of a particular byte of data. When people talk about a 32-bit CPU or a 64-bit CPU, they are usually talking about how big the address is. A 64-bit CPU can deal with much, much more memory than a 32-bit CPU.
In Xcode, create a new project: a C Command Line Tool named Addresses.
The address of a variable is the location in memory where the value for that variable is stored. To get the variable’s address, you use the & operator:
#include <stdio.h> int main(int argc, const char * argv[]) { int i = 17; printf("i stores its value at %pn", &i); return 0; }
Notice the %p token. That’s the token you can replace with a memory address. Build and run the program. You’ll see something like:
i stores its value at 0xbffff738
although your computer may put i at a completely different address. Memory addresses are nearly always printed in hexadecimal format.
In a computer, everything is stored in memory, and thus everything has an address. For example, a function starts at some particular address. To get that address, you just use the function’s name:
int main(int argc, const char * argv[]) { int i = 17; printf("i stores its value at %pn", &i); printf("this function starts at %pn", main); return 0; }
Build and run the program.