Shire's Essays in C Language: A Brief Discussion on Pointers (1)

For experienced connoisseurs, handy tools are often more difficult at the beginning than those that are easy to use.
-"C Trap and Flaws", [America] Andrew Koenig

Preface

Hello, everyone, I am Xeler, you can call me Shire (the ruthless stalking machine).
Recently, I was chatting and blowing water in the Bit brothers group. I accidentally swiped my mouth and blew off the cowhide that wrote a pointer.
There was no turning back arrow when I opened the bow, so I had to write, so I have this strange article you see now.
Did this guy write prose? (Whisper bb)
It's this bad guy, add him to WeChat harassment

Preface

Since we want to talk about pointers, of course, we must first show what a pointer is:

int *p;

I believe people who read this article know how to declare a variable in C language. The above code declares the simplest pointer. In fact, a pointer is a special variable. Just add an asterisk * in front of the variable name when declaring it, and the compiler will take care of the rest for us.
No difficulty, right? But if we want to apply it freely, we also need to look at the sacred heel of the pointer.

The nature of pointers

This topic is actually very big, what exactly are pointers? The books on the market have their own opinions on this. Some call it "a signpost", some call it "an address", and some even call it "a pointer to a certain address" (you leave it alone). Let's put it here).
Back to the original point, we can see that so many explanations actually vaguely reveal the same content, that is:

A pointer variable is a variable that points to a certain address.
——The
person above Xeler also put it here.
-Shire

Don't worry! To understand where the pointer to the pointer array pointer to the pointer function came from, we need to understand a lot of things. For example, the first step is to figure out what the memory is?

RAM

Everyone on earth knows that computers use electricity (nonsense). But have you ever wondered how the memory uses electricity?
The answer is latches. We don’t need to care about so many technical details today. You just need to know that there are a lot of latches in the memory (4G of memory is almost 4 1024 1024 1024 8=34,359,738,368, about 34 billion) latches, these When something is energized and running, there are only two states: high level or low level, which we call 1 and 0. The memory accessory is like a warehouse, managing all these latches and numbering them one by one.
So what do these numbers look like? In fact, the number of these latches in groups of 8 is close to this:
Cardiac arrest
cough, it should actually grow like this↓

0x1234 5678,又做0x12345678

The number beginning with 0x means hexadecimal, which is not in the category of this article. In fact, this thing is the address we often talk about.
Tips: What we often say about 1GB is actually 1024MB=1024*1024KB, each KB is 1024Byte, and 1Byte=8Bit. The 32-bit and 64-bit CPUs we often say actually represent 4Bytes. The data is still 8Byte data (computer uses 1024 hexadecimal, not the 1000 hexadecimal used by the hard disk/U disk manufacturer to calculate the capacity under normal circumstances, you can see if your hard disk is like this (1T is only about 931G))

Operating system scam

Speaking of this, some more savvy children may go back and try it out, but I don’t know if you have discovered a situation: at certain moments, two programs will use the same memory space at the same address?
This situation occurs because the operating system has set up a scam: virtual memory. Regarding its principle, we will not list it for the time being. We only need to know that the memory in the application program is 16G that belongs to it alone, and this size It is managed by the operating system, and it is based on this virtual memory area that we may be able to run high-speed applications on some computers that are not rich in hardware performance. And the memory address obtained by our pointer is actually the address of this virtual memory (we call it a logical address), not the real memory address (we call it a physical address).
So, good boy, don't try to access other applications through pointers.

So what exactly is a pointer

After the above series of explanations, you should be able to understand exactly what the address is: it is just a number of the unit storing data in the memory. From this, we can draw a conclusion: in fact, the pointer is a hexadecimal number. Integer variables.
That's right, after all, the strict full name of a pointer is a pointer variable, a variable variable, that is, a thing for storing 0 and 1. (Strictly speaking, there are only integer variables and floating-point variables in the C language, and the essence of characters and pointers are integer variables) It
is correct, but far from enough. Understanding what the pointer is is only the first step. To fully understand the twists and turns in the pointer, we still have a little way to go. (Compared to a fingertip Galaxy) But don't worry, please allow me to leave it to the next issue. Why don't you give me a three-link support?

Guess you like

Origin blog.csdn.net/u013506650/article/details/115321913