How do computers remember where they store things?

The exact methods depend on what specifically you are talking about and how deep you want to go. For example, storing files on a hard drive is different than storing something in memory or storing something in a database. Although the concepts are similar. And how you do it at a programming level is a different explanation than how a computer does it at the I/O level.

Most systems use some sort of directory/index/registry mechanism to allow the computer to find and access the data. This index/directory will contain one or more keys, and the address the data is actually located in (whether that be hard drive, RAM, database, etc.).

Hard Drive Example

If you want to access a file on the computer. When you access it, the computer looks in its index for the name of the file, and in that index is the physical address where the file as actually stored on the hard drive. Knowing this address, it then gets the data and does whatever it was programmed to do. This is all encapsulated from the user and happens behind the scenes.

Or an operating system may decide that certain data is always stored at a certain location. For example, in most operating systems, the boot sector is always at the first sector on the drive. It doesn't need an index for this, since the OS assumes it is always at the same spot on the hard drive.

Computer Program Example

A computer program can access memory in a variety of ways. Typically the operating system gives the program an address space, and the program can do what it wants with that address space. It can write directly to any address within its memory space, and it can keep track of that how it wants. This will sometimes vary by programming language and operating system, or even according to a programmer's preferred techniques.

As mentioned in some of the other answers, the exact coding or programming used differs, but typically behind the scenes it uses something like a stack. It has a register that stores the memory location where the current stack starts, and then a method of knowing where in that stack a function or variable is.

In many higher level programming languages, it takes care of all that for you. All you have to do is declare a variable, and store something in that variable, and it creates the necessary stacks and arrays behind the scenes for you.

But considering how versatile programming is, there isn't really one answer, since a programmer can choose to write directly to any address within its allocated space any time (assuming he is using a programming language that allows that). Then he could store its location in an array, or even just hard code it into the program (i.e. the variable "alpha" is always stored at the beginning of the stack or always stored in the first 32 bits of allocated memory).

Summary

So basically, there has to be some mechanism behind the scenes that tells the computer where data is stored. One of the most popular ways is some sort of index/directory that contains key(s) and the memory address. This is implemented in all sorts of ways and is usually encapsulated from the user (and sometimes even encapsulated from the programmer).

About the Author

WisTex's picture
Scott M. Stolz

Recommended Content