Author Xie Enming, public number “programmer union”. Please indicate the source of reprint. Original: www.jianshu.com/p/e2954324e…

The whole series of C language Exploration

Content abstract


  1. preface
  2. Different types of storage
  3. memory
  4. conclusion
  5. Part 1 Lesson 4 Notice

1. Introduction


After a class C language exploration trip | the first part of the third class: your first program, we already know how to display text on the screen.

That’s all we can do for now. And that’s normal, because we haven’t learned one of the most important things about programming: variables.

There are many knowledge points related to variables, and we will learn them in several classes:

  • The memory thing;
  • Declare variables;
  • Display variable content and extract input content.

In this lesson we will learn about computer memory.

Every normal person has memory, our old friend computer is no exception, but it is “better” than us, it has several kinds of memory (memory).

Why do computers have multiple types of memory? Isn’t just one kind enough?

Not enough: The main problem is that we want computers to have memory that is both fast (able to extract information quickly) and large (able to store a lot of data).

You will laugh, however, because we do not yet have a memory that is both fast and large. More precisely, high-speed memory is too expensive, so the capacity can only be low.

Therefore, we can only equip the computer with:

  • Or very fast but low capacity memory;
  • Or large but slow storage.

2. Different types of memory


Here’s a list of the computer’s memory types, in order from fastest to slowest:

  1. Register: Located on the Processor, the access speed is very fast (because “the nearest house gets the moon first”).

  2. Cache: Used to link registers to memory.

  3. Memory: This is the most common Memory we deal with when programming, and one of the most important data we care about when buying a computer.

  4. A Hard Disk, “Hard Disk,” means a C or D drive on a Windows computer that you use to store files, photos, videos, etc.

As mentioned above, we have classified the memory here from the fastest (registers) to the slowest (hard disks). You should be able to deduce that registers are much smaller than hard disks.

Registers can only store a small amount of data, while hard disks can store a large amount of data.

When we say a memory is slow, we are talking about the processing speed of a computer.

It takes about 8 milliseconds to read a hard disk, which is pretty slow for a computer. (The average home COMPUTER can perform billions of calculations per second, or 1 gigahertz; If your computer is running at 2.5 GHz, that’s 2.5 billion calculations per second. Dual-core computers at home today typically run at around 5 billion calculations per second (5ghz).

What do we need to know about this?

In fact, you only need to care about part of it. Remember, in programming, we’re basically just dealing with memory. We will also learn how to read and write hard drives and create files on hard drives later in the course. As for registers and caches, we don’t really care. It’s a computer thing.

Of course, registers are often used in very low-level languages such as Assembly (ASM).

I used to follow the teacher Wang Shuang’s “assembly language” second edition to learn assembly, the book is quite good.

But WHAT I want to say is that assembly, even just doing multiplication, is not easy. Fortunately, multiplication is easy to do in C (or most other languages).

It’s also important to remember that of the four types of storage mentioned above, only the hard disk can store data permanently. The rest of the memory (registers, cache, memory) is temporary memory: it is cleared when you shut down the computer.

Fortunately, when you turn your computer back on, the data on your hard drive is always there, so your computer knows who it is.

3. The memory


Since we’re going to spend most of our time dealing with memory, why don’t we get to know our friend?

Let’s take it slow with a zoom lens.

We start with an overall picture of our computer working environment:

You’re probably familiar with the mouse, keyboard, and display in the picture above, as well as the big thing on the right: the main box.

What we care about is the main box, because that’s where our memory resides.

Of course, our hard disk, registers and height cache are all in this main box.

Let’s open the lid and see what’s inside:

It looks a little messy, but don’t worry, we don’t need to know what each part does, we just need to worry about the main character of this chapter: memory.

Where is it? As you may have noticed, the area I’ve marked in red is where the memory is.

I have not indicated the other two types of storage: caches and registers, because they are too small to be easily seen.

Here is what the memory module looks like:

Memory is also often referred to as RAM, short for Random Access Memory. So later in the course we’ll sometimes call memory RAM as well.

Memory is


From the outside, even if we look at the memory stick under a microscope, it doesn’t make sense. What we care about is the inner workings of it.

Here is an illustration of the memory mechanism:

Pretty lean, right? Because we don’t need to know too many details. Otherwise we might be told, “You, you know too much”. And then… And then…

If you can understand the picture above, that’s enough for now.

As you can see, in the figure above we care about two columns:

  • The left column is the address: a memory address is a number that allows the computer to locate in memory, starting at 0 and ending at 3448765900126. This is just an example, I don’t know how many addresses are in this memory (not necessarily ending at 3448765900126), the capacity of each ram is different. Generally speaking, the larger the memory, the more addresses and more information can be stored.

  • The right column is for values (numbers) : each memory address stores a value for later use by the computer, and only one value can be stored at each address.

Our memory can only store numbers.

So how do you store an English word in memory?

Good question. In fact, to computers, letters are also represented as numbers. A sentence is a series of numbers.

There is a table that stores the correspondence between numbers and letters. This table is called the ASCII table. For example, the number 67 corresponds to a capital C.

We’ll leave the ASCII table out for now, but we’ll come back to it later in the course.

If we want to store a number in memory, say, 5, memory will look at which address is available and store 5 inside (say, 35487412). And then when memory wants to get that number again, it goes to 35487412, and it gets 5.

That’s basically how memory works. It’s a little fuzzy for now, but it will become clearer in later chapters, I promise you.

7. Part 1 Lesson 5 Notice


That’s all for today’s lesson, come on!

Next lesson: C exploration trip | lesson 5: the first part of the world of the variable (2), the variable declaration

In the next lesson, we will continue to explore the magical world of C variables.


I am Xie Enming, the operator of the public account “Programmer Union”, and the moOCs elite lecturer Oscar, a lifelong learner. Love life, like swimming, a little cooking. Life motto: “Run straight for the pole”