How Computers Think
Introduction
Computers. What goes on inside their heads? How do they think? Well, it’s nothing like the human brain. Instead, it’s a precise and logical process that involves managing memory and executing instructions. Let’s delve into the inner workings of computers to understand how they “think.”
Storing Files in Memory
You’re probably familiar with storing files on computers. When you save a file, like a text file, it has a file name and some content. Think of your computer’s memory as a large grid where files and data are stored.
When you save a file, it has a file name, which acts like an icon you click on. This file name is stored in the computer’s file directory, and it includes the location or address of where the file’s content is in memory. Computers are excellent at jumping directly to a specific memory spot if they know the address, which is literally a number. Each memory spot has a unique number, called an address or a “pointer.”
Example: Saving a Text File
Let’s say you save a text file named example.txt
. Here’s what happens:
- File Name:
example.txt
is stored in the file directory. - Pointer: The file name is associated with a pointer that indicates where the file’s contents start in memory.
- File Size: Additional information, like the file size (e.g., 4 kilobytes), is stored.
When you access this file, the computer uses the pointer to jump to the memory location, reads the next 4 kilobytes, and loads the content.
Variables and Memory in Programming
When we write and run a computer program, we directly interact with the computer’s memory. Programs create small bits of data called “variables,” which have names and point to specific data locations in memory.
Example: Simple Program
Consider this simple program:
A = 2
B = 3
C = A + B
Here’s how the computer processes it:
- A = 2: The computer creates a variable
A
and points it to the value2
in memory. - B = 3: Similarly, it creates a variable
B
and points it to the value3
. - C = A + B: The computer fetches the values of
A
andB
, adds them, and stores the result in a new variableC
.
Understanding Memory Pointers
Knowing how computers store and fetch data is crucial, especially with complex data structures like lists.
Example: List in Python
Consider this Python code:
A = [1, 2, 3, 4, 5]
B = A
A[0] = 6
- A = [1, 2, 3, 4, 5]: A list is created, and
A
points to this list in memory. - B = A: Instead of duplicating
A
,B
points to the same memory location asA
. - A[0] = 6: Changing the first element of
A
also changes it forB
because both point to the same memory location.
Conclusion
When programming, we work directly with the computer’s memory, putting data into it, fetching data out, and manipulating it. Understanding this process helps us write more efficient and bug-free code. Having a mental model of what’s happening behind the scenes is invaluable for any programmer.
Understanding how computers think gives us better insight into the precise and logical world of computing, allowing us to harness their power more effectively.