For you to understand some data types better, I have to introduce you to the concept of computer memory. You’ll get familiar with its problems and limitations, which will help us reason about the characteristics of the data types you’re about to learn.
There are several types of computer memory, but that is out of scope for this article. What we’ll focus on is how data is stored and retrieved. We’ll talk about the efficiency of these operations and learn some basic optimizations.
The Idea Behind
Computer memory is a complex device. To serve as a storage medium, it needs to know how to store, retrieve, and delete bits of information efficiently. All these operations must be conducted in the most optimal manner possible. Let’s analyze how it is built.
Linear Memory
There are three basic memory models today, but I’ll focus only on the first one: the Flat or Linear Model. Can you remember music cassettes or compact discs? Some time ago, they were used to store music, movies, pictures, and text. Now, we stream everything from the internet, so these storage mediums have become practically obsolete. However, they are a great example of linear data layout.
For those who have never used a music cassette, this is how it worked. Inside a flat cartridge was a long magnetic tape held by two spools. When placed into a player’s deck, two operations could be performed: data could be read from or overwritten on the tape. A special head was used for both operations, working on the moving tape.
Linear Memory Holds Linear Data
Every time you want to read or write to the tape, you need to rewind it to the desired position before performing the task. Imagine you recorded ten songs from the radio onto a cassette. After a few days, songs two and seven became boring, so you decided to overwrite them with better tracks. You had to choose carefully: the new songs couldn’t be longer than the existing ones, or they would overwrite the remaining tracks. They also shouldn’t be shorter, or fragments of the old songs would still be played. Does that sound like a convenient storage method?
Magnetic tape could store both music (analog signals) and digital data. When dealing with digital data, searching and overwriting become even more difficult: rewind, stop, write, rewind, read, and so on. Unlike songs, digital data is often stored in chunks as part of a larger structure, making processing even slower.
Memory Cells
When dealing with music, each track was a kind of single cell, holding a musical composition. In the case of computer data, it’s not much different. A cell is a small storage unit holding a basic piece of information, typically a byte. Cells have their addresses, just like songs had track numbers. Both the address and the data itself are represented as numbers, often in hexadecimal notation.
Address | Data | Information |
---|---|---|
… | … | … |
1000 | 72 | Letter “H” |
1001 | 69 | Letter “E” |
1002 | 76 | Letter “L” |
1003 | 76 | Letter “L” |
1004 | 79 | Letter “O” |
The table above shows how the string "HELLO"
is stored in linear memory. It occupies consecutive memory cells. However,
this is an ideal scenario. In reality, data isn’t always stored contiguously. When you delete some data, empty spaces (cells)
remain in memory. These gaps get filled with chunks of new data in the next write cycle. This phenomenon is called fragmentation.
Fragmentation
To illustrate fragmentation, let’s take a part of linear memory containing the string "HELLO BIG WORLD"
.
Now, let’s say we delete the substring " BIG "
and replace it with "SAVAGE"
.
- Initially, the memory contains:
HELLO BIG WORLD
- We clear cells from index 5 to 9:
HELLO WORLD
- We overwrite the empty cells:
HELLOSAVAG WORLD
Since there were only five empty cells available, but “SAVAGE” requires six, the last character ('E'
) had to be
stored in the next free space. Now, the data is fragmented, requiring additional processing to reconstruct the string correctly.
You might wonder how the system retrieves fragmented data. Specialized data structures manage this type of storage, ensuring data is reconstructed properly. You’ll learn about them in the upcoming articles of this tutorial. Stay with me!
Low-Level Structures
There are two fundamental data structures that serve as the foundation for many others:
- Linked Lists: Efficient for inserting and deleting data.
- Arrays: Provide fast access to elements.
You’ll get more familiar with them in upcoming posts.
Summary
I’ve covered only one memory type - Linear Memory - but that’s enough for now. Make sure you understand the key concepts: Address, Cell, and Fragmentation. These are foundational to understanding more advanced topics in memory management.