Pointer topic 1

zhaozj2021-02-16  102

http://community.9cbs.net/expert/topic/3149/3149011.xml?temp=3.557986e-02

Pointer, naughty elves.

We should understand the program code this way: one can be interpreted by the machine. So, we can use a unified point of view to view the program and data and associate it with the pointer. Runtime data is always stored in memory to prepare the CPU from time to time. In order to quickly access the data, the internal power of the computer is bound to numbered memory - just like the house number. With a clear number, the CPU has access to the memory and accurately and accurately and extracts the data. Here, "Memory Number" has played a role of an indication address. It can be said that anything existing in memory has an address to facilitate access to it. In fact, this "memory number", or "address", is also a data - after all, its data type is unsigned integer, of course, can be saved. However, this data is a bit special: it is used to indicate the address of other data, so the variable type corresponding to the memory area is saved: pointer type. As the name suggests, the pointer is a variable with directivity. It's very simple to understand? But the more simple things are more, the more, such as ... the pointer has two properties, one is its value (that is, the address, whether it is true), one is its type (data type). Flower two branches, each table, let's talk about "address value". The address value has a size range, which is from 0 to 2 ^ n-1, which is from 0 to 2 n power, where N is the CPU word length, which represents the maximum range that can be addressed (such as 16-bit CPU) The word length is N, addressing ranges from 0 to 65535, and I don't understand the computer base). On different hardware platforms, the memory size of the pointer variable is proportional to its address value, that is, the pointer variable overcomes N internal storage (BIT). Zero in mathematics is a very special value, and the value of zero is also a special address. It uses a memory area indicating the address to zero-nonsense-but the operating system generally scores the zero address area to a safe area and can only be used as a capture cross-boundary pointer. Therefore, it is very secure to initialize a pointer to a zero address, and it is necessary (necessary to initialize the pointer in some compilers in C). In general, we don't have to care about the exact value of the address (real site, virtual address, logical address, physical address ...), which is still thrown to the compiler to do it, after all, the human brain is not a compiler :). Another attribute of a pointer is a data type. In fact, this type is the data type of the variable pointed to by the pointer, not the type of the pointer itself. Because the type of pointer is the so-called "user-defined type" instead of "compiler build type". This type has two functions: 1) Indicates that the compiler should get several bytes from the memory when performing the decomposition (the number of bytes corresponding to the data type), 2), and indicate the compiler in the pointer type. How to check when converting. Usually, the same address can be matched with different data types, which can get different types of data (or said that the value in the same block is interpreted as different types of data by the pointer). It is 1) the role of the pointer mentioned in.

For example: int A = 0xff00; char * p = (char *) & a; // mandatory type conversion, ensure type matching (reflection of the second point) Cout << * P << endl; / / Indicates that the compiler only gets one byte value (the embodiment of the first point effect, to take out the correct data) The result is 0, that is, the value of a low hexadecimal bit byte. // ---------- The above is about the preamble knowledge of the pointer, I hope everyone will take bricks.

[C / C duty room | Signal blade | 2004.7.6 10:36]

转载请注明原文地址:https://www.9cbs.com/read-13985.html

New Post(0)