In fact, the way C language operates memory is very simple. The CPU finds our memory through the address (the memory can be a memory stick, graphics card, USB, etc.). How are the memory resources found? We need to find the resources we need, which is the addressing we often see in hardware. By finding its address, that is, the house number of the memory, we can find this piece of resource, and then we can use the data here. Here is a new concept-address. In C, we did not name a keyword called address, nor did we have such a type. C uses pointers to describe the concept of address. We need to rely on pointers to access memory space. Finding resources is actually using pointers to point to its address. We can understand that pointers are synonymous with memory type resource addresses and house numbers.
Pointer storage The pointer points to the memory resource, but where is it stored? The pointer is just a concept. If we want to use this pointer, we should have a pointer variable. A variable is a piece of land we circle in the memory, and then we store something on this land. Variables really exist in the memory. Pointer variable: A box that stores the concept of pointer. Let's take an example. For example, a variable of type int a; the red box is a 32-byte area that we have circled, which stores the value of the int variable (the int variable may be understood as a different size in different compilers). So how do we define a pointer variable?
In order to show that the ability of pointers to store addresses is different from other variables, a new symbol * is created. If the * is followed by a number, we all know that it means multiplication. Once the * is followed by a legal identifier, such as *p (pointer variables are defined with the name starting with P_, which stands for pointer), this is a pointer variable named p. The C language compiler has two questions about the special concept of pointers? How big should a box be when allocating it? Memory is a large space, and we use the address of the memory, that is, its house number, to find a specific small piece of memory. Now our computer has 1G memory, which is quite simple. What we need now is how to access every byte of the memory in this 1G. What should we use to describe this memory? 1GB=2^10MB=2^20KB=2^30Byte. So if we want to describe every byte of the memory, if we use integers to assign a house number to these memories in sequence, starting from 0, one byte is a room, then 1G of memory requires 2^30 numbers. We must ensure that all addresses of the memory can be accessed through pointers. In a 32-bit operating system: two concepts about 32 bits. 1) Data processing is 32 bits, and data higher than 32 bits cannot be processed. 2) The size of the CPU operating memory is also 32 bits. It can only operate 2^32 memory, 2^32Byte=4GB, so the maximum memory of a 32-bit operating system can only be 4G, and it is useless to have more memory. A pointer variable is also a variable that stores an address. How much space is needed to store this address? In a 32-bit system, a pointer is 4 bytes. When we understand this concept, we know that char *p or xxx *p, the address of this pointer variable is 4 bytes. 4 bytes is the house number of other memory addresses, which is an integer. The pointer variable interprets it as an address. How to read the memory pointed to by the address stored in the box? When we see a pointer, we should think that it stores an address, so how big does the memory pointed to by this address need to be? When the compiler sees a pointer variable declaration, it needs to know the size of the space pointed to by this pointer variable. Does the space pointed to by the pointer only read one byte when we read it? That's the same as the char type. So the definition of a pointer is that we not only need to know the first address of the data storage, but also how many bytes should be read in this memory. If we define a char *p, the compiler first sees that we have declared a pointer variable *p, and then finds the size of the preceding char type, which lets the compiler know that if we need to read the value of this pointer, we should read one byte of memory. There can be many types in front, such as int, double, struct, etc.
This content is originally created by EEWORLD forum user 莫等闲11. If you need to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source